springboot配置mysql连接的实例代码
作者:guxiaohai_ 时间:2024-01-21 20:47:52
一:导入pmo.xm配置包
mysql库连接、druid连接池、mybatis组件
<!-- 使用MySQL数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 使用mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
配置扫描文件
<build>
<!--扫描xml文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
二:application.yml文件配置
#项目工程信息
spring:
#Mysql数据库信息
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://连接IP地址:端口/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: 用户名
password: 密码
type: com.alibaba.druid.pool.DruidDataSource
druid:
#下面为连接池的补充设置,应用到上面所有数据源中
initial-size: 5 #初始化大小,最小,最大
min-idle: 5 #最小,最大
max-active: 20 #最大
max-wait: 60000 #配置获取连接等待超时的时间
time-between-eviction-runs-millis: 60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
min-evictable-idle-time-millis: 300000 #配置一个连接在池中最小生存的时间,单位是毫秒
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true #打开PSCache,并且指定每个连接上PSCache的大小
max-pool-prepared-statement-per-connection-size: 20 #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall
use-global-data-source-stat: true
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #通过connectProperties属性来打开mergeSql功能;慢SQL记录
#配置监控服务器
stat-view-servlet:
login-username: admin
login-password: 123456
reset-enable: false
url-pattern: /druid/*
web-stat-filter:
url-pattern: /* #添加过滤规则
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" #忽略过滤格式
#mybatis集成
mybatis:
mapper-locations: classpath:com/../../mapper/*.xml
三:编写dao层接口
使用注解:@Mapper
四:编写xml文件sql语句
<?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="dao层类路径">
<!--查询-->
<select></select>
<!--新增-->
<insert></insert>
<!--修改-->
<update></update>
<!--删除-->
<delete></delete>
......
</mapper>
来源:https://blog.csdn.net/guxiaohai_/article/details/112170785
标签:springboot,mysql,连接
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
通过两种方式增加从库——不停止mysql服务
2024-01-17 17:44:28
JS实现根据数组对象的某一属性排序操作示例
2024-04-19 10:14:50
![](https://img.aspxhome.com/file/2023/5/135875_0s.png)
python map比for循环快在哪
2021-06-16 09:39:04
![](https://img.aspxhome.com/file/2023/7/88997_0s.png)
wxPython电子表格功能wx.grid实例教程
2023-02-16 13:12:22
Python3.5常见内置方法参数用法实例详解
2023-02-24 21:38:38
Perl中的正则表达式介绍
2023-08-11 21:14:25
Python 面向对象编程详解
2023-06-25 05:24:23
Oracle不同数据库间对比分析脚本
2010-07-26 13:24:00
asp内置对象ObjectContext详解
2007-09-18 13:16:00
Python实现图片添加文字
2021-11-22 21:17:11
简单谈谈Python中函数的可变参数
2022-05-13 02:32:49
Python识别快递条形码及Tesseract-OCR使用详解
2022-10-20 01:32:32
![](https://img.aspxhome.com/file/2023/8/125888_0s.jpg)
Python获取单个程序CPU使用情况趋势图
2021-11-19 02:36:59
![](https://img.aspxhome.com/file/2023/4/115074_0s.png)
SQLServer2005 XML数据操作代码
2024-01-27 11:50:17
python 实现一个图形界面的汇率计算器
2021-05-07 07:12:24
![](https://img.aspxhome.com/file/2023/0/87950_0s.png)
一个将半角"转换为中文"的asp函数
2007-09-19 11:47:00
如何通过阿里云实现动态域名解析DDNS的方法
2022-05-30 23:04:50
Python中集合类型(set)学习小结
2023-03-18 13:56:27
PHP单例模式简单用法示例
2023-11-18 19:45:41
python开发实时可视化仪表盘的示例
2022-04-23 15:33:44
![](https://img.aspxhome.com/file/2023/0/112370_0s.gif)