欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring Boot与MyBatis整合

发布时间:2025/3/21 javascript 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring Boot与MyBatis整合 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

 

数据库以MySQL为例

IDE:spring tool suite

1. 新建项目

    file -> new -> Spring Starter Project

    填写项目名称,直接next,最后finish。

2. 更改pom.xml

    添加如下依赖:

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version> </dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.27</version> </dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId> </dependency>

3. 更改配置文件

    配置文件使用的yml格式

spring:datasource:name: ebusurl: jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=falseusername: rootpassword: root# 使用druid数据源type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Drivermybatis:type-aliases-package: com.lty.ebus.model#如果不是在Mapper里直接写sql语句,而是在xml里写,需要指定XML文件路径#mapper-locations: classpath:mapping/*.xmllogging:level:root: WARNcom:lantaiyuan:ebus:mapper: TRACE

配置文件使用properties

#主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性 spring.profiles.active=dev spring.application.name=user# 应用自定义配置 logging.config=classpath:logback.xml# mysql spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource #初始化连接大小 spring.datasource.druid.initial-size=8#最小空闲连接数 spring.datasource.druid.min-idle=5 #最大连接数 spring.datasource.druid.max-active=10 #查询超时时间 spring.datasource.druid.query-timeout=6000 #事务查询超时时间 spring.datasource.druid.transaction-query-timeout=6000 #关闭空闲连接超时时间 spring.datasource.druid.remove-abandoned-timeout=1800# REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=10.1.10.80 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.timeout=0#mybatis mybatis.mapper-locations=classpath:mapping/*.xml

注意: type-aliases-package配置项。

4. 新建Model和Mapper

model:

public class City {private int id;private String name;private int state;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getState() {return state;}public void setState(int state) {this.state = state;}}

mapper(需要使用@Mapper注解): 

@Mapper public interface CityMapper {@Select("select * from city where state = #{state}")City findByState(@Param("state") int state);/*** 如果不是在Mapper中写sql语句,这里直接写方法即可。* City findByState(@Param("state") int state);*/}

5. 新建Spring boot application

@SpringBootApplication public class SampleMapperApplication implements CommandLineRunner {public static void main(String[] args) {SpringApplication.run(SampleMapperApplication.class, args);}final private CityMapper cityMapper;public SampleMapperApplication(CityMapper cityMapper) {this.cityMapper = cityMapper;}@Overridepublic void run(String... args) throws Exception {System.out.println(this.cityMapper.findByState(1).getName());}}

6. 右键run as -> Spring Boot App

    运行结果如下:

7. 补充说明

其一:

项目目录结构如下:

SampleMapperApplication.java与mapper的位置需注意,不然mapper有可能注入失败。

其二:

yml配置MySQL连接时,加上autoReconnect=true&useSSL=false

url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false

如果不加上,会有如下警告:

WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

加上后,不再有警告,此处参考Stack overflow。

其三:

第一次使用Spring boot,能快速整合成功,还参考了mybatis-spring-boot-autoconfigure和Spring Boot 集成MyBatis。

转载于:https://my.oschina.net/u/3149614/blog/860199

总结

以上是生活随笔为你收集整理的Spring Boot与MyBatis整合的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。