zoukankan      html  css  js  c++  java
  • Spring Boot 集成 MyBatis, 分页插件 PageHelper, 通用 Mapper

    Spring Boot 集成 MyBatis, 分页插件 PageHelper, 通用 Mapper

    配置

    • pom依赖
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
    <!--mapper-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>1.2.4</version>
    </dependency>
    <!--pagehelper-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>
    • application配置
    #mybatis
    mybatis.type-aliases-package=tk.mybatis.springboot.model
    mybatis.mapper-locations=classpath:mapper/*.xml
    
    #mapper
    #mappers 多个接口时逗号隔开
    mapper.mappers=tk.mybatis.springboot.util.MyMapper
    mapper.not-empty=false
    mapper.identity=MYSQL
    
    #pagehelper
    pagehelper.helperDialect=mysql
    pagehelper.reasonable=true
    pagehelper.supportMethodsArguments=true
    pagehelper.params=count=countSql
    • application.yml 配置
    mybatis:
        type-aliases-package: tk.mybatis.springboot.model
        mapper-locations: classpath:mapper/*.xml
    
    mapper:
        mappers:
            - tk.mybatis.springboot.util.MyMapper
        not-empty: false
        identity: MYSQL
    
    pagehelper:
        helperDialect: mysql
        reasonable: true
        supportMethodsArguments: true
        params: count=countSql
    • 创建一个通用接口
    //接口名随意
    public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
        //TODO
        //FIXME 特别注意,该接口不能被扫描到,否则会出错
    }
    • mapper层配置

      注意:entity与数据表必须对应,否则会报错

    @Mapper
    public interface UserMapper extends MyMapper<User> {//实现自定义的接口
    }
    • entity层配置
    1. entity类必须与数据库表同名或与驼峰形式对应;
    2. entity参数必须与数据表字段对应;
    3. 需要在entity上添加属性时,不能修改原始entity,需要重新创建一个entity继承原entity;增删改功能可以直接使用新entity,查询时返回的数据表对应的entity类型必须是原entity类型,若想转为新的entity,需要自己转换比如
    List<子> tab2s = JSONArray.parseArray(JSON.toJSONString(tab1s), 子.class);

    注意:当entity中添加其他参数时,调用增删改查方法都会异常,因为增删改查是以entity为基础的;在mapper层继承MyMapper时填入了entity,entity变化会引起对应SQL语句变化,会导致调用数据库异常.

    调用

    • 基本增删改查

    1547712353544

    1547712369629

    1547712384613

    1547712397040

    • 复杂查询
    Example example=new Example(User.class);
    example.setOrderByClause("id DESC");
    Example.Criteria criteria=example.createCriteria();
    criteria.andIsNotNull("id");
    userMapper.selectByExample(example);




  • 相关阅读:
    SVN使用教程总结
    学习duilib库过程中的笔记
    duilib库使用(一)-- 编译生成依赖库
    在Windows服务进程中启动需管理员权限(带盾牌图标)的应用程序
    如何在Windows服务程序中读写HKEY_CURRENT_USER注册表
    vs2015 编译boost库
    NSIS 打包工具使用
    C 读文件
    常用的字符转化的方法
    C# 中对于json的解析小结
  • 原文地址:https://www.cnblogs.com/ziyue7575/p/60f124ff97528ba51a74009649823d9a.html
Copyright © 2011-2022 走看看