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 into cuser(uname, realname, password, phone, email, user_type, deleted, birthday) values(#{uname}, #{realname}, #{password}, #{phone}, #{email}, #{userType}, #{deleted}, #{birthday})
service层中获取插入的id:
baseMapper.insertUser(user); int id= user.getId();
递归获取单位树列表
获取一个单位所有子单位,company表中含有cid和pcid,分别是单位的id和父单位的id,最顶层的单位id为null。想查询出树列表。
class Company是直接根据数据库生成的model,class CompanyVo在Company基础上加了字段
private Listchildren;
通过递归调用获取单位的树列表:
public ListcompanyTree() { // 调用mybatisplus的默认函数获取所有单位 List list = this.list(); // 获取所有最顶层的单位 List parentList = getParentList(list); // 递归调用填充子单位 List allList = getChildrenList(list, parentList); return allList; } private List getParentList(List list) { List parentList = new ArrayList<>(); list.forEach(comp ->{ if(comp.getPcid() == null) { parentList.add(new CompanyVo((comp))); } }); return parentList; } private List getChildrenList(List list, List parentList) { parentList.forEach(parent -> { List childrenList = new ArrayList (); 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 PagelistAllUser(Page page, int cid) { // 获取编号为cid的单位的所有子单位的列表 List allChile = baseMapper.listAllChildComp(cid); if(allChile.size() > 0) { String str = "'"; for(int i=0; i Mapper层,不能直接写in语句,需要用where position,把获取的所有单位编号转换成一个字符串传入:
swagger页面测试:
