spring boot 集成mybatis 通用mapper与分页插件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26138699/article/details/79709427

之前开发的时候一直使用mybatis代码生成器生成单表crud,一旦数据库表字段出现修改就要重新生成一次xml或者手动修复,容易出现错误以及麻烦。新项目就换了个方式代码生成器只生成xml以及集成映射,crud改用通用mapper并使用pagehelper做分页。

yml

mybatis:
    mapper-locations: classpath:com/api/web/**/mapper/*.xml
    configuration:
        callSettersOnNulls: true
        defaultStatementTimeout: 180
mapper:
    mappers: com.api.util.mapper.MyMapper
    not-empty: false
    identity: ORACLE

pagehelper:
    helperDialect: oracle
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

pom

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <version>1.1.3</version>
</dependency>

<dependency>
  <groupId>tk.mybatis</groupId>
  <artifactId>mapper-spring-boot-starter</artifactId>
  <version>1.1.4</version>
</dependency>

代码



public class BaseEntity {
    @Id
    @Column(name = "Id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Transient
    private Integer page = 1;

    @Transient
    private Integer rows = 10;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }
}
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
    
}

这里注意,MyMapper<T>不能被springboot扫描到。

entity   


Table指定数据库表面 @id主键 属性名默认就是开启驼峰的

@Data

@Table(name ="TB_KS_PUNISH_LOG")
public class PunishLog {
  @Id
  private String plushId;

  private String areaCode;

  private String netbarWacode;
}

dao

简单继承MyMapper类即可

/**
 * @author lcc
 */
public interface PunishLogMapper1 extends MyMapper<PunishLog> {}

service 

分页插件非常方便,拦截mybatis sql 执行前加入分页的sql

PageHelper.startPage(page, rows);
Example example = new Example(PunishLog.class);
example.createCriteria().orEqualTo("netbarWacode", punishLogAttach.getNetbarWacode());



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页