tk.mybatis主键生成策略使用uuid生成或者序列生成
说明:GeneratedValue和keySql2个注解选一个即可,keySql注解是来替换GeneratedValue注解的
方法一,使用@GeneratedValue
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "select sys_guid() from dual")
这种方法是官网的写法,但是存在一个问题,没有在插入之前生成主键id,所以报错如下
java.sql.SQLException: 无效的列类型: 1111
解决方法就是新增如下配置
package com.yss.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import java.util.Properties;
/**
* @author wangmin
* @date 2019-12-01 14:49
*/
@Configuration
public class SpringConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
// 包扫描
configurer.setBasePackage("com.yss.mapper");
Properties properties = new Properties();
// 设置在新增之前生成uuid
properties.setProperty("order", "before");
configurer.setProperties(properties);
return configurer;
}
}
删除包扫描
问题完美解决
方法二,使用@keySql
@Id
@KeySql(sql = "select sys_guid() from dual", order = ORDER.BEFORE)
其他的无需配置,若想使用序列,直接将sql语句修改成序列即可
这样
@Id
@KeySql(sql = “select people_seq.nextval from dual”, order = ORDER.BEFORE)