欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IDEA+maven+SpringBoot+JPA+Thymeleaf實(shí)現(xiàn)Crud及分頁

 更新時(shí)間:2018年03月08日 08:36:56   作者:{name:  
這篇文章主要介紹了不需要電腦任何操作基于IDEA + maven + SpringBoot + JPA + Thymeleaf實(shí)現(xiàn)CRUD及分頁,需要的朋友可以參考下

一、開發(fā)環(huán)境:

1、windows 7 企業(yè)版

2、IDEA 14

3、JDK 1.8

4、Maven 3.5.2

5、MariaDB

6、SQLYog

二、Maven設(shè)置:

Maven目錄下的conf目錄下的settings.xml做如下內(nèi)容的添加:

1、使用阿里云的倉庫,比官網(wǎng)訪問速度快很多

 <mirror>
 <id>nexus-aliyun</id>
 <mirrorOf>central</mirrorOf>
 <name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

2、全局JDK配置

<!-- 全局jdk配置,settings.xml --> 
<profile> 
 <id>jdk18</id> 
 <activation> 
 <activeByDefault>true</activeByDefault> 
 <jdk>1.8</jdk> 
 </activation> 
 <properties> 
 <maven.compiler.source>1.8</maven.compiler.source> 
 <maven.compiler.target>1.8</maven.compiler.target> 
 <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> 
 </properties> 
</profile>

三、IDEA基本設(shè)置:

1、Maven設(shè)置:選擇Maven目錄,同時(shí)配置文件和本地倉庫

2、字符編碼設(shè)置

四、使用IDEA創(chuàng)建Maven工程:

選擇Enable Auto-Import,創(chuàng)建好的工程目錄如下圖:

五、體驗(yàn)SpringBoot結(jié)合JPA的快速開發(fā)吧

1、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>cn.temptation</groupId>
 <artifactId>studySpringBoot</artifactId>
 <version>1.0-SNAPSHOT</version>
 <!-- 使用spring boot的默認(rèn)設(shè)置 -->
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.0.0.RELEASE</version>
 </parent>
 <dependencies>
 <!-- web -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <!-- thymeleaf -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <!-- mysql-->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.21</version>
 </dependency>
 <!-- jpa-->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>
 </dependencies>
</project>

2、resources目錄下新建application.properties(當(dāng)然喜歡用yaml的可以用yaml)

# 數(shù)據(jù)庫連接
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=sa
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# JPA配置
spring.jpa.properties.hibernate.hbm2ddl.auto=update

3、創(chuàng)建SpringBoot程序啟動(dòng)類SpringbootApplication.java

package cn.temptation;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootApplication {
 public static void main(String[] args) {
 // SpringBoot項(xiàng)目啟動(dòng)
 SpringApplication.run(SpringbootApplication.class, args);
 }
}

4、創(chuàng)建實(shí)體類Category.java

package cn.temptation.model;
import javax.persistence.*;
// 建庫建表
//DROP TABLE category;
//
//CREATE TABLE category
//(
// categoryid INT AUTO_INCREMENT PRIMARY KEY,
// categoryname VARCHAR(10) NOT NULL
//);
//
//INSERT INTO category VALUES(NULL, '手機(jī)'), (NULL, '圖書'), (NULL, '服裝'), (NULL, '鞋帽');
//
//SELECT * FROM category;
@Entity
@Table(name = "category")
public class Category {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "categoryid")
 private Integer categoryid;
 @Column(name = "categoryname")
 private String categoryname;
 public Integer getCategoryid() {
 return categoryid;
 }
 public void setCategoryid(Integer categoryid) {
 this.categoryid = categoryid;
 }
 public String getCategoryname() {
 return categoryname;
 }
 public void setCategoryname(String categoryname) {
 this.categoryname = categoryname;
 }
}

5、創(chuàng)建DAO接口CategoryDao.java

package cn.temptation.dao;
import cn.temptation.model.Category;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CategoryDao extends JpaRepository<Category, Integer> {
}

6、創(chuàng)建控制器類CategoryController.java

package cn.temptation.web;
import cn.temptation.dao.CategoryDao;
import cn.temptation.model.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class CategoryController {
 @Autowired
 private CategoryDao categoryDao;
 /**
 * 不分頁查詢
 *
 * @return
 */
// @RequestMapping("/categorylist")
// public ModelAndView categorylist() {
// List<Category> list = categoryDao.findAll();
//
// ModelAndView mav = new ModelAndView("categorylist");
// mav.addObject("list", list);
// return mav;
// }
 /**
 * 分頁查詢
 *
 * @return
 */
 @RequestMapping("/categorylist")
 public ModelAndView categorylist(@RequestParam(value = "start", defaultValue = "0") Integer start,
   @RequestParam(value = "limit", defaultValue = "2") Integer limit) {
 start = start < 0 ? 0 : start;
 Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "categoryid");
 Pageable pageable = new PageRequest(start, limit, sort);
 Page<Category> page = categoryDao.findAll(pageable);
// System.out.println(page.getNumber());
// System.out.println(page.getNumberOfElements());
// System.out.println(page.getSize());
// System.out.println(page.getTotalElements());
// System.out.println(page.getTotalPages());
// System.out.println(page.isFirst());
// System.out.println(page.isLast());
 ModelAndView mav = new ModelAndView("categorylist");
 mav.addObject("page", page);
 return mav;
 }
 /**
 * 類別新增視圖
 * @return
 */
 @RequestMapping("/categoryinit")
 public String categoryinit() {
 return "categoryinit";
 }
 /**
 * 類別新增操作
 * @param model
 * @return
 */
 @RequestMapping("/categoryinsert")
 public String categoryinsert(Category model) {
 categoryDao.save(model);
 return "redirect:categorylist";
 }
 /**
 * 類別刪除操作
 * @param categoryid
 * @return
 */
 @RequestMapping("/categorydelete")
 public String categorydelete(Integer categoryid) {
 categoryDao.deleteById(categoryid);
 return "redirect:categorylist";
 }
 /**
 * 類別編輯視圖
 * @param categoryid
 * @return
 */
 @RequestMapping("/categoryedit")
 public ModelAndView categoryedit(Integer categoryid) {
 Category model = categoryDao.getOne(categoryid);
 ModelAndView mav = new ModelAndView("categoryedit");
 mav.addObject("category", model);
 return mav;
 }
 /**
 * 類別編輯操作
 * @param model
 * @return
 */
 @RequestMapping("/categoryupdate")
 public String categoryupdate(Category model) {
 categoryDao.save(model);
 return "redirect:categorylist";
 }
}

