您好,登錄后才能下訂單哦!
1、windows 7 企業版
2、IDEA 14
3、JDK 1.8
4、Maven 3.5.2
5、MariaDB
6、SQLYog
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>
1、Maven設置:選擇Maven目錄,同時配置文件和本地倉庫
2、字符編碼設置
選擇Enable Auto-Import,創建好的工程目錄如下圖:
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及分頁,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。