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

栏目: jQuery · 发布时间: 4年前

内容简介:想要完成列表的搜索,就必须对sql按提交搜索条件进行逻辑判断组织sql,也就是动态sql1.加入依赖

课程目标

  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")
}
  1. 配置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
  1. 使用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);

}
// 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();
    }
}
// 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;
    }

课程成果

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

  1. 完成使用jqgrid+spring boot+mybatis 的数据列表搜索
  2. 遗留page 和 pageSize 的传参控制,下节课对代码进行稍微的改动就可以支持
  3. 目前使用了jpa的Hibernate entity 这么用合理么?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

人人都是产品经理——写给产品新人

人人都是产品经理——写给产品新人

苏杰 / 电子工业出版社 / 2017-6 / 66.60

《人人都是产品经理——写给产品新人》为经典畅销书《人人都是产品经理》的内容升级版本,和《人人都是产品经理2.0——写给泛产品经理》相当于上下册的关系。对于大量成长起来的优秀互联网产品经理、众多想投身产品工作的其他岗位从业者,以及更多有志从事这一职业的学生而言,这《人人都是产品经理——写给产品新人》曾是他们记忆深刻的启蒙读物、思想基石和行动手册。作者以分享经历与体会为出发点,以“朋友间聊聊如何做产品......一起来看看 《人人都是产品经理——写给产品新人》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具