第三課(spring-boot+mybatis+jqgrid)

課程目標

  1. 完成與spring boot 與的mybatis的集成處理數據curd

課程計劃

  1. 使用mybatis完成博客後臺管理員列表的jqgird搜索

課程分析

想要完成列表的搜索,就必須對sql按提交搜索條件進行邏輯判斷組織sql,也就是動態sql

步驟

1.加入依賴

// 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'
    // 使用mybatis
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testCompile("junit:junit")
}

2. 配置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. 使用mybatis mapper

// model/AdminMapper
@Mapper
public interface AdminMapper {
    //使用註解方式
    @Select("SELECT * FROM ADMIN WHERE name = #{name} LIMIT 1")
    Admin findByName(String name);

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

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

    //動態sql 返回行數
    @SelectProvider(type = AdminService.class,method = "countAdminSearch")
    @ResultType(Integer.class)
    int countBySearch(Admin admin);

}

4.編寫動態sql語句

// 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.使用mybatis查詢方法

    // 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; //頁數
        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;
    }

課程成果

  1. 完成使用jqgrid+spring boot+mybatis 的數據列表搜索
  2. 遺留page 和 pageSize 的傳參控制,下節課對代碼進行稍微的改動就可以支持
  3. 目前使用了jpa的Hibernate entity 這麼用合理麼?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章