父级模块搭建
子级模块搭建
父级模块、子级模块配置
数据库连接
测试运行
Mysql 8.0.13+
Spring Boot 2.1.3+
File -> new -> Project
点击 next
点击 next
点击 finish
服务接口模块搭建
点击 Next
选择 插件
JDBC驱动链接
Spring Boot start web spring web项目依赖
Mybatis 插件 页面模板框架、Mysql 连接驱动
点击 Next
finish
添加依赖
阿里巴巴 druid 连接池、fastjson
github pagehelp 分页插件
<dependencies>
<!--阿里巴巴 集成Druid连接池 监控面板-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<!--jdbc 驱动链接-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring web 项目依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis 依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--mysql 连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--阿里巴巴的json解析类库fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--github pagehelp 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
</dependencies>
配置父级模块信息
com.sm
demo
0.0.1-SNAPSHOT
去除 多余依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
子模块复用父级模块依赖
父级模块配置
编辑 父级模块 pom.xml 添加 moduls 子级模块
service
本文使用 properties 文件格式
编辑 service 模块 resources-> application.properties
spring boot 2.0 版本以上废除以下配置
spring.datasource.initial-size=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.validation-query=SELECT 1
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
2.0 +版本配置
# #8.0以上版本的mysql使用这个驱动
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
# 配置 hikari 连接池
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.stat-view-servlet.deny=192.168.1.73
spring.datasource.druid.stat-view-servlet.reset-enable=false
#以下4项,生产环境 需要修改
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,*
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.enabled=true
#上下文配置
server.port=9999
# 服务主访问路径
server.servlet.context-path=/demo
编辑 service 模块->test->java->service->ServiceApplicationTests
添加 datasource 测试
package com.sm.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
@SpringBootTest(properties = "workerClassName=A")
class ServiceApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads(){
try {
System.out.println("获取的数据库连接为:"+dataSource.getConnection());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
运行
运行 service 模块
service 模块->src->main->java->service->ServiceApplication
访问
根据 端口号 + 服务主访问路径+ /druid 访问监控面板
本文未经授权不得转载!