Spring boot集成各种数据源操作数据库
一、最基础的数据源方式
1.导入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
2.修改spring boot配置文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.username: root
spring.datasource.password: 123456
#spring boot推荐的数据源
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
3.集成mybatis
#mybatis配置
#通配符配置查找mapper.xml
mybatis.mapper-locations=classpath:mapper/*.xml
#实体包位置通配符配置
mybatis.type-aliases-package=com.demo.*.model
#自动转换user_name -> userName
mybatis.configuration.map-underscore-to-camel-case=true
#指定分页数据库类型(方言)
pagehelper.helper-dialect=mysql
#合理查询超过最大也,则查询最后一页
pagehelper.reasonable=true
# 支持通过Mapper接口参数来传递分页参数,默认false
pagehelper.support-methods-arguments=true
# POJO或者Map中发现了countSql属性,就会作为count参数使用
pagehelper.params=pacount=countSql
MyBatis-Spring-Boot-Starter Spring Boot MyBatis-Spring Java
3.0 3.0 3.0 17 或更高
2.3.x 2.5 - 2.7 2.1 8 或更高
2.2.x 2.5 - 2.7 2.0(2.0.6 以上可开启所有特性) 8 或更高
2.1.x 2.1 - 2.4 2.0(2.0.6 以上可开启所有特性) 8 或更高
MyBatis-Spring-Boot-Starter | Spring Boot | MyBatis-Spring | Java |
3.0 | 3.0 | 3.0 | 17+ |
2.3.x | 2.5 - 2.7 | 2.1 | 8+ |
2.2.x | 2.5 - 2.7 | 2.0(2.0.6 以上可开启所有特性) | 8+ |
2.1.x | 2.1 - 2.4 | 2.0(2.0.6 以上可开启所有特性) | 8+ |