tk.mybatis實現(xiàn)uuid主鍵生成的示例代碼
引入依賴
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency>
1、創(chuàng)建一個GenId的實現(xiàn)類
package com.xiaobu.base.entity; import tk.mybatis.mapper.genid.GenId; import java.util.UUID; /** * @author xiaobu * @version JDK1.8.0_171 * @date on 2019/3/27 11:37 * @description V1.0 */ public class UUIdGenId implements GenId<String> { @Override public String genId(String s, String s1) { return UUID.randomUUID().toString().replace("-",""); } }
2、創(chuàng)建實體類
package com.xiaobu.entity; import com.xiaobu.base.entity.UUIdGenId; import lombok.Data; import tk.mybatis.mapper.annotation.KeySql; import javax.persistence.Id; import java.io.Serializable; /** * 功能描述: 測試uuid主鍵生成 * @author xiaobu * @date 2019/3/27 16:30 * @version 1.0 */ @Data public class TbDemo1 implements Serializable { /** * */ @Id @KeySql(genId = UUIdGenId.class) private String id; /** * */ private String name; private static final long serialVersionUID = 1L; }
3、mapper類集成通用mapper
package com.xiaobu.mapper; import com.xiaobu.base.mapper.MyMapper; import com.xiaobu.entity.TbDemo1; import org.apache.ibatis.annotations.Mapper; /** * 功能描述:繼承通用mapper * @author xiaobu * @date 2019/3/27 17:06 * @version 1.0 */ @Mapper public interface TbDemo1Mapper extends MyMapper<TbDemo1> { }
4、測試
package com.xiaobu; import com.xiaobu.entity.TbDemo1; import com.xiaobu.mapper.TbDemo1Mapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * @author xiaobu * @version JDK1.8.0_171 * @date on 2019/3/27 11:11 * @description V1.0 */ @RunWith(SpringRunner.class) @SpringBootTest public class TbDemo1Test { @Autowired private TbDemo1Mapper tbDemo1Mapper; @Test public void insert(){ TbDemo1 tbDemo1 = new TbDemo1(); tbDemo1.setName("測試uuid生成"); tbDemo1Mapper.insert(tbDemo1); System.out.println("新增完成....."); } }
tk.mapper的insertList不支持,自己寫的插入方法也是不支持的。
到此這篇關(guān)于tk.mybatis實現(xiàn)uuid主鍵生成的示例代碼的文章就介紹到這了,更多相關(guān)tk.mybatis生成uuid主鍵內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決myBatis generator逆向生成沒有根據(jù)主鍵的select,update和delete問題
這篇文章主要介紹了解決myBatis generator逆向生成沒有根據(jù)主鍵的select,update和delete問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09簡單學(xué)習(xí)Java API 設(shè)計實踐
API(Application Programming Interface,應(yīng)用程序編程接口)是一些預(yù)先定義的函數(shù),目的是提供應(yīng)用程序與開發(fā)人員基于某軟件或硬件的以訪問一組例程的能力,而又無需訪問源碼,或理解內(nèi)部工作機制的細(xì)節(jié)。需要的可以了解一下2019-06-06Java中ArrayList和LinkedList的遍歷與性能分析
這篇文章主要給大家介紹了ArrayList和LinkedList這兩種list的五種循環(huán)遍歷方式,各種方式的性能測試對比,根據(jù)ArrayList和LinkedList的源碼實現(xiàn)分析性能結(jié)果,總結(jié)結(jié)論。相信對大家的理解和學(xué)習(xí)具有一定的參考價值,有需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-12-12詳解Java中Array和ArrayList的比較和轉(zhuǎn)換
在 Java 編程中,arrays 和 arraylists 都是基本的數(shù)據(jù)結(jié)構(gòu),用來存放數(shù)據(jù)集合,雖然兩者的用途一樣,但是它們的特點極大地影響應(yīng)用的性能和靈活性,本文探討 arrays 和 arraylists 的重要特性,它們各自的強項和弱點,,需要的朋友可以參考下2023-08-08詳解SpringBoot中使用JPA作為數(shù)據(jù)持久化框架
這篇文章主要介紹了SpringBoot中使用JPA作為數(shù)據(jù)持久化框架的相關(guān)知識,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03Spring AOP的幾種實現(xiàn)方式總結(jié)
本篇文章主要介紹了Spring AOP的幾種實現(xiàn)方式總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02