您好,登錄后才能下訂單哦!
今天小編給大家分享一下使用mybatisPlus生成oracle自增序列遇到的坑如何解決的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
記錄一次使用mybatisPlus遇到的坑,在網上找了各種配置,依然沒有實現oracle插入數據實現序列自增,原因是引入的mybatisPlus依賴有誤。
下面記錄下代碼:
<!--mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.6</version> </dependency>
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<People> { List<People> selectDistinct(); void importExcel(List<People> list); void importAll(List<People> list); void callInsert(Map<String, Object> 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<People> { /** * excel導入 * * @param list * @return */ Object importExcel(List<People> list) throws Exception; List<ErrorExcelResult> importExcelForEach(List<People> list) throws Exception; List<People> selectDistinct(); }
@Service public class PeopleServiceImpl extends ServiceImpl<PeopleMapper, People> implements PeopleService { @Autowired private PeopleMapper peopleMapper; @Override @Transactional(rollbackFor = Exception.class) public Object importExcel(List<People> list) throws Exception { if (MyStringUtil.isEmpty(list)) { throw new Exception("沒有要導入的數據"); } peopleMapper.importAll(list); Map<String, Object> map = new HashMap<>(); peopleMapper.callInsert(map); List<People> repeatList = (List<People>) map.get("P_CURSOR"); List<ErrorExcelResult> 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<ErrorExcelResult> importExcelForEach(List<People> list) throws Exception { if (MyStringUtil.isEmpty(list)) { throw new Exception("沒有要導入的數據"); } List<ErrorExcelResult> errorList = new ArrayList<>(); ErrorExcelResult errorExcelResult; List<People> rightList = new ArrayList<>(); long start = System.currentTimeMillis(); for (People p : list) { //如果重複記錄該條數據 if (peopleMapper.selectCount(new QueryWrapper<People>().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<People> 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; } }
只是白天工作的時候一直生成數據庫序列自增失敗,所以下班想找找原因,代碼寫的特別簡單,就是為了試試能不能生成自增序列。
以上就是“使用mybatisPlus生成oracle自增序列遇到的坑如何解決”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。