MybatisPlus+Postgresql整合的幾個(gè)坑及解決
最近把用戶管理服務(wù)切換成PostgreSql數(shù)據(jù)庫,和Mybatis整合時(shí)遇到了幾個(gè)坑,記錄一下。
基礎(chǔ)設(shè)置
application.yml設(shè)置,注意schema的設(shè)置
spring: datasource: platform: postgres url: jdbc:postgresql://192.188.1.245:5432/uum?currentSchema=uum schemaName: uum username: xxxx password: xxxx driver-class-name: org.postgresql.Driver
自增字段
關(guān)于自增字段,postgresql中沒有自增字段,用的是sequence,比如user表中的主鍵id字段:
create sequence uum.userid_seq start with 1 increment by 1 no minvalue no maxvalue cache 1; alter sequence uum.userid_seq owner to smartsys; alter table uum.user alter column id set default nextval('uum.userid_seq');
插入時(shí)的sql,id不傳入。
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.sifang.uum.model.Cuser"> insert into cuser(uname, realname, password, phone, email, user_type, deleted, birthday) values(#{uname}, #{realname}, #{password}, #{phone}, #{email}, #{userType}, #{deleted}, #{birthday}) </insert>
service層中獲取插入的id:
baseMapper.insertUser(user); int id= user.getId();
遞歸獲取單位樹列表
獲取一個(gè)單位所有子單位,company表中含有cid和pcid,分別是單位的id和父單位的id,最頂層的單位id為null。想查詢出樹列表。
class Company是直接根據(jù)數(shù)據(jù)庫生成的model,class CompanyVo在Company基礎(chǔ)上加了字段
private List<CompanyVo> children;
通過遞歸調(diào)用獲取單位的樹列表:
public List<CompanyVo> companyTree() { // 調(diào)用mybatisplus的默認(rèn)函數(shù)獲取所有單位 List<Company> list = this.list(); // 獲取所有最頂層的單位 List<CompanyVo> parentList = getParentList(list); // 遞歸調(diào)用填充子單位 List<CompanyVo> allList = getChildrenList(list, parentList); return allList; } private List<CompanyVo> getParentList(List<Company> list) { List<CompanyVo> parentList = new ArrayList<>(); list.forEach(comp ->{ if(comp.getPcid() == null) { parentList.add(new CompanyVo((comp))); } }); return parentList; } private List<CompanyVo> getChildrenList(List<Company> list, List<CompanyVo> parentList) { parentList.forEach(parent -> { List<CompanyVo> childrenList = new ArrayList<CompanyVo>(); list.forEach(comp -> { if(parent.getCid() == comp.getPcid()) { childrenList.add(new CompanyVo(comp)); } }); parent.setChildren(getChildrenList(list, childrenList)); }); return parentList; }
Swagger頁面測(cè)試:
獲取一個(gè)單位及其子單位下所有用戶列表
每個(gè)單位通過一個(gè)rel_comp_user關(guān)系表和用戶表做了關(guān)聯(lián),想獲取一個(gè)單位及其所有子單位的人員列表。
service層代碼
public Page<Cuser> listAllUser(Page<Cuser> page, int cid) { // 獲取編號(hào)為cid的單位的所有子單位的列表 List<Integer> allChile = baseMapper.listAllChildComp(cid); if(allChile.size() > 0) { String str = "'"; for(int i=0; i<allChile.size(); i++) { str += allChile.get(i).toString(); if(i != allChile.size() - 1) { str += ","; } } str += "'"; System.out.println(str); // 獲取所有這些單位的人員列表 return baseMapper.listAllChildUser(page, str); } else { return null; } }
Mapper層,不能直接寫in語句,需要用where position,把獲取的所有單位編號(hào)轉(zhuǎn)換成一個(gè)字符串傳入:
<select id="listAllChildComp" parameterType="java.lang.Integer" resultType="java.lang.Integer"> WITH RECURSIVE T ( cid, pcid ) AS ( SELECT A.cid, A.pcid FROM company A WHERE A.cid = #{cid} UNION ALL SELECT b.cid, b.pcid FROM company b, T WHERE b.pcid = T.cid ) SELECT cid FROM T </select> <select id="listAllChildUser" resultType="com.sifang.uum.model.Cuser"> select cuser.id id, cuser.uname uname from cuser left join rel_comp_user on cuser.id=rel_comp_user.uid where position(','||rel_comp_user.cid||',' in ','||${childComp}||',')>0 </select>
swagger頁面測(cè)試:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目加入沖突動(dòng)態(tài)監(jiān)測(cè)算法的實(shí)現(xiàn)
沖突動(dòng)態(tài)監(jiān)測(cè)算法是一種網(wǎng)絡(luò)通信中的沖突檢測(cè)方法,適用于無線網(wǎng)絡(luò)或其他共享傳輸介質(zhì)的環(huán)境,本文主要介紹了SpringBoot項(xiàng)目加入沖突動(dòng)態(tài)監(jiān)測(cè)算法的實(shí)現(xiàn),感興趣的可以了解一下2023-09-09Spring?Security?OAuth?Client配置加載源碼解析
這篇文章主要為大家介紹了Spring?Security?OAuth?Client配置加載源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java操作MongoDB數(shù)據(jù)庫的示例代碼
這篇文章主要介紹了Java操作MongoDB的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04