当前位置:龙哥网 > 网络技术 > tk.mybatis实现uuid主键生成的示例代码_java

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

longge3个月前 (03-01)网络技术56

引入依赖

 <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.mybatis实现uuid主键生成的示例代码_java

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

免责声明
本站部分资源来源于互联网 如有侵权 请联系站长删除
龙哥网是优质的互联网科技创业资源_行业项目分享_网络知识引流变现方法的平台为广大网友提供学习互联网相关知识_内容变现的方法。#转载请注明出处!