通用Mapper 入门

通用Mapper 入门

通用mapper 是什么

通用 Mapper4 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及Example 相关的单表操作。通用 Mapper 是为了解决 MyBatis 使用中 90% 的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。

Spring Boot 集成通用mapper

添加如下依赖:

<dependency>
  <groupId>tk.mybatis</groupId>
  <artifactId>mapper-spring-boot-starter</artifactId>
  <version>版本号</version>
</dependency>
@tk.mybatis.spring.annotation.MapperScan(basePackages = "扫描包")
@SpringBootApplication
public class SampleMapperApplication implements CommandLineRunner {

你可以给带有 @Configuration 的类配置该注解,或者直接配置到 Spring Boot 的启动类上,如上

简单示例

数据库有如下表:

CREATE TABLE `country` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `countryname` varchar(255) DEFAULT NULL COMMENT '名称',
  `countrycode` varchar(255) DEFAULT NULL COMMENT '代码',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=10011 DEFAULT CHARSET=utf8 COMMENT='国家信息';

对应的 Java 实体类型如下:

public class Country {
    @Id
    private Integer id;
    private String  countryname;
    private String  countrycode;

    //省略 getter 和 setter
}

通用 Mapper 提供了大量的通用接口,这里以最常用的 Mapper 接口为例*

该实体类对应的数据库操作接口如下:

import tk.mybatis.mapper.common.Mapper;

public interface CountryMapper extends Mapper<Country> {
}

只要配置 MyBatis 时能注册或者扫描到该接口,该接口提供的方法就都可以使用。

该接口默认继承的方法如下:

  • selectOne
  • select
  • selectAll
  • selectCount
  • selectByPrimaryKey
  • 方法太多,省略其他…

数据库映射

1.@NameStyle 注解(Mapper)

这个注解可以在类上进行配置,优先级高于对应的 style 全局配置。

注解支持以下几个选项:

normal,                     //原值
camelhump,                  //驼峰转下划线
uppercase,                  //转换为大写
lowercase,                  //转换为小写
camelhumpAndUppercase,      //驼峰转下划线大写形式
camelhumpAndLowercase,      //驼峰转下划线小写形式

使用时,直接在类上配置即可,例如:

@NameStyle(Style.camelhumpAndUppercase)
public class Country

配置该注解后,对该类和其中的字段进行转换时,会将形如 userName 的字段转换为表中的 USER_NAME 字段。

2.@Table 注解(JPA)

@Table 注解可以配置 name,catalogschema 三个属性,配置 name 属性后,直接使用提供的表名,不再根据实体类名进行转换

3.@Column 注解(JPA)

@Column 注解支持 name, insertableupdateable 三个属性。

name 配置映射的列名。

使用maven 生成实体类,mapper接口,xml文件

此部分请自行查询网上资料,后续可能会出一篇文章来描述这个过程。

通用Mapper 使用

我们重点看下此部分,因为和大家代码编写关系比较紧密。

  1. MBG 生成的Example

    用法如下:

    CountryExample example = new CountryExample();
    example.createCriteria().andCountrynameLike("A%");
    example.or().andIdGreaterThan(100);
    example.setDistinct(true);
    int count = mapper.deleteByExample(example);
    

    对于的 SQL 日志如下:

    DEBUG [main] - ==>  Preparing: DELETE FROM country WHERE ( countryname like ? ) or ( Id > ? ) 
    DEBUG [main] - ==> Parameters: A%(String), 100(Integer)
    DEBUG [main] - <==    Updates: 95
    
  2. 通用Example

    查询

    示例:

    Example example = new Example(Country.class);
    example.setForUpdate(true);
    example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
    example.or().andLessThan("id", 41);
    List<Country> countries = mapper.selectByExample(example);
    

    日志:

    DEBUG [main] - ==>  Preparing: SELECT id,countryname,countrycode FROM country WHERE ( id > ? and id < ? ) or ( id < ? ) ORDER BY id desc FOR UPDATE 
    DEBUG [main] - ==> Parameters: 100(Integer), 151(Integer), 41(Integer)
    DEBUG [main] - <==      Total: 90
    

    动态sql

    示例:

    Example example = new Example(Country.class);
    Example.Criteria criteria = example.createCriteria();
    if(query.getCountryname() != null){
        criteria.andLike("countryname", query.getCountryname() + "%");
    }
    if(query.getId() != null){
        criteria.andGreaterThan("id", query.getId());
    }
    List<Country> countries = mapper.selectByExample(example);
    

    日志:

    DEBUG [main] - ==>  Preparing: SELECT id,countryname,countrycode FROM country WHERE ( countryname like ? ) ORDER BY id desc 
    DEBUG [main] - ==> Parameters: China%(String)
    DEBUG [main] - <==      Total: 1
    

    排序

    示例:

    Example example = new Example(Country.class);
    example.orderBy("id").desc().orderBy("countryname").orderBy("countrycode").asc();
    List<Country> countries = mapper.selectByExample(example);
    

    日志:

    DEBUG [main] - ==>  Preparing: SELECT id,countryname,countrycode FROM country order by id DESC,countryname,countrycode ASC 
    DEBUG [main] - ==> Parameters: 
    DEBUG [main] - <==      Total: 183
    

    去重

    示例:

    CountryExample example = new CountryExample();
    //设置 distinct
    example.setDistinct(true);
    example.createCriteria().andCountrynameLike("A%");
    example.or().andIdGreaterThan(100);
    List<Country> countries = mapper.selectByExample(example);
    

    日志:

    DEBUG [main] - ==>  Preparing: SELECT distinct id,countryname,countrycode FROM country WHERE ( countryname like ? ) or ( Id > ? ) ORDER BY id desc 
    DEBUG [main] - ==> Parameters: A%(String), 100(Integer)
    DEBUG [main] - <==      Total: 95
    

​ 设置查询列

​ 示例:

	Example example = new Example(Country.class);
	example.selectProperties("id", "countryname");
	List<Country> countries = mapper.selectByExample(example);

​ 日志:

	DEBUG [main] - ==>  Preparing: SELECT id , countryname FROM country ORDER BY 						id desc 
	DEBUG [main] - ==> Parameters: 
	DEBUG [main] - <== Total: 183
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值