天天看點

mybatis的xml使用ognl調用靜态java類方法時省略包名并支援lambda取執行個體類中列名

mybatis或mybatisplus做連表查詢時,輸入參數往往不是單一的實體類,而是采用更靈活的Map對象,

但map中key參數的名稱定義過于随便,雖然可以使用接口定義常量。但原生mybatis在xml中調用靜态類方法和變量時需要填寫完整的包名不利于大量采用

是否可以像在mybatisplus中使用lambda表達式翻譯entity中的列名稱

mpp做了封裝支援xml的ognl中引入預設包名,并支援lambda定義列名稱

例如xml使用以下語句引入map參數中create_time

原生方式

#{create_time}
           

mpp的預設包名引用接口常量方式

配置檔案中mpp.utilBasePath可設定ognl預設包名

#{${@[email protected]}}
           

mpp的lambda方式

#{${@[email protected]("TestEntity::getCreateTime")}}
           

從中央庫引入jar

<dependency>
        <groupId>com.github.jeffreyning</groupId>
        <artifactId>mybatisplus-plus</artifactId>
        <version>1.1.0-RELEASE</version>
    </dependency>
           

在實體類字段上設定@InsertFill,在插入時對seqno字段自動填充複雜計算值

查詢目前最大的seqno值并加3,轉換成10位字元串,不夠位數時用0填充

@TableField(value="seqno",fill=FieldFill.INSERT )
    @InsertFill("select lpad(max(seqno)+3,10,'0') from test")
    private String seqno;
           

在實體類主鍵字段上設定@InsertFill,在插入時對id字段自動填充複雜計算值

@TableId(value = "id", type=IdType.INPUT)
    @InsertFill("select CONVERT(max(seqno)+3,SIGNED) from test")
    private Integer id;
           

在實體類字段上設定@InsertFill @UpdateFill,插入和更新時使用目前時間填充

@InsertFill("select now()")
    @UpdateFill("select now()")
    @TableField(value="update_time",fill=FieldFill.INSERT_UPDATE)
    private Date updateTime;
           

在啟動類中使用@EnableMPP啟動擴充自定義填充功能和自動建立resultmap功能

@SpringBootApplication
@EnableMPP
public class PlusDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(PlusDemoApplication.class, args);
    }
}
           

在實體類上使用@AutoMap注解

JoinEntity是TestEntity的子類

@TableName(autoResultMap=true)

autoResultMap必須設定為true

父類可以不加@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路徑,多個路徑用逗号分隔

mpp:
  entityBasePath: com.github.jeffreyning.mybatisplus.demo.entity
           

配置檔案中加入ognl執行java靜态方法的類加載預設路徑,多個路徑用逗号分隔

mpp:
  utilBasePath: com.github.jeffreyning.mybatisplus.demo.common
           

xml檔案中引入自動生成的resultMap & xml中使用省略包名調用靜态方法 & @[email protected]通過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[ <= ]]> #{${@[email protected]("TestEntity::getCreateTime")}}
        and test.id=#{${@[email protected]("TestEntity::getId")}}
        and update_time <![CDATA[ <= ]]> #{${@[email protected]}}
    </select>
</mapper>
           

接口直接傳回執行個體類

@Mapper
public interface TestMapper extends BaseMapper<TestEntity> {
    public List<JoinEntity> queryUseRM();
    public List<JoinEntity> queryUse(Map param);
}
           

使用ColNameUtil.pn靜态方法,擷取實體類中讀取方法對應的列名稱

System.out.println(ColNameUtil.pn(TestEntity::getCreateTime));
       System.out.println(ColNameUtil.pn(TestEntity::getId));
       System.out.println(ColNameUtil.pn(JoinEntity::getSome2));
       System.out.println(ColNameUtil.pn(JoinEntity::getUpdateTime));
           

demo下載下傳

mybatisplus-plus 1.1.1 示例工程下載下傳位址

連結:https://pan.baidu.com/s/1X7pftcq5LJvsze7mDK1ALQ

掃描訂閱公衆号,回複"plus"擷取下載下傳密碼

mybatis的xml使用ognl調用靜态java類方法時省略包名并支援lambda取執行個體類中列名

繼續閱讀