【原】無腦操做:IDEA + maven + SpringBoot + JPA + Thymeleaf實現CRUD及分頁

1、開發環境:html

一、windows 7 企業版java

二、IDEA 14mysql

三、JDK 1.8web

四、Maven 3.5.2spring

五、MariaDBsql

六、SQLYog數據庫

2、Maven設置:apache

Maven目錄下的conf目錄下的settings.xml作以下內容的添加:windows

一、使用阿里雲的倉庫,比官網訪問速度快不少app

1 <mirror>
2 <id>nexus-aliyun</id>
3 <mirrorOf>central</mirrorOf>
4 <name>Nexus aliyun</name>
5 <url>http://maven.aliyun.com/nexus/content/groups/public</url>
6 </mirror>

二、全局JDK配置

 1 <!-- 全局jdk配置,settings.xml -->  
 2 <profile>    
 3     <id>jdk18</id>    
 4     <activation>    
 5         <activeByDefault>true</activeByDefault>    
 6         <jdk>1.8</jdk>    
 7     </activation>    
 8     <properties>    
 9         <maven.compiler.source>1.8</maven.compiler.source>    
10         <maven.compiler.target>1.8</maven.compiler.target>    
11         <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
12     </properties>     
13 </profile>

3、IDEA基本設置:

一、Maven設置:選擇Maven目錄,同時配置文件和本地倉庫

二、字符編碼設置

4、使用IDEA建立Maven工程:

選擇Enable Auto-Import,建立好的工程目錄以下圖:

5、體驗SpringBoot結合JPA的快速開發吧

一、pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>cn.temptation</groupId>
 8     <artifactId>studySpringBoot</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <!-- 使用spring boot的默認設置 -->
12     <parent>
13         <groupId>org.springframework.boot</groupId>
14         <artifactId>spring-boot-starter-parent</artifactId>
15         <version>2.0.0.RELEASE</version>
16     </parent>
17 
18     <dependencies>
19         <!-- web -->
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-web</artifactId>
23         </dependency>
24         <!-- thymeleaf -->
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-thymeleaf</artifactId>
28         </dependency>
29         <!-- mysql-->
30         <dependency>
31             <groupId>mysql</groupId>
32             <artifactId>mysql-connector-java</artifactId>
33             <version>5.1.21</version>
34         </dependency>
35         <!-- jpa-->
36         <dependency>
37             <groupId>org.springframework.boot</groupId>
38             <artifactId>spring-boot-starter-data-jpa</artifactId>
39         </dependency>
40     </dependencies>
41 </project>

二、resources目錄下新建application.properties(固然喜歡用yaml的能夠用yaml)

1 # 數據庫鏈接 2 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test 3 spring.datasource.username=root 4 spring.datasource.password=sa 5 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 6 
7 # JPA配置 8 spring.jpa.properties.hibernate.hbm2ddl.auto=update

三、建立SpringBoot程序啓動類SpringbootApplication.java

 1 package cn.temptation;  2 
 3 import org.springframework.boot.SpringApplication;  4 import org.springframework.boot.autoconfigure.SpringBootApplication;  5 
 6 @SpringBootApplication  7 public class SpringbootApplication {  8     public static void main(String[] args) {  9         // SpringBoot項目啓動
10         SpringApplication.run(SpringbootApplication.class, args); 11  } 12 }

四、建立實體類Category.java

 1 package cn.temptation.model;  2 
 3 import javax.persistence.*;  4 
 5 // 建庫建表  6 //DROP TABLE category;  7 //
 8 //CREATE TABLE category  9 //( 10 // categoryid INT AUTO_INCREMENT PRIMARY KEY, 11 // categoryname VARCHAR(10) NOT NULL 12 //); 13 //
14 //INSERT INTO category VALUES(NULL, '手機'), (NULL, '圖書'), (NULL, '服裝'), (NULL, '鞋帽'); 15 //
16 //SELECT * FROM category;
17 @Entity 18 @Table(name = "category") 19 public class Category { 20  @Id 21     @GeneratedValue(strategy = GenerationType.IDENTITY) 22     @Column(name = "categoryid") 23     private Integer categoryid; 24 
25     @Column(name = "categoryname") 26     private String categoryname; 27 
28     public Integer getCategoryid() { 29         return categoryid; 30  } 31 
32     public void setCategoryid(Integer categoryid) { 33         this.categoryid = categoryid; 34  } 35 
36     public String getCategoryname() { 37         return categoryname; 38  } 39 
40     public void setCategoryname(String categoryname) { 41         this.categoryname = categoryname; 42  } 43 }

五、建立DAO接口CategoryDao.java

1 package cn.temptation.dao; 2 
3 import cn.temptation.model.Category; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 
6 public interface CategoryDao extends JpaRepository<Category, Integer> { 7 
8 }

