1. Windows 7 Enterprise Edition
2. IDEA 14
3. JDK 1.8
4. Maven 3.5.2
5. MariaDB
6. SQLYog
Add settings.xml in the conf directory in the Maven directory to the following content:
1. Using Alibaba Cloud's repository is much faster than the official website.
<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public</url></mirror>
2. Global JDK configuration
<!-- Global jdk configuration, 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> </properties> </properties> </profile>
1. Maven settings: select the Maven directory, and configure the file and local repository at the same time.
2. Character encoding settings
Select Enable Auto-Import and create the project directory as shown below:
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> <!-- Default settings for using 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. Create new application.properties in the resources directory (of course, if you like to use yaml, you can use yaml)
# Database connection spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testspring.datasource.username=rootspring.datasource.password=saspring.datasource.driver-class-name=com.mysql.jdbc.Driver# JPA configuration spring.jpa.properties.hibernate.hbm2ddl.auto=update
3. Create SpringBoot program startup class SpringbootApplication.java
package cn.temptation;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootApplication { public static void main(String[] args) { // SpringBoot project starts SpringApplication.run(SpringbootApplication.class, args); }}4. Create entity class Category.java
package cn.temptation.model;import javax.persistence.*;// library building table//DROP TABLE category;////CREATE TABLE category//(// categoryid INT AUTO_INCREMENT PRIMARY KEY,// categoryname VARCHAR(10) NOT NULL//);////INSERT INTO category VALUES(NULL, 'mobile'), (NULL, 'book'), (NULL, 'clothing'), (NULL, 'shoes');/////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. Create DAO interface 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. Create the controller class 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;@Controllerpublic class CategoryController { @Autowired private CategoryDao categoryDao; /** * Unpaging query* * @return */// @RequestMapping("/categorylist")// public ModelAndView categorylist() {// List<Category> list = categoryDao.findAll();//// ModelAndView mav = new ModelAndView("categorylist");// mav.addObject("list", list);// return mav;// } /** * Pagination query* * @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; } /** * New view in category* @return */ @RequestMapping("/categoryinit") public String categoryinit() { return "categoryinit"; } /** * New operation in category* @param model * @return */ @RequestMapping("/categoryinsert") public String categoryinsert(Category model) { categoryDao.save(model); return "redirect:categorylist"; } /** * Category delete operation* @param categoryid * @return */ @RequestMapping("/categorydelete") public String categorydelete(Integer categoryid) { categoryDao.deleteById(categoryid); return "redirect:categorylist"; } /** * Category editing view* @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; } /** * Category editing operation* @param model * @return */ @RequestMapping("/categoryupdate") public String categoryupdate(Category model) { categoryDao.save(model); return "redirect:categorylist"; }}7. Create a new templates directory under the resources directory and create a presentation layer: category list page (categorylist.html), category new page (categoryinit.html), category edit page (categoryedit.html)
Categorylist page (categorylist.html)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Category List</title> <style> table, th, td { border: 1px solid green; border-collapse: collapse; } </style></head><body><a th:href="@{/categoryinit}">Add </a><table> <tr> <th>Category Number</th> <th>Category Name</th> <th>Operation</th> </tr> <!--No page traversal--> <!--<tr th:each="item : ${list}">--> <!--Page traversal--> <tr th:each="item : ${page.content}"> <td th:text="${item.categoryid}">Category number</td> <td th:text="${item.categoryname}">Category name</td> <td> <a th:href="@{/categoryedit(categoryid=${item.categoryid})}">Edit</a> <a th:href="@{/categorydelete(categoryid=${item.categoryid})}">Delete</a> </td> </td> </tr></table><div> <a th:href="@{/categorylist(start=0)}">[Home]</a> <a th:if="${not page.isFirst()}" th:href="@{/categorylist(start=${page.totalPage-1})}">[Previous Page]</a> <a th:if="${not page.isLast()}" th:href="@{/categorylist(start=${page.number+1})}">[Next Page]</a> <a th:href="@{/categorylist(start=${page.totalPages-1})}">[Last Page]</a></div></body></html>New page for category (categoryinit.html)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>New category</title></head><body><form action="categoryinsert" method="post"> <label for="txtCategoryname">Category name:</label> <input type="text" id="txtCategoryname" name="categoryname" /><br/> <input type="submit" value="submit"></form></body></html>
Category Edit Page (categoryedit.html)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Category Edit</title></head><body><form action="categoryupdate" method="post"> <input type="hidden" id="txtCategoryid" name="categoryid" th:field="${category.categoryid}"/><br/> <label for="txtCategoryname">Category Name:</label> <input type="text" id="txtCategoryname" name="categoryname" th:field="${category.categoryname}"/><br/> <input type="submit" value="submit"></form></body></html>Summarize
The above is the IDEA+maven+SpringBoot+JPA+Thymeleaf implementation of Crud and pagination introduced to you by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!