查看: 851|回复: 0

[Java学习] IDEA+maven+SpringBoot+JPA+Thymeleaf实现Crud及分页

发表于 2018-4-15 08:00:01

一、开发环境:

1、windows 7 企业版

2、IDEA 14

3、JDK 1.8

4、Maven 3.5.2

5、MariaDB

6、SQLYog

二、Maven设置:

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

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

  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>
复制代码

2、全局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>
复制代码

三、IDEA基本设置:

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

2、字符编码设置

四、使用IDEA创建Maven工程:

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

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

1、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. <groupId>cn.temptation</groupId>
  7. <artifactId>studySpringBoot</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!-- 使用spring boot的默认设置 -->
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.0.0.RELEASE</version>
  14. </parent>
  15. <dependencies>
  16. <!-- web -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <!-- thymeleaf -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  25. </dependency>
  26. <!-- mysql-->
  27. <dependency>
  28. <groupId>mysql</groupId>
  29. <artifactId>mysql-connector-java</artifactId>
  30. <version>5.1.21</version>
  31. </dependency>
  32. <!-- jpa-->
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-data-jpa</artifactId>
  36. </dependency>
  37. </dependencies>
  38. </project>
复制代码

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. # JPA配置
  7. spring.jpa.properties.hibernate.hbm2ddl.auto=update
复制代码

3、创建SpringBoot程序启动类SpringbootApplication.java

  1. package cn.temptation;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class SpringbootApplication {
  6. public static void main(String[] args) {
  7. // SpringBoot项目启动
  8. SpringApplication.run(SpringbootApplication.class, args);
  9. }
  10. }
复制代码

4、创建实体类Category.java

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

5、创建DAO接口CategoryDao.java

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

6、创建控制器类CategoryController.java

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

7、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>操 作</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>
  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>
  35. <a th:if="${not page.isFirst()}" th:href="@{/categorylist(start=${page.number-1})}">[上页]</a>
  36. <a th:if="${not page.isLast()}" th:href="@{/categorylist(start=${page.number+1})}">[下页]</a>
  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>
复制代码

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

总结

以上所述是小编给大家介绍的IDEA+maven+SpringBoot+JPA+Thymeleaf实现Crud及分页,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!



回复

使用道具 举报