<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.connector.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.version}version>
dependency>
spring:
datasource:
url: jdbc:mysql://ip:port/schema?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: ***
password: ****
mybatis:
# 配置所有以Mapper结尾的xml文件
mapper-locations: classpath:*Mapper.xml
# 配置文件的位置,可以配置mapper文件和别名
config-location: classpath:mybatis-config.xml
# 数据实体类的别名,这个包下面的实体类都可以使用别名
typeAliasesPackage: org.numb.domain
mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
configuration>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.connector.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.version}version>
dependency>
spring:
datasource:
# 数据库访问配置, 使用druid数据源(默认数据源是HikariDataSource)
type: com.alibaba.druid.pool.DruidDataSource
#连接池配置
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://ip:port/nowcoder?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: ***
password: ***
# 连接池配置:大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
# 连接等待超时时间
max-wait: 30000
# 配置检测可以关闭的空闲连接,间隔时间
time-between-eviction-runs-millis: 60000
# 配置连接在池中的最小生存时间
min-evictable-idle-time-millis: 300000
# 检测连接是否有效的select语句
validation-query: select '1' from dual
# 申请连接的时候检测,如果空闲时间大于time-between-eviction-runs-millis,执行validationQuery检测连接是否有效,建议配置为true,不影响性能,并且保证安全性。
test-while-idle: true
# 申请连接时执行validationQuery检测连接是否有效,建议设置为false,不然会会降低性能
test-on-borrow: false
# 归还连接时执行validationQuery检测连接是否有效,建议设置为false,不然会会降低性能
test-on-return: false
# 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开
# 打开PSCache,并且指定每个连接上PSCache的大小
pool-prepared-statements: true
max-open-prepared-statements: 20
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙防御sql注入,stat监控统计,logback日志
filters: stat,wall
# Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
#aop-patterns: com.springboot.servie.*
# lowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# WebStatFilter监控配置
web-stat-filter:
enabled: true
# 添加过滤规则:那些访问拦截统计
url-pattern: /*
# 忽略过滤的格式:哪些不拦截,不统计
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
# StatViewServlet配置(Druid监控后台的Servlet映射配置,因为SpringBoot项目没有web.xml所在在这里使用配置文件设置)
stat-view-servlet:
enabled: true
# 配置Servlet的访问路径:访问路径为/druid/**时,跳转到StatViewServlet,会自动转到Druid监控后台
url-pattern: /druid/*
# 是否能够重置数据
reset-enable: false
# 设置监控后台的访问账户及密码
login-username: ***
login-password: ***
# IP白名单:允许哪些主机访问,默认为“”任何主机
# allow: 127.0.0.1
# IP黑名单:禁止IP访问,(共同存在时,deny优先于allow)
# deny: 192.168.1.218
# 配置StatFilter
filter:
stat:
log-slow-sql: true
mybatis:
# 配置所有以Mapper结尾的xml文件
mapper-locations: classpath*:**/*Mapper.xml
# 配置文件的位置
config-location: classpath:mybatis-config.xml
# 数据实体类的别名,这个包下面的实体类都可以使用别名
typeAliasesPackage: org.numb.domain
spring:
datasource:
druid:
stat-view-servlet:
enabled: true
# 配置Servlet的访问路径:访问路径为/druid/**时,跳转到StatViewServlet,会自动转到Druid监控后台
url-pattern: /druid/*
# 是否能够重置数据
reset-enable: false
# 设置监控后台的访问账户及密码
login-username: ***
login-password: ***
使用上面配置的与用户密码,访问http::/localhost:port/druid
例如创建一个job表,根据job_id查询job_status
CREATE TABLE `job` (
`id` bigint NOT NULL AUTO_INCREMENT,
`job_id` varchar(64) NOT NULL,
`job_type` varchar(64) NOT NULL,
`job_status` varchar(64) NOT NULL,
`job_context` longtext,
`create_time` bigint NOT NULL,
`update_time` bigint NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_job_id` (`job_id`),
KEY `idx_job_info` (`job_type`,`job_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
INSERT INTO job(job_id, job_type, job_status, create_time, update_time) VALUES('i3ek7bfUD', 'create_job', 'CREATED', UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()));
@Getter
@Setter
public class JobPo {
int id;
String jobId;
String jobType;
String jobStatus;
String context;
Long createTime;
Long updateTime;
}
JobDao.java
@Mapper
public interface JobDao {
String getJobStatus(String jobId);
}
JobMapper.xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.numb.web.bussiness.job.repo.dao.JobDao">
<resultMap id="AllEntity" type="org.numb.web.bussiness.job.repo.dao.po.JobPo">
<id column="id" jdbcType="BIGINT" javaType="int"/>
<id column="job_id" jdbcType="VARCHAR" javaType="String"/>
<id column="job_type" jdbcType="VARCHAR" javaType="String"/>
<id column="job_status" jdbcType="VARCHAR" javaType="String"/>
<id column="context" jdbcType="BLOB" javaType="String"/>
<id column="create_time" jdbcType="BIGINT" javaType="long"/>
<id column="update_time" jdbcType="BIGINT" javaType="long"/>
resultMap>
<select id="getJobStatus" resultType="string" >
SELECT job_status FROM job WHERE job_id = #{jobId}
select>
mapper>
pom.xml增加如下配置
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
ApiController.java
@Controller
public class ApiController {
@Resource
private JobDao jobDao;
@RequestMapping("/job/{job_id}")
@ResponseBody
public String test(@PathVariable("job_id") String jobId) {
// 业务
return jobDao.getJobStatus(jobId);
}
}
测试