六、建立控制器類CategoryController.java

 1 package cn.temptation.web;  2 
 3 import cn.temptation.dao.CategoryDao;  4 import cn.temptation.model.Category;  5 import org.springframework.beans.factory.annotation.Autowired;  6 import org.springframework.data.domain.Page;  7 import org.springframework.data.domain.PageRequest;  8 import org.springframework.data.domain.Pageable;  9 import org.springframework.data.domain.Sort;  10 import org.springframework.stereotype.Controller;  11 import org.springframework.web.bind.annotation.RequestMapping;  12 import org.springframework.web.bind.annotation.RequestParam;  13 import org.springframework.web.servlet.ModelAndView;  14 
 15 import java.util.List;  16 
 17 @Controller  18 public class CategoryController {  19  @Autowired  20     private CategoryDao categoryDao;  21 
 22     /**
 23  * 不分頁查詢  24  *  25  * @return
 26      */
 27 // @RequestMapping("/categorylist")  28 // public ModelAndView categorylist() {  29 // List<Category> list = categoryDao.findAll();  30 //
 31 // ModelAndView mav = new ModelAndView("categorylist");  32 // mav.addObject("list", list);  33 // return mav;  34 // }
 35 
 36     /**
 37  * 分頁查詢  38  *  39  * @return
 40      */
 41     @RequestMapping("/categorylist")  42     public ModelAndView categorylist(@RequestParam(value = "start", defaultValue = "0") Integer start,  43                                      @RequestParam(value = "limit", defaultValue = "2") Integer limit) {  44         start = start < 0 ? 0 : start;  45 
 46         Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "categoryid");  47         Pageable pageable = new PageRequest(start, limit, sort);  48         Page<Category> page = categoryDao.findAll(pageable);  49 
 50 // System.out.println(page.getNumber());  51 // System.out.println(page.getNumberOfElements());  52 // System.out.println(page.getSize());  53 // System.out.println(page.getTotalElements());  54 // System.out.println(page.getTotalPages());  55 // System.out.println(page.isFirst());  56 // System.out.println(page.isLast());
 57 
 58         ModelAndView mav = new ModelAndView("categorylist");  59         mav.addObject("page", page);  60         return mav;  61  }  62 
 63     /**
 64  * 類別新增視圖  65  * @return
 66      */
 67     @RequestMapping("/categoryinit")  68     public String categoryinit() {  69         return "categoryinit";  70  }  71 
 72     /**
 73  * 類別新增操做  74  * @param model  75  * @return
 76      */
 77     @RequestMapping("/categoryinsert")  78     public String categoryinsert(Category model) {  79  categoryDao.save(model);  80         return "redirect:categorylist";  81  }  82 
 83     /**
 84  * 類別刪除操做  85  * @param categoryid  86  * @return
 87      */
 88     @RequestMapping("/categorydelete")  89     public String categorydelete(Integer categoryid) {  90  categoryDao.deleteById(categoryid);  91         return "redirect:categorylist";  92  }  93 
 94     /**
 95  * 類別編輯視圖  96  * @param categoryid  97  * @return
 98      */
 99     @RequestMapping("/categoryedit") 100     public ModelAndView categoryedit(Integer categoryid) { 101         Category model = categoryDao.getOne(categoryid); 102 
103         ModelAndView mav = new ModelAndView("categoryedit"); 104         mav.addObject("category", model); 105         return mav; 106  } 107 
108     /**
109  * 類別編輯操做 110  * @param model 111  * @return
112      */
113     @RequestMapping("/categoryupdate") 114     public String categoryupdate(Category model) { 115  categoryDao.save(model); 116         return "redirect:categorylist"; 117  } 118 }

七、resources目錄下新建templates目錄,建立表現層:類別列表頁面(categorylist.html)、類別新增頁面(categoryinit.html)、類別編輯頁面(categoryedit.html)

類別列表頁面(categorylist.html)

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>類別列表</title>
 6     <style>
 7  table, th, td {
 8  border: 1px solid green;
 9  border-collapse: collapse;
10         }
11     </style>
12 </head>
13 <body>
14 <a th:href="@{/categoryinit}">新增</a>
15 <table>
16     <tr>
17         <th>類別編號</th>
18         <th>類別名稱</th>
19         <th>&nbsp;&nbsp;</th>
20     </tr>
21     <!--不分頁遍歷-->
22     <!--<tr th:each="item : ${list}">-->
23     <!--分頁遍歷-->
24     <tr th:each="item : ${page.content}">
25         <td th:text="${item.categoryid}">類別編號</td>
26         <td th:text="${item.categoryname}">類別名稱</td>
27         <td>
28             <a th:href="@{/categoryedit(categoryid=${item.categoryid})}">編輯</a>&nbsp;&nbsp;
29             <a th:href="@{/categorydelete(categoryid=${item.categoryid})}">刪除</a>
30         </td>
31     </tr>
32 </table>
33 <div>
34     <a th:href="@{/categorylist(start=0)}">[首頁]</a>&nbsp;&nbsp;
35     <a th:if="${not page.isFirst()}" th:href="@{/categorylist(start=${page.number-1})}">[上頁]</a>&nbsp;&nbsp;
36     <a th:if="${not page.isLast()}" th:href="@{/categorylist(start=${page.number+1})}">[下頁]</a>&nbsp;&nbsp;
37     <a th:href="@{/categorylist(start=${page.totalPages-1})}">[末頁]</a>
38 </div>
39 </body>
40 </html>

類別新增頁面(categoryinit.html)

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>類別新增</title>
 6 </head>
 7 <body>
 8 <form action="categoryinsert" method="post">
 9     <label for="txtCategoryname">類別名稱:</label>
10     <input type="text" id="txtCategoryname" name="categoryname" /><br/>
11     <input type="submit" value="提交">
12 </form>
13 </body>
14 </html>

類別編輯頁面(categoryedit.html)

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>類別編輯</title>
 6 </head>
 7 <body>
 8 <form action="categoryupdate" method="post">
 9     <input type="hidden" id="txtCategoryid" name="categoryid" th:field="${category.categoryid}"/><br/>
10     <label for="txtCategoryname">類別名稱:</label>
11     <input type="text" id="txtCategoryname" name="categoryname" th:field="${category.categoryname}"/><br/>
12     <input type="submit" value="提交">
13 </form>
14 </body>
15 </html>

6、啓動項目,運行效果以下

相關文章
相關標籤/搜索