基於mybatisplus實現自動掃描Entity類構建ResultMap功能,自動填充支持多個主鍵

mybatisplus-plus

mybatisplus-plus對mybatisplus的一些功能補充git

自動填充優化功能 & 自動掃描Entity類構建ResultMap功能
原生mybatisplus只能作%s+1和now兩種填充,mybatisplus-plus在插入或更新時對指定字段進行自定義複雜sql填充。<br>
須要在實體類字段上用原生註解@TableField設置fill=FieldFill.INSERT fill=FieldFill.UPDATE或fill=FieldFill.INSERT_UPDATE不然不會觸發自定義填充<br>br/>mybatisplus-plus使用@InsertFill註解觸發插入時,執行註解中自定義的sql填充實體類字段<br>
mybatisplus-plus使用@UpdateFill註解觸發更新時,執行註解中自定義的sql填充實體類字段<br>br/>還能夠自動填充主鍵字段,解決原生mybatisplus不支持多個主鍵的問題<br>
<br>
在xml中編寫resultmap是件頭痛的事,特別是錶鏈接時返回的對象是多樣的,若是不按照map返回,分別建resultmap工做量會翻倍。<br>
使用@AutoMap註解entity實體類,就能夠在應用啓動時解析使用@TableField註解的字段,自動生成scan.mybatis-plus_xxxx爲id的resultMap<br>
能夠在xml中直接配置使用這個resultMap實例<br>
而且還支持繼承關係,掃描實體子類會附加上父類的字段信息一塊兒構建子類的resultmap<br>
對於各類錶鏈接造成的返回實體對象,能夠經過繼承來生成。經過掃描後自動構建各類resultmap,在xml中引用。<br>github

從中央庫引入jarsql

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

在實體類字段上設置@InsertFill,在插入時對seqno字段自動填充複雜計算值
查詢當前最大的seqno值並加3,轉換成10位字符串,不夠位數時用0填充mybatis

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

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

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

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

@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註解br/>JoinEntity是TestEntity的子類
@TableName(autoResultMap=true)
autoResultMap必須設置爲truebr/>父類能夠不加@AutoMap,父類設置autoResultMap=true時mybatisplus負責生成resultmap
但原生mybatisplus生成的resultmap的id爲mybatis-plus_xxxx沒有scan.前綴this

@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路徑,多個路徑用逗號分隔spa

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

xml文件中引入自動生成的resultMapcode

<?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>
</mapper>

接口直接返回實例類

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

demo下載
mybatisplus-plus 1.0.0 示例工程下載地址
連接:https://pan.baidu.com/s/19t6Z295O9I7MqM6UUNWDYA

掃描訂閱公衆號,回覆"plus"獲取下載密碼
Image text

相關文章
相關標籤/搜索