IDEA+maven+SpringBoot+JPA+Thymeleaf实现Crud及分页
程序员文章站
2022-03-25 15:01:55
一、开发环境:
1、windows 7 企业版
2、idea 14
3、jdk 1.8
4、maven 3.5.2
5、mariadb
6、sqlyog...
一、开发环境:
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、使用阿里云的仓库,比官网访问速度快很多
<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基本设置:
1、maven设置:选择maven目录,同时配置文件和本地仓库
2、字符编码设置
四、使用idea创建maven工程:
选择enable auto-import,创建好的工程目录如下图:
五、体验springboot结合jpa的快速开发吧
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的默认设置 --> <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(当然喜欢用yaml的可以用yaml)
# 数据库连接 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、创建springboot程序启动类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项目启动 springapplication.run(springbootapplication.class, args); } }
4、创建实体类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, '手机'), (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、创建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、创建控制器类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目录,创建表现层:类别列表页面(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>类别编号</th> <th>类别名称</th> <th>操 作</th> </tr> <!--不分页遍历--> <!--<tr th:each="item : ${list}">--> <!--分页遍历--> <tr th:each="item : ${page.content}"> <td th:text="${item.categoryid}">类别编号</td> <td th:text="${item.categoryname}">类别名称</td> <td> <a th:href="@{/categoryedit(categoryid=${item.categoryid})}">编辑</a> <a th:href="@{/categorydelete(categoryid=${item.categoryid})}">删除</a> </td> </tr> </table> <div> <a th:href="@{/categorylist(start=0)}">[首页]</a> <a th:if="${not page.isfirst()}" th:href="@{/categorylist(start=${page.number-1})}">[上页]</a> <a th:if="${not page.islast()}" th:href="@{/categorylist(start=${page.number+1})}">[下页]</a> <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>
六、启动项目,运行效果如下
总结
以上所述是小编给大家介绍的idea+maven+springboot+jpa+thymeleaf实现crud及分页,希望对大家有所帮助