本篇内容主要讲解“MybatisPlus+Postgresql整合的坑怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MybatisPlus+Postgresql整合的坑怎么解决”吧!
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
关于自增字段,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是直接根据数据库生成的model,class CompanyVo在Company基础上加了字段
private List<CompanyVo> children;
通过递归调用获取单位的树列表:
public List<CompanyVo> companyTree() {
// 调用mybatisplus的默认函数获取所有单位
List<Company> list = this.list();
// 获取所有最顶层的单位
List<CompanyVo> parentList = getParentList(list);
// 递归调用填充子单位
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关系表和用户表做了关联,想获取一个单位及其所有子单位的人员列表。
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,把获取的所有单位编号转换成一个字符串传入:
<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页面测试:
到此,相信大家对“MybatisPlus+Postgresql整合的坑怎么解决”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/xuruilll/article/details/122670781