tk.mybatis实现uuid主键生成的示例代码

引入依赖

 
            tk.mybatis
            mapper-spring-boot-starter
            2.0.2
        

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 {
    @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 {
 
}

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.mybatis实现uuid主键生成的示例代码_第1张图片

tk.mapper的insertList不支持,自己写的插入方法也是不支持的。

到此这篇关于tk.mybatis实现uuid主键生成的示例代码的文章就介绍到这了,更多相关tk.mybatis生成uuid主键内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(tk.mybatis实现uuid主键生成的示例代码)