7、resources目錄下新建templates目錄,創(chuàng)建表現(xiàn)層:類別列表頁面(categorylist.html)、類別新增頁面(categoryinit.html)、類別編輯頁面(categoryedit.html)

類別列表頁面(categorylist.html)

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>類別列表</title>
 <style>
 table, th, td {
 border: 1px solid green;
 border-collapse: collapse;
 }
 </style>
</head>
<body>
<a th:href="@{/categoryinit}">新增</a>
<table>
 <tr>
 <th>類別編號(hào)</th>
 <th>類別名稱</th>
 <th>操&nbsp;&nbsp;作</th>
 </tr>
 <!--不分頁遍歷-->
 <!--<tr th:each="item : ${list}">-->
 <!--分頁遍歷-->
 <tr th:each="item : ${page.content}">
 <td th:text="${item.categoryid}">類別編號(hào)</td>
 <td th:text="${item.categoryname}">類別名稱</td>
 <td>
 <a th:href="@{/categoryedit(categoryid=${item.categoryid})}">編輯</a>&nbsp;&nbsp;
 <a th:href="@{/categorydelete(categoryid=${item.categoryid})}">刪除</a>
 </td>
 </tr>
</table>
<div>
 <a th:href="@{/categorylist(start=0)}">[首頁]</a>&nbsp;&nbsp;
 <a th:if="${not page.isFirst()}" th:href="@{/categorylist(start=${page.number-1})}">[上頁]</a>&nbsp;&nbsp;
 <a th:if="${not page.isLast()}" th:href="@{/categorylist(start=${page.number+1})}">[下頁]</a>&nbsp;&nbsp;
 <a th:href="@{/categorylist(start=${page.totalPages-1})}">[末頁]</a>
</div>
</body>
</html>

類別新增頁面(categoryinit.html)

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>類別新增</title>
</head>
<body>
<form action="categoryinsert" method="post">
 <label for="txtCategoryname">類別名稱:</label>
 <input type="text" id="txtCategoryname" name="categoryname" /><br/>
 <input type="submit" value="提交">
</form>
</body>
</html>

類別編輯頁面(categoryedit.html)

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>類別編輯</title>
</head>
<body>
<form action="categoryupdate" method="post">
 <input type="hidden" id="txtCategoryid" name="categoryid" th:field="${category.categoryid}"/><br/>
 <label for="txtCategoryname">類別名稱:</label>
 <input type="text" id="txtCategoryname" name="categoryname" th:field="${category.categoryname}"/><br/>
 <input type="submit" value="提交">
</form>
</body>
</html>

六、啟動(dòng)項(xiàng)目,運(yùn)行效果如下

總結(jié)

以上所述是小編給大家介紹的IDEA+maven+SpringBoot+JPA+Thymeleaf實(shí)現(xiàn)Crud及分頁,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(圖文)

    Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(圖文)

    這篇文章主要介紹了Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot集成slf4j日志配置的方法

    SpringBoot集成slf4j日志配置的方法

    本文主要介紹了SpringBoot集成slf4j日志配置的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Java實(shí)現(xiàn)int、long、Integer、Long之間的相互轉(zhuǎn)換

    Java實(shí)現(xiàn)int、long、Integer、Long之間的相互轉(zhuǎn)換

    本文主要介紹了Java實(shí)現(xiàn)int、long、Integer、Long之間的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Springboot jar包遠(yuǎn)程調(diào)試詳解

    Springboot jar包遠(yuǎn)程調(diào)試詳解

    這篇文章主要為大家詳細(xì)介紹了Springboot jar包遠(yuǎn)程調(diào)試,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 關(guān)于SpringBoot打包測(cè)試、生產(chǎn)環(huán)境方式

    關(guān)于SpringBoot打包測(cè)試、生產(chǎn)環(huán)境方式

    這篇文章主要介紹了關(guān)于SpringBoot打包測(cè)試、生產(chǎn)環(huán)境方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問題

    HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問題

    這篇文章主要介紹了HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • mybatis in查詢傳入String方式

    mybatis in查詢傳入String方式

    這篇文章主要介紹了mybatis in查詢傳入String方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java實(shí)現(xiàn)一致性Hash算法詳情

    Java實(shí)現(xiàn)一致性Hash算法詳情

    這篇文章主要介紹了Java實(shí)現(xiàn)一致性Hash算法詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Spring?boot詳解fastjson過濾字段為null值如何解決

    Spring?boot詳解fastjson過濾字段為null值如何解決

    這篇文章主要介紹了解決Spring?boot中fastjson過濾字段為null值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • SpringBoot Admin 如何實(shí)現(xiàn)Actuator端點(diǎn)可視化監(jiān)控

    SpringBoot Admin 如何實(shí)現(xiàn)Actuator端點(diǎn)可視化監(jiān)控

    這篇文章主要介紹了SpringBoot Admin 如何實(shí)現(xiàn)Actuator端點(diǎn)可視化監(jiān)控,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論