tk.mybatis实现uuid主键生成的示例代码
程序员文章站
2022-03-09 17:36:44
引入依赖 tk.mybatis
引入依赖
<dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper-spring-boot-starter</artifactid> <version>2.0.2</version> </dependency>
1、创建一个genid的实现类
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、创建实体类
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不支持,自己写的插入方法也是不支持的。
到此这篇关于tk.mybatis实现uuid主键生成的示例代码的文章就介绍到这了,更多相关tk.mybatis生成uuid主键内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 小米手机怎么开启新闻早报 小米手机开启新闻早报教程
下一篇: aws免费服务器申请及网络代理搭建教程