资讯专栏INFORMATION COLUMN

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

terasum / 686人阅读

摘要:课程目标完成与与的的集成处理数据课程计划使用完成博客后台管理员列表的搜索课程分析想要完成列表的搜索,就必须对按提交搜索条件进行逻辑判断组织也就是动态步骤加入依赖使用配置使用使用注解方式动态动

课程目标

完成与spring boot 与的mybatis的集成处理数据curd

课程计划

使用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 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 getAdminList(
            Admin adminReq,
            @RequestParam(defaultValue = "1", value = "page") String page,
            @RequestParam(defaultValue = "10", value = "rows") String rows) {
        String total; //页数
        List admin_list;
        int records;
        records = adminMapper.countBySearch(adminReq);
        int pageSize = Integer.valueOf(rows);
        total = Integer.toString((records + pageSize - 1) / pageSize);
        DataResponse response = new DataResponse<>();
        admin_list = adminMapper.findBySearch(adminReq);
        response.setTotal(total);
        response.setRows(admin_list);
        response.setPage(page);
        response.setRecords(records);
        return response;
    }
课程成果

完成使用jqgrid+spring boot+mybatis 的数据列表搜索

遗留page 和 pageSize 的传参控制,下节课对代码进行稍微的改动就可以支持

目前使用了jpa的Hibernate entity 这么用合理么?

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74087.html

相关文章

  • SpringBoot 入门简介

    摘要:这里使用的是数据库启动类上加上注解在启动类中添加对包扫描扫描多个包下的可以有以下几种方法扫描会自动加载相关配置,数据源就会自动注入到中,会自动注入到中,可以直接使用。有配置文件下的使用扫描多个包下的可以有以下几种方法扫描 Spring-Boot 学习笔记 1 Spring-Boot 介绍 1.1 什么是Spring-Boot Spring-Boot是由Pivotal团队提供的全新框架...

    chuyao 评论0 收藏0
  • spring-boot 整合mybatis-plus 组成后台开发基本框架

    摘要:一直想搞一套后台基本开发框架出来,无奈太忙其实太懒,最近受到两位大佬的启发,就改动了一下大佬做好的东西。更新简单整合使用项目目录修复修改模板文件的包名问题,之后只在包里文件中的与即可地址 一直想搞一套后台基本开发框架出来,无奈太忙(其实太懒),最近受到两位大佬的启发,就改动了一下大佬做好的东西。初始版本:https://github.com/lihengming... showImg(...

    abson 评论0 收藏0
  • String-boot + mybatis +pagehelper 使用分页

    摘要:最近自己搭建一个的项目听说最近很是流行之前在一件公司用的觉得挺不错所以自己配置一下学习学习结果做到分页的时候看到了貌似是个很牛的插件所以用来学习一下结果两天都没怎么弄明白网上查了好多资料但好像没有看到代码很全的让我这个小白彻底看懂的自己愚 最近自己搭建一个spring-boot 的项目听说最近很是流行,之前在一件公司用的觉得挺不错.所以自己配置一下学习学习.结果做到分页的时候看到了pa...

    Drinkey 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<