欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MybatisPlus+Postgresql整合的幾個(gè)坑及解決

 更新時(shí)間:2023年03月30日 10:08:56   作者:xuruilll  
這篇文章主要介紹了MybatisPlus+Postgresql整合的幾個(gè)坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

最近把用戶管理服務(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)文章

  • 使用IDEA如何拉取GitLab項(xiàng)目

    使用IDEA如何拉取GitLab項(xiàng)目

    使用IDEA拉取GitLab項(xiàng)目,首先需要組長提供的socket和賬號(hào)密碼登錄內(nèi)網(wǎng)的GitLab,打開IDEA,選擇新建項(xiàng)目,選擇Project from Version Control,然后在項(xiàng)目路徑后面添加.git,以上步驟為個(gè)人操作經(jīng)驗(yàn),希望能為大家提供參考
    2024-10-10
  • 超級(jí)詳細(xì)的Java安裝教程(Mac版)

    超級(jí)詳細(xì)的Java安裝教程(Mac版)

    Java是一種廣泛使用的編程語言,可用于開發(fā)各種類型的應(yīng)用程序,這篇文章主要給大家介紹了關(guān)于Mac系統(tǒng)下Java安裝的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Java多線程之悲觀鎖與樂觀鎖

    Java多線程之悲觀鎖與樂觀鎖

    這篇文章主要為大家詳細(xì)介紹了Java悲觀鎖與樂觀鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 劍指Offer之Java算法習(xí)題精講求和篇

    劍指Offer之Java算法習(xí)題精講求和篇

    跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • SpringBoot項(xiàng)目加入沖突動(dòng)態(tài)監(jiān)測(cè)算法的實(shí)現(xià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-09
  • Spring?Security?OAuth?Client配置加載源碼解析

    Spring?Security?OAuth?Client配置加載源碼解析

    這篇文章主要為大家介紹了Spring?Security?OAuth?Client配置加載源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Java操作MongoDB數(shù)據(jù)庫的示例代碼

    Java操作MongoDB數(shù)據(jù)庫的示例代碼

    這篇文章主要介紹了Java操作MongoDB的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04
  • 淺談在eclipse中如何修改svn的用戶名和密碼

    淺談在eclipse中如何修改svn的用戶名和密碼

    這篇文章主要介紹了在eclipse中如何修改svn的用戶名和密碼的方法,在eclipse中經(jīng)常用svn進(jìn)行代碼版本控制,提交或更新代碼的時(shí)候需要我們輸入用戶名和密碼。對(duì)此感興趣的話可以來了解一下
    2020-07-07
  • Spring中@Async用法詳解及簡單實(shí)例

    Spring中@Async用法詳解及簡單實(shí)例

    這篇文章主要介紹了Spring中@Async用法詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • IDEA Project不顯示/缺失文件問題及解決

    IDEA Project不顯示/缺失文件問題及解決

    在側(cè)邊欄的project模式下,如果發(fā)現(xiàn)缺少部分文件,可以嘗試關(guān)閉項(xiàng)目,打開項(xiàng)目所在目錄,刪除目錄下的.idea文件夾,然后重新打開項(xiàng)目即可解決
    2024-11-11

最新評(píng)論