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目錄,同時配置文件和本地倉庫

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程序啟動類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)目啟動
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>類別編號</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>
六、啟動項(xiàng)目,運(yùn)行效果如下

總結(jié)
以上所述是小編給大家介紹的IDEA+maven+SpringBoot+JPA+Thymeleaf實(shí)現(xiàn)Crud及分頁,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java中Arrays.asList()方法詳解及實(shí)例
這篇文章主要介紹了Java中Arrays.asList()方法將數(shù)組作為列表時的一些差異的相關(guān)資料,需要的朋友可以參考下2017-06-06
springboot如何開啟和關(guān)閉kafka消費(fèi)
在Kafka消費(fèi)者中,通過關(guān)閉自動消費(fèi)配置,使用自定義容器工廠,并在消費(fèi)監(jiān)聽器上設(shè)置id,可以手動控制消費(fèi)的開啟和關(guān)閉,這是根據(jù)個人經(jīng)驗(yàn)總結(jié)的方法,旨在幫助其他開發(fā)者2024-12-12
SpringBoot整合Redis實(shí)現(xiàn)附近位置查找(LBS)功能
Redis 提供了 GEO 數(shù)據(jù)結(jié)構(gòu),可以高效地存儲和查詢地理位置數(shù)據(jù),本文將介紹如何使用 Spring Boot + Redis 來實(shí)現(xiàn)附近位置查找,需要的可以了解下2025-03-03
java實(shí)戰(zhàn)CPU占用過高問題的排查及解決
這篇文章給大家分享了java實(shí)戰(zhàn)CPU占用過高問題的排查及解決方法,有需要的朋友們可以學(xué)習(xí)下。2018-08-08
SpringBoot集成Swagger構(gòu)建api文檔的操作
這篇文章主要介紹了SpringBoot集成Swagger構(gòu)建api文檔的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
關(guān)于eclipse安裝spring插件報(bào)錯An error occurred while collecting item
這篇文章主要介紹了關(guān)于eclipse安裝spring插件報(bào)錯An error occurred while collecting items to be installed...解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
spring boot設(shè)置過濾器、監(jiān)聽器及攔截器的方法
這篇文章主要給大家介紹了關(guān)于spring boot設(shè)置過濾器、監(jiān)聽器及攔截器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

