使用mybatisPlus生成oracle自增序列遇到的坑如何解决
记录一次使用mybatisPlus遇到的坑,在网上找了各种配置,依然没有实现oracle插入数据实现序列自增,原因是引入的mybatisPlus依赖有误。
下面记录下代码:

正确依赖
com.baomidou mybatis-plus-boot-starter 3.0.6
配置文件
mybatis-plus: #配置mapper.xml路径 mapper-locations: classpath:/mapper/*.xml #配置实体类路径 type-aliases-package: com.jp.entity global-config: #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID"; id-type: 1 #驼峰下划线转换 db-column-underline: true configuration: #配置打印sql log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
配置类一定不能少,自以为在application.yml文件中配置过就完事了,谁知道还要添加一个配置类来配置oracle序列,就是这里坑了我好久,一定记得加上。
如下:
package com.jp.config;
import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author ljp
* @date 2020/4/23 22:14
*/
@Configuration
@MapperScan("com.jp.mapper")
public class MybatisPlusConfig {
@Bean
public OracleKeyGenerator oracleKeyGenerator() {
return new OracleKeyGenerator();
}
}下面是实体类
package com.jp.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @author :Y19090908
* @date :Created in 2020/3/25 下午 05:17
*/
@Data
@TableName("people")
@KeySequence(value = "seq_people", clazz = Integer.class)
public class People implements Serializable {
private static final long serialVersionUID = 1110056585174675869L;
@TableId(value = "ID", type = IdType.INPUT)
private Integer id;
private String name;
private String sex;
private String city;
private String job;
private String money;
private Date createTime;
public People() {
}
public People(String name, String sex, String city, String job, String money) {
this.name = name;
this.sex = sex;
this.city = city;
this.job = job;
this.money = money;
}
public People(Integer id, String name, String sex, String city, String job, String money) {
this.id = id;
this.name = name;
this.sex = sex;
this.city = city;
this.job = job;
this.money = money;
}
}mapper:
@Mapper public interface PeopleMapper extends BaseMapper{ List selectDistinct(); void importExcel(List list); void importAll(List list); void callInsert(Map map); void removeAll(); }
service:
package com.jp.service; import com.baomidou.mybatisplus.extension.service.IService; import com.jp.bean.ErrorExcelResult; import com.jp.entity.People; import java.util.List; /** * @author :Y19090908 * @date :Created in 2020/3/25 下午 05:24 */ public interface PeopleService extends IService{ /** * excel導入 * * @param list * @return */ Object importExcel(List list) throws Exception; List importExcelForEach(List list) throws Exception; List selectDistinct(); }
实现类
@Service public class PeopleServiceImpl extends ServiceImplimplements PeopleService { @Autowired private PeopleMapper peopleMapper; @Override @Transactional(rollbackFor = Exception.class) public Object importExcel(List list) throws Exception { if (MyStringUtil.isEmpty(list)) { throw new Exception("沒有要導入的數據"); } peopleMapper.importAll(list); Map map = new HashMap<>(); peopleMapper.callInsert(map); List repeatList = (List ) map.get("P_CURSOR"); List errorList = new ArrayList<>(); ErrorExcelResult errorExcelResult; if (!MyStringUtil.isEmpty(repeatList)) { for (People p : repeatList) { errorExcelResult = new ErrorExcelResult(p.getName(), p.getSex(), p.getCity(), p.getJob(), p.getMoney(), "數據重複"); errorList.add(errorExcelResult); } } // peopleMapper.removeAll(); return errorList; } @Override @Transactional(rollbackFor = Exception.class) public List importExcelForEach(List list) throws Exception { if (MyStringUtil.isEmpty(list)) { throw new Exception("沒有要導入的數據"); } List errorList = new ArrayList<>(); ErrorExcelResult errorExcelResult; List rightList = new ArrayList<>(); long start = System.currentTimeMillis(); for (People p : list) { //如果重複記錄該條數據 if (peopleMapper.selectCount(new QueryWrapper ().eq("name", p.getName()).eq("sex", p.getSex())) > 0) { errorExcelResult = new ErrorExcelResult(p.getName(), p.getSex(), p.getCity(), p.getJob(), p.getMoney(), "該條數據重複"); errorList.add(errorExcelResult); continue; } rightList.add(p); } peopleMapper.importExcel(rightList); // this.saveBatch(rightList); long end = System.currentTimeMillis(); System.out.println(end - start); return errorList; } @Override public List selectDistinct() { return peopleMapper.selectDistinct(); } }
service实现类是要加@Service注解的,之前会忘记。
下面是新增的接口,还是蛮简单的,只把添加的接口展示出来了。
@PostMapping("/page/easy/add")
@ResponseBody
public Object add(@RequestBody People people) {
JSONObject jsonObject = new JSONObject();
try {
validate(people);
people.setCreateTime(new Date());
peopleService.save(people);
jsonObject.put("code", 0);
return jsonObject;
} catch (Exception e) {
log.error(e.getMessage());
jsonObject.put("code", 1);
jsonObject.put("msg", e.getMessage());
return jsonObject;
}
}只是白天工作的时候一直生成数据库序列自增失败,所以下班想找找原因,代码写的特别简单,就是为了试试能不能生成自增序列。
以下是postman测试传入的参数

