springboot+mybatisplus+postgres整合
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>TestDemo01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<guli.version>0.0.1-SNAPSHOT</guli.version>
<mybatis-plus.version>3.4.2</mybatis-plus.version>
<velocity.version>2.0</velocity.version>
<swagger.version>2.7.0</swagger.version>
<poi.version>3.9</poi.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.8</version>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.14</version>
</dependency>
<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<!-- swagger http://localhost:8012/swagger-ui.html -->
<dependency>
<groupId>cn.weiguangfu</groupId>
<artifactId>springfox-swagger2-plus</artifactId>
<version>2.7.0-1</version>
</dependency>
<!-- Swagger皮肤 - knife4j http://localhost:8080/doc.html#/home -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId>
<version>2.0.5</version>
</dependency>
<!-- 生成md接口文档 swagger2markup-->
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.netzwerg/paleo-core -->
<dependency>
<groupId>ch.netzwerg</groupId>
<artifactId>paleo-core</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.2</version>
</dependency>
<!--添加tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!--日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.24</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.platform.cc.TestApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。
application.properties
# 服务端口
server.port=8012
# 服务名
spring.application.name=testDemo
server.servlet.context-path=/testDemo
# 环境设置:dev、test、prod
#spring.profiles.active=dev
//数据库的地址以及端口号
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres?currentSchema=public
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
#mybatis日志sql
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath*:mapper/*Mapper.xml
swagger.plus.enable=true
配置swagger
package com.platform.cc.config;
import cn.weiguangfu.swagger2.plus.annotation.EnableSwagger2Plus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
// swagger配置类
@Configuration
@EnableSwagger2Plus
public class Swagger2Config
{
@Bean
public Docket docket()
{
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfo(
// swagger页面标题
"接口文档",
// swagger页面描述
"该文档描述了**项目接口信息",
// 标题右边的版本号
"1.1",
// 留空
"",
// 作者联系方式
new Contact("cc", "www.baidu.com", "123.qq.com"),
// license
"123",
// license
"123",
new ArrayList()))
// 分组名称
.groupName("group1")
// 指定扫描接口的包,select和build成组出现
.select()
.apis(RequestHandlerSelectors.basePackage("com.platform.cc.controller"))
.build();
}
}
业务代码
1、entity
@Data
@TableName("t_club")
public class ClubPo {
/**
* 主键
* @TableId中可以决定主键的类型,不写会采取默认值,默认值可以在yml中配置
* AUTO: 数据库ID自增
* INPUT: 用户输入ID
* ID_WORKER: 全局唯一ID,Long类型的主键
* ID_WORKER_STR: 字符串全局唯一ID
* UUID: 全局唯一ID,UUID类型的主键
* NONE: 该类型为未设置主键类型
*/
@TableId(type = IdType.AUTO)
private Integer id;
@TableField("name")
private String name;
@TableField("money")
private Double money;
@TableField("nick_name")
private String nickName;
@TableField("birthday")
private Date birthday;
@TableField("create_time")
private Date createTime;
@TableField("update_time")
private Date updateTime;
}
service:
public interface ClubPoService extends IService<ClubPo> {
}
service.impl:
@Service
public class ClubServiceImpl extends ServiceImpl<ClubDao, ClubPo> implements ClubPoService {
}
dao:
public interface ClubDao extends BaseMapper<ClubPo> {
}
数据库执行脚本:
drop table if exists t_club;
create table t_club (
id int4 not null,
name varchar(32),
money int4,
nick_name varchar(32),
birthday timestamp(6) default current_timestamp,
create_time timestamp(6) default current_timestamp,
update_time timestamp(6) default current_timestamp,
primary key (id)
);
insert into t_club(id, name, money, nick_name, birthday, create_time, update_time) VALUES (2, '张三', 100, 'zhangsanfeng', '2022-07-08 09:41:32', '2022-07-08 09:41:35', '2022-07-08 09:41:38');
打印监听器swagger地址
@Component
public class PlatformStartLisenter implements ApplicationRunner {
private static final Logger log = LoggerFactory.getLogger(PlatformStartLisenter.class);
private String host = "localhost";
@Value("${server.port:8080}")
private String port;
@Value("${server.servlet.context-path:}")
private String contextPath;
public PlatformStartLisenter() {
}
public void run(ApplicationArguments var1) {
log.info("---------启动成功,后端跟路径: http://{}:{}{}", new Object[]{this.host, this.port, this.contextPath});
log.info("---------启动成功,前端首页: http://{}:{}{}/front/dist/index.html", new Object[]{this.host, this.port, this.contextPath});
log.info("---------启动成功,访问druid: http://{}:{}{}/druid", new Object[]{this.host, this.port, this.contextPath});
log.info("---------启动成功,访问knife4j: http://{}:{}{}/doc.html", new Object[]{this.host, this.port, this.contextPath});
}
}