67,546
社区成员




@Id
@SequenceGenerator(name = "SEQ", sequenceName = "SEQ_ATTR_ID", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
private Long attr_id;
int row = mapper.insertSelective(attr);
package tk.mybatis.mapper.common.base.insert;
import org.apache.ibatis.annotations.InsertProvider;
import tk.mybatis.mapper.provider.base.BaseInsertProvider;
/**
* 通用Mapper接口,插入
*
* @param <T> 不能为空
* @author liuzh
*/
public interface InsertSelectiveMapper<T> {
/**
* 保存一个实体,null的属性不会保存,会使用数据库默认值
*
* @param record
* @return
*/
@InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
int insertSelective(T record);
}
package com.zr;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.github.pagehelper.PageHelper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
@EnableScheduling
@SpringBootApplication // 启动
@ServletComponentScan // 拦截器
@EnableTransactionManagement // 事务管理
public class Startup {
@Value("${myData.port.ajpport}")
private int ajpport;
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
p.setProperty("dialect", "oracle");
pageHelper.setProperties(p);
return pageHelper;
}
//重点在于这个方法
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.zr.mapper");
Properties propertiesMapper = new Properties();
// 通用mapper位置,不要和其他mapper、dao放在同一个目录
propertiesMapper.setProperty("mappers", "com.zr.bean.MyMapper");
propertiesMapper.setProperty("notEmpty", "false");
// 主键UUID回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)
propertiesMapper.setProperty("ORDER", "BEFORE");
mapperScannerConfigurer.setProperties(propertiesMapper);
return mapperScannerConfigurer;
}
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Startup.class);
// 修改Banner的模式为OFF
builder.bannerMode(Banner.Mode.OFF).run(args);
}
}
然后实体类id生成改成这样就好了.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "select SEQ_ID.nextval from dual")
private Long id;
private String name;