日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot整合mybatis步骤

發布時間:2025/1/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot整合mybatis步骤 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring boot整合mybatis步驟

官方說明:MyBatis-Spring-Boot-Starter will help you use MyBatis with Spring Boot

其實就是myBatis看spring boot這么火熱,為了迎合springboot也開發出一套解決方案來湊湊熱鬧, mybatis-spring-boot-starter,這個jar包含了mybatis核心包以及mybatis自動配置類。

依賴jar

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version> </dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>

配置數據庫信息

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mapper開發(注解開發)

@Select("SELECT * FROM user WHERE id= #{id}") User getUserById(int id);

spring boot的注解分析

@ConditionalOnBean(僅僅在當前上下文中存在某個對象時,才會實例化一個Bean) @ConditionalOnClass(某個class位于類路徑上,才會實例化一個Bean) @ConditionalOnExpression(當表達式為true的時候,才會實例化一個Bean) @ConditionalOnMissingBean(僅僅在當前上下文中不存在某個對象時,才會實例化一個Bean) @ConditionalOnMissingClass(某個class類路徑上不存在的時候,才會實例化一個Bean) @ConditionalOnNotWebApplication(不是web應用) @AutoConfigureAfter 在某個bean初始化后再初始化

數據庫連接池

spring boot的默認連接池

1:在pom文件中直接依賴官方提供的spring-boot-start-jdbc模塊

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId> </dependency>

注意:如果你引入了————

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version> </dependency>

那么你無須再去依賴spring-boot-start-jdbc,因為mybatis-spring-boot-starter中包含spring-boot-start-jdbc,不信看下面————

2:springboot默認使用的是tomcat-jdbc數據源

properties文件配置:

spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

將其更改為第三方數據庫連接池

如果不想使用默認的tomcat-jdbc數據源,也可以根據需要選擇其它性能優秀的數據源,如Druid、c3p0等等。以Druid為例。

A: 引入POM依賴

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.6</version> </dependency>

B: 配置

spring.datasource.druid.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8 spring.datasource.druid.username=root spring.datasource.druid.password=root spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver

參考文檔

https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

初始化連接池

spring.datasource.druid.initial-size= spring.datasource.druid.max-active= spring.datasource.druid.min-idle= spring.datasource.druid.max-wait= spring.datasource.druid.pool-prepared-statements= spring.datasource.druid.max-pool-prepared-statement-per-connection-size= spring.datasource.druid.max-open-prepared-statements= #和上面的等價 spring.datasource.druid.validation-query= spring.datasource.druid.validation-query-timeout= spring.datasource.druid.test-on-borrow= spring.datasource.druid.test-on-return= spring.datasource.druid.test-while-idle= spring.datasource.druid.time-between-eviction-runs-millis= spring.datasource.druid.min-evictable-idle-time-millis= spring.datasource.druid.max-evictable-idle-time-millis= spring.datasource.druid.filters= #配置多個英文逗號分隔

druid的監控臺:
http://localhost:8080/druid/index.html

總結

以上是生活随笔為你收集整理的spring boot整合mybatis步骤的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。