无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页

一、开发环境:

1、windows 7 企业版

2、IDEA 14

3、JDK 1.8

4、Maven 3.5.2

5、MariaDB

6、SQLYog

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第1张图片

二、Maven设置:

Maven目录下的conf目录下的settings.xml做如下内容的添加:

1、使用阿里云的仓库,比官网访问速度快很多

1 
2 nexus-aliyun
3 central
4 Nexus aliyun
5 http://maven.aliyun.com/nexus/content/groups/public
6 

2、全局JDK配置

复制代码

 1   
 2     
 3     jdk18    
 4         
 5         true    
 6         1.8    
 7         
 8         
 9         1.8    
10         1.8    
11         1.8    
12          
13 

复制代码

三、IDEA基本设置:

1、Maven设置:选择Maven目录,同时配置文件和本地仓库

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第2张图片

2、字符编码设置

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第3张图片

四、使用IDEA创建Maven工程:

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第4张图片

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第5张图片

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第6张图片

选择Enable Auto-Import,创建好的工程目录如下图:

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第7张图片

五、体验SpringBoot结合JPA的快速开发吧

1、pom.xml

复制代码

 1 
 2 
 5     4.0.0
 6 
 7     cn.temptation
 8     studySpringBoot
 9     1.0-SNAPSHOT
10 
11     
12     
13         org.springframework.boot
14         spring-boot-starter-parent
15         2.0.0.RELEASE
16     
17 
18     
19         
20         
21             org.springframework.boot
22             spring-boot-starter-web
23         
24         
25         
26             org.springframework.boot
27             spring-boot-starter-thymeleaf
28         
29         
30         
31             mysql
32             mysql-connector-java
33             5.1.21
34         
35         
36         
37             org.springframework.boot
38             spring-boot-starter-data-jpa
39         
40     
41 

复制代码

2、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

复制代码

3、创建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 }

复制代码

4、创建实体类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 }

复制代码

5、创建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 {
7 
8 }

复制代码

6、创建控制器类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 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 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 }

复制代码

7、resources目录下新建templates目录,创建表现层:类别列表页面(categorylist.html)、类别新增页面(categoryinit.html)、类别编辑页面(categoryedit.html)

类别列表页面(categorylist.html)

复制代码

 1 
 2 
 3 
 4     
 5     类别列表
 6     
12 
13 
14 新增
15 
16     
17         
18         
19         
20     
21     
22     
23     
24     
25         
26         
27         
31     
32 
类别编号类别名称操  作
类别编号类别名称 28 编辑   29 删除 30
33
34 [首页]   35 [上页]   36 [下页]   37 [末页] 38
39 40

复制代码

类别新增页面(categoryinit.html)

复制代码

 1 
 2 
 3 
 4     
 5     类别新增
 6 
 7 
 8 
9 10
11 12
13 14

复制代码

类别编辑页面(categoryedit.html)

复制代码

 1 
 2 
 3 
 4     
 5     类别编辑
 6 
 7 
 8 
9
10 11
12 13
14 15

复制代码

六、启动项目,运行效果如下

无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页_第8张图片

你可能感兴趣的:(微服务)