mybatis或mybatisplus作连表查询时,输入参数每每不是单一的实体类,而是采用更灵活的Map对象,<br>
但map中key参数的名称定义过于随便,虽然能够使用接口定义常量。但原生mybatis在xml中调用静态类方法和变量时须要填写完整的包名不利于大量采用<br>
是否能够像在mybatisplus中使用lambda表达式翻译entity中的列名称<br>
mpp作了封装支持xml的ognl中引入默认包名,并支持lambda定义列名称<br>
例如xml使用如下语句引入map参数中create_time
原生方式<br>java
#{create_time}
mpp的默认包名引用接口常量方式<br>
配置文件中mpp.utilBasePath可设置ognl默认包名<br>git
#{${@ColInfo@createTime}}
mpp的lambda方式github
#{${@MPP@col("TestEntity::getCreateTime")}}
从中央库引入jarmybatis
<dependency> <groupId>com.github.jeffreyning</groupId> <artifactId>mybatisplus-plus</artifactId> <version>1.1.0-RELEASE</version> </dependency>
在实体类字段上设置@InsertFill,在插入时对seqno字段自动填充复杂计算值
查询当前最大的seqno值并加3,转换成10位字符串,不够位数时用0填充app
@TableField(value="seqno",fill=FieldFill.INSERT ) @InsertFill("select lpad(max(seqno)+3,10,'0') from test") private String seqno;
在实体类主键字段上设置@InsertFill,在插入时对id字段自动填充复杂计算值ide
@TableId(value = "id", type=IdType.INPUT) @InsertFill("select CONVERT(max(seqno)+3,SIGNED) from test") private Integer id;
在实体类字段上设置@InsertFill @UpdateFill,插入和更新时使用当前时间填充this
@InsertFill("select now()") @UpdateFill("select now()") @TableField(value="update_time",fill=FieldFill.INSERT_UPDATE) private Date updateTime;
在启动类中使用@EnableMPP启动扩展自定义填充功能和自动建立resultmap功能spa
@SpringBootApplication @EnableMPP public class PlusDemoApplication { public static void main(String[] args) { SpringApplication.run(PlusDemoApplication.class, args); } }
在实体类上使用@AutoMap注解br/>JoinEntity是TestEntity的子类
@TableName(autoResultMap=true)
autoResultMap必须设置为truebr/>父类能够不加@AutoMap,父类设置autoResultMap=true时mybatisplus负责生成resultmap
但原生mybatisplus生成的resultmap的id为mybatis-plus_xxxx没有scan.前缀翻译
@AutoMap @TableName(autoResultMap=true) public class JoinEntity extends TestEntity{ @TableField("some2") private String some2; public String getSome2() { return some2; } public void setSome2(String some2) { this.some2 = some2; } @Override public String toString() { return "JoinEntity{" + "some2='" + some2 + '\'' + '}'; } }
配置文件中加入扫描entity路径,多个路径用逗号分隔code
mpp: entityBasePath: com.github.jeffreyning.mybatisplus.demo.entity
配置文件中加入ognl执行java静态方法的类加载默认路径,多个路径用逗号分隔
mpp: utilBasePath: com.github.jeffreyning.mybatisplus.demo.common
xml文件中引入自动生成的resultMap & xml中使用省略包名调用静态方法 & @MPP@col经过entity的lambda表达式翻译列名
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.github.jeffreyning.mybatisplus.demo.mapper.TestMapper"> <select id="queryUseRM" resultMap="scan.mybatis-plus_JoinEntity"> select * from test inner join test2 on test.id=test2.refid </select> <select id="queryUse" resultMap="scan.mybatis-plus_JoinEntity"> select * from test inner join test2 on test.id=test2.refid where test.create_time <![CDATA[ <= ]]> #{${@MPP@col("TestEntity::getCreateTime")}} and test.id=#{${@MPP@col("TestEntity::getId")}} and update_time <![CDATA[ <= ]]> #{${@ColInfo@updateTime}} </select> </mapper>
接口直接返回实例类
@Mapper public interface TestMapper extends BaseMapper<TestEntity> { public List<JoinEntity> queryUseRM(); public List<JoinEntity> queryUse(Map param); }
demo下载
mybatisplus-plus 1.1.0 示例工程下载地址
连接:https://pan.baidu.com/s/1uGyywC-9-R0L_i7fWAIDwA
扫描订阅公众号,回复"plus"获取下载密码