Lesson 3 (spring-boot+mybatis+jqgrid)

Keywords: Java Spring Mybatis SQL MySQL

Curriculum objectives

  1. Complete data curd integration with spring boot and mybatis

Curriculum plan

  1. Use mybatis to complete jqgird search of blog administrators list

Curriculum analysis

In order to complete the list search, SQL must be logically judged and organized according to the submission search conditions, that is, dynamic sql.

step

1. Adding dependencies

// build.gradle
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")
    // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    // Use MySQL Connector-J
    compile 'mysql:mysql-connector-java'
    // Use mybatis
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testCompile("junit:junit")
}

2. Configure mybatis

spring:
  jpa:
    hibernate:
      ddl-auto: update
  datasource:
    url: jdbc:mysql://localhost:3306/db_sptest?useSSL=false
    username: mysqluser
    password: mysqlpwd
  mvc:
    static-path-pattern: /static/**
mybatis:
  type-aliases-package: hello.model

3. Use mybatis mapper

// model/AdminMapper
@Mapper
public interface AdminMapper {
    //Use annotations
    @Select("SELECT * FROM ADMIN WHERE name = #{name} LIMIT 1")
    Admin findByName(String name);

    @Select("SELECT * FROM ADMIN WHERE id = #{id}")
    Admin findById(Integer id);

    //dynamic sql
    @SelectProvider(type = AdminService.class,method = "selectAdminLike")
    List<Admin> findBySearch(Admin admin);

    //Dynamic sql returns rows
    @SelectProvider(type = AdminService.class,method = "countAdminSearch")
    @ResultType(Integer.class)
    int countBySearch(Admin admin);

}

4. Writing dynamic sql statements

// service/AdminService
import org.apache.ibatis.jdbc.SQL;

public class AdminService {

    // With conditionals (note the final parameters, required for the anonymous inner class to access them)
    public String selectAdminLike(Admin admin) {
        return new SQL() {{
            SELECT("A.name,A.email,A.id");
            FROM("ADMIN A");
            if (admin.getName() != null) {
                WHERE("A.name = '" + admin.getName() + "'");
            }
            if (admin.getEmail() != null) {
                WHERE("A.email = " + admin.getEmail());
            }
        }}.toString();
    }

    public String countAdminSearch(Admin admin){
        return new SQL() {{
            SELECT("count(*)");
            FROM("ADMIN A");
            if (admin.getName() != null) {
                WHERE("A.name = '" + admin.getName() + "'");
            }
            if (admin.getEmail() != null) {
                WHERE("A.email = " + admin.getEmail());
            }
        }}.toString();
    }
}

5. Using mybatis query method

    // AdminController
    @GetMapping(path = "get_list")
    @ResponseBody
    public DataResponse<Admin> getAdminList(
            Admin adminReq,
            @RequestParam(defaultValue = "1", value = "page") String page,
            @RequestParam(defaultValue = "10", value = "rows") String rows) {
        String total; //The number of pages
        List<Admin> admin_list;
        int records;
        records = adminMapper.countBySearch(adminReq);
        int pageSize = Integer.valueOf(rows);
        total = Integer.toString((records + pageSize - 1) / pageSize);
        DataResponse<Admin> response = new DataResponse<>();
        admin_list = adminMapper.findBySearch(adminReq);
        response.setTotal(total);
        response.setRows(admin_list);
        response.setPage(page);
        response.setRecords(records);
        return response;
    }

Curriculum achievement

  1. Complete data list search using jqgrid+spring boot+mybatis
  2. Legacy page and pageSize parameter control, the next lesson on the code slightly changed to support
  3. Is it reasonable to use Hibernate entity, which currently uses jpa?

Posted by Froolah on Sun, 07 Apr 2019 17:51:30 -0700