MybatisPlus+Postgresql整合的幾個坑及解決
最近把用戶管理服務切換成PostgreSql數(shù)據(jù)庫,和Mybatis整合時遇到了幾個坑,記錄一下。
基礎設置
application.yml設置,注意schema的設置
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');插入時的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();
遞歸獲取單位樹列表
獲取一個單位所有子單位,company表中含有cid和pcid,分別是單位的id和父單位的id,最頂層的單位id為null。想查詢出樹列表。
class Company是直接根據(jù)數(shù)據(jù)庫生成的model,class CompanyVo在Company基礎上加了字段
private List<CompanyVo> children;
通過遞歸調(diào)用獲取單位的樹列表:
public List<CompanyVo> companyTree() {
// 調(diào)用mybatisplus的默認函數(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頁面測試:

獲取一個單位及其子單位下所有用戶列表
每個單位通過一個rel_comp_user關(guān)系表和用戶表做了關(guān)聯(lián),想獲取一個單位及其所有子單位的人員列表。
service層代碼
public Page<Cuser> listAllUser(Page<Cuser> page, int cid) {
// 獲取編號為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,把獲取的所有單位編號轉(zhuǎn)換成一個字符串傳入:
<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頁面測試:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring如何基于Proxy及cglib實現(xiàn)動態(tài)代理
這篇文章主要介紹了Spring如何基于Proxy及cglib實現(xiàn)動態(tài)代理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
高并發(fā)環(huán)境下安全修改同一行數(shù)據(jù)庫數(shù)據(jù)的策略分享
隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,越來越多的應用需要在高并發(fā)環(huán)境中運行,數(shù)據(jù)庫的并發(fā)控制成為了業(yè)務的關(guān)鍵,本文將介紹如何在高并發(fā)情況下,安全地修改數(shù)據(jù)庫中的同一行數(shù)據(jù),需要的可以參考一下2023-06-06
Java?String類和StringBuffer類的區(qū)別介紹
這篇文章主要介紹了Java?String類和StringBuffer類的區(qū)別,?關(guān)于java的字符串處理我們一般使用String類和StringBuffer類有什么不同呢,下面我們一起來看看詳細介紹吧2022-03-03

