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

mybatisplus-plus

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

自動填充優化功能 & 自動掃描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>







從中央庫引入jar

    <dependency>
        <groupId>com.github.jeffreyning</groupId>
        <artifactId>mybatisplus-plus</artifactId>
        <version>1.0.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註解br/>JoinEntity是TestEntity的子類
@TableName(autoResultMap=true)
autoResultMap必須設置爲true

br/>父類可以不加@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

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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章