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"獲取下載密碼