tk.mybatis实现uuid主键生成的示例代码
作者:小布1994
本文主要介绍了tk.mybatis实现uuid主键生成的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
引入依赖
<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主键内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!