Spring boot implements simple addition, deletion, modification and query as well as conditional paging

Keywords: Spring Thymeleaf MySQL JDBC

  • condition

  • Use Spring boot, mybatis and mysql to realize the following functions

  1. User list
  2. User increase
  3. Information modification
  4. Information view
  5. User search
  6. paging

    ps: the page engine uses the thmeleaf engine recommended by Spring boot

  • The renderings are as follows

  • step

  1. Using IDEA to build Spring Initializr
  2. Template selection: web, Thymeleaf, MyBatis, MySQL (Baidu does not understand the above steps)
  3. Under src, create the following package name and directory as follows:

    src

    main

    java

    com.xxx.xxx

    controller

    mapper

    pojo

    tools

    resources

    templates

  4. 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
    
  5. 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>
    
  6. Add entity class in pojo
    public class Users {
        private int id;
        private String mobName;
        private String userName;
        ...
    }
    
  7. 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);
    
    }
    
  8. 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;
    }
}
  1. Add controller
@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";
    }
    
    ...
}
  1. Add. html page in templates
  • See the specific code

Source code

  • PageHelper details

Paging tool address

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

Posted by Kainproductions on Mon, 04 Nov 2019 09:11:38 -0800