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主鍵內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: