SpringBoot:整合Druid并开启监控、整合Mybaits
程序员文章站
2024-03-12 23:12:50
...
一、整合Druid
1、导入starter
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
2、配置yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?serverTimezone=UTC
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource
#连接池配置(可选)
# 连接池的配置信息初始化大小,最小,最大
name: testDruidDataSource
initial-size: 5 #初始化连接大小
max-active: 20 #最大连接数
min-idle: 5 #最小空闲连接数
max-wait: 60000 # 配置获取连接等待超时的时间
#######监控配置 说明请参考Druid Wiki
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall
#配置 Web Stat Filter
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
session-stat-enable: true
session-stat-max-count: 10
principal-session-name: session_name
principal-cookie-name: cookie_name
profile-enable:
# Stat View Servlet 配置 配置默认false
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: false #禁用HTML页面上的“Reset All”功能
login-username: admin #监控页面登录的用户名
login-password: 123 #监控页面登录的密码
#IP白名单(没有配置或者为空,则允许所有访问)
allow:
#IP黑名单 (存在共同时,deny优先于allow)
deny:
由于springboot默认的是hikari,号称最快的数据源,这里我们换成了阿里巴巴的数据源,druid最强大的则是监控功能
class com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper
com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3f866f50
执行一条查询语句
二、整合Mybaits
1、导包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
启动器文档:mybatis-spring-boot-starter
2、实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private int tid;
}
3、StudentMapper
添加了@Mapper注解之后这个接口在编译时会生成相应的实现类
@Mapper
@Repository
public interface StudentMapper {
List<Student> selectAllStudent();
}
4、StudentMapper .xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="site.kexing.mapper.StudentMapper">
<select id="selectAllStudent" resultType="student">
select * from student
</select>
</mapper>
5、配置数据源、实体别名、mapper地址
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?serverTimezone=UTC
username: root
password: 123
mybatis:
type-aliases-package: site.kexing.pojo
mapper-locations: classpath:mapper/*.xml
6、测试输出
@RestController
public class StudentController {
@Autowired
StudentMapper studentMapper;
@RequestMapping("/selectall")
public List<Student> selectAllStudent(){
List<Student> students = studentMapper.selectAllStudent();
return students;
}
}
[{"id":1,"name":"小明","tid":1},{"id":2,"name":"小红","tid":1},{"id":3,"name":"小张","tid":2},{"id":4,"name":"小李","tid":1},{"id":5,"name":"旺旺","tid":1}]
下一篇: 深入理解java中的重载和覆盖
推荐阅读
-
SpringBoot:整合Druid并开启监控、整合Mybaits
-
springboot整合druid并开启监控
-
SpringBoot - 整合Druid数据源并配置监控
-
springboot整合高版本druid数据源(1.1.20+),解决监控页打不开的问题
-
Springboot2.x 整合mybatis + Druid, 配置Druid数据源监控
-
62、尚硅谷_SpringBoot_数据访问-整合Druid&配置数据源监控
-
springboot整合高版本druid数据源(1.1.20+),解决监控页打不开的问题
-
SpringBoot配置Druid并开启监控的方法
-
Druid 的整合 springboot mybaits
-
Druid 的整合 springboot mybaits