SpringBoot+MybatisPlus+PostgreSQL解决方案(DEMO版)

场景需求

  切换MySQL数据源 -> PostgreSQL

所需依赖

<properties>
<druid-spring-boot-starter.version>1.1.18</druid-spring-boot-starter.version>
<mybatis-plus-generator.version>3.1.0</mybatis-plus-generator.version>
<velocity.version>1.7</velocity.version>
</properties>

    <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
    <!-- POSTGRESQL --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
<!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid-spring-boot-starter.version}</version> </dependency> <!-- 生成Dao代码所依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus-generator.version}</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>${velocity.version}</version> <exclusions> <exclusion> <artifactId>commons-collections</artifactId> <groupId>commons-collections</groupId> </exclusion> </exclusions> </dependency>

代码生成器调整

配置(YML)调整

  datasource:
    url: jdbc:postgresql://yourServerAddress:80/database?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&allowMultiQueries=true
    username: yourUserName
    password: yourPassword
    #driverClassName: com.mysql.cj.jdbc.Driver
    driverClassName: org.postgresql.Driver
  
  #validation-query: SELECT 1 FROM DUAL -> 报错dual
  validation-query: SELECT 1

XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zijiejump.wechat.user.dao.mapper.EnterpriseWechatAlluserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zijiejump.wechat.user.model.EnterpriseWechatAlluser">
        <result column="userid" property="userid" />
        <result column="name" property="name" />
        <result column="mobile" property="mobile" />
        <result column="department" property="department" />
        <result column="position" property="position" />
        <result column="gender" property="gender" />
        <result column="email" property="email" />
        <result column="biz_mail" property="bizMail" />
        <result column="is_leader_in_dept" property="isLeaderInDept" />
        <result column="direct_leader" property="directLeader" />
        <result column="avatar" property="avatar" />
        <result column="thumb_avatar" property="thumbAvatar" />
        <result column="telephone" property="telephone" />
        <result column="alias" property="alias" />
        <result column="status" property="status" />
        <result column="qr_code" property="qrCode" />
        <result column="main_department" property="mainDepartment" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        userid, name, mobile, department, position, gender, email, biz_mail, is_leader_in_dept, direct_leader, avatar, thumb_avatar, telephone, alias, status, qr_code, main_department
    </sql>

</mapper>

Mapper

package com.zijiejump.wechat.user.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zijiejump.wechat.user.model.EnterpriseWechatAlluser;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @since 2022-05-15
 */
public interface EnterpriseWechatAlluserMapper extends BaseMapper<EnterpriseWechatAlluser> {

}

Model

package com.zijiejump.wechat.user.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * <p>
 * 
 * </p>
 *
 * @since 2022-05-15
 */
@Data
@Accessors(chain = true)
@TableName("schemaName.tableName")
public class EnterpriseWechatAlluser {

    private static final long serialVersionUID = 1L;

    private String userid;

    private String name;

    private String mobile;

    private String department;
@TableField(exist
= false) #不能使用关键字作字段   private String order; private String position; private String gender; private String email; private String bizMail; private String isLeaderInDept; private String directLeader; private String avatar; private String thumbAvatar; private String telephone; private String alias; private Long status; private String qrCode; private Long mainDepartment; }

Service

package com.zijiejump.wechat.user.service;

import com.zijiejump.wechat.common.service.common.BaseService;
import com.zijiejump.wechat.user.model.EnterpriseWechatAlluser;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @since 2022-05-15
 */
public interface EnterpriseWechatAlluserService extends BaseService<EnterpriseWechatAlluser> {

}

ServiceImpl

package com.zijiejump.wechat.user.service.impl;

import com.zijiejump.wechat.common.service.common.impl.BaseServiceImpl;
import com.zijiejump.wechat.user.dao.EnterpriseWechatAlluserMapper;
import com.zijiejump.wechat.user.model.EnterpriseWechatAlluser;
import com.zijiejump.wechat.user.service.EnterpriseWechatAlluserService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2022-05-15
 */
@Service
public class EnterpriseWechatAlluserServiceImpl extends BaseServiceImpl<EnterpriseWechatAlluserMapper, EnterpriseWechatAlluser> implements EnterpriseWechatAlluserService {

}

Controller

package com.zijiejump.wechat.user.controller;


import com.zijiejump.wechat.common.constant.Constants;
import com.zijiejump.wechat.common.result.Result;
import com.zijiejump.wechat.user.model.EnterpriseWechatAlluser;
import com.zijiejump.wechat.user.service.EnterpriseWechatAlluserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @since 2022-05-15
 */
@RestController
@RequestMapping(Constants.API_V1 + "/statics")
public class EnterpriseWechatAlluserController {

    @Autowired
    private EnterpriseWechatAlluserService alluserService;

    @GetMapping("/list")
    public Result<List<EnterpriseWechatAlluser>> list() {
        List<EnterpriseWechatAlluser> list = alluserService.list();
        return Result.success(list);
    }

    @PostMapping("")
    public Result<EnterpriseWechatAlluser> create(@RequestBody EnterpriseWechatAlluser enterpriseWechatAlluser) {
        alluserService.save(enterpriseWechatAlluser);
        return Result.success(enterpriseWechatAlluser);
    }
}

Test(Postman)

测试-写操作

 

 

测试-读操作

参考文献

  https://www.cnblogs.com/sea-stream/p/11197772.html《PostgreSQL 常用语句》

 

posted @ 2022-05-16 11:46  程序员巨轮  阅读(4011)  评论(0编辑  收藏  举报