-
User list
-
User increase
-
Information modification
-
Information view
-
User search
-
paging
ps: the page engine uses the thmeleaf engine recommended by Spring boot
-
Using IDEA to build Spring Initializr
-
Template selection: web, Thymeleaf, MyBatis, MySQL (Baidu does not understand the above steps)
-
Under src, create the following package name and directory as follows:
src
main
java
controller
mapper
pojo
tools
resources
templates
-
Modify application.properties in the resources directory
#thymeleaf configuration spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html #The cache is set to false, so it will take effect immediately after modification, which is convenient for debugging spring.thymeleaf.cache=false #Database configuration spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zx?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=root # Mysql8.0 please change to com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver
-
Add the following configuration to pom.xml
<!-- servlet rely on. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat Support.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- pageHelper Used for paging --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
-
Add entity class in pojo
public class Users { private int id; private String mobName; private String userName; ... }
-
Add annotation mode database operation interface in mapper
@Mapper public interface UsersMapper { @Select("select * from users where CONCAT(mobName,userName,nickname,phone) like CONCAT('%',#{name},'%')") List<Users> getUsersList(String name); @Insert("insert into users (id,mobName,userName,nickname,phone,register) " + "values (#{id},#{mobName},#{userName},#{nickname},#{phone},#{register})") public int add(Users users); @Delete(" delete from users where id= #{id} ") public void del(int id); @Select("select * from users where id= #{id} ") public Users getUser(int id); @Update("update users set " + "mobName = #{mobName}," + "userName = #{userName}," + "nickname = #{nickname}," + "phone = #{phone}," + "register = #{register} " + "where id=#{id} ") public int update(Users users); }
-
Add paging tool class in tools
import com.github.pagehelper.PageHelper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration public class PageHelperConfig { @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum", "true"); properties.setProperty("rowBoundsWithCount", "true"); properties.setProperty("reasonable", "true"); pageHelper.setProperties(properties); return pageHelper; } }
@Controller public class UserController { @Resource private UsersMapper usersMapper; //Add page @RequestMapping("add") public String add() { return "add"; } //Find (for query) @RequestMapping("getUser") public String getUser(int id, Model model) throws Exception { Users Users = usersMapper.getUser(id); model.addAttribute("Users", Users); return "userShow"; } ... }
PageHelper.startPage(int PageNum,int PageSize): used to set the position of the page and the number of displayed data entries;
PageInfo pageInfo = new PageInfo(list); PageInfo is used to encapsulate page information and return it to the foreground interface
Some common parameters in pageHelper:
PageInfo.list | Result set |
---|---|
PageInfo.pageNum | Current page number |
PageInfo.pageSize | Data entry displayed on the current page |
PageInfo.pages | PageCount |
PageInfo.total | Total entries of data |
PageInfo.prePage | Previous page |
PageInfo.nextPage | next page |
PageInfo.isFirstPage | Is it the first page |
PageInfo.isLastPage | Last page or not |
PageInfo.hasPreviousPage | Is there a previous page |
PageHelper.hasNextPage | Is there a next page |