springboot框架中如何整合mybatis框架思路詳解
springboot框架中如何整合mybatis框架?
思路: 1.引入依賴
springboot-web
mysgl相關(guān)mysql驅(qū)動druid數(shù)據(jù)源
mybatis相關(guān)的(mybatis-spring-boot-stater)依賴(mybatis mybatis-spring)
2.書寫配置
a.開啟注解掃描@SpringBootApplication @Componentscan 省略 b.創(chuàng)建數(shù)據(jù)源
1.指定數(shù)據(jù)源類型2.指定數(shù)據(jù)庫驅(qū)動3.指定url
4.指定username5.指定password
c.創(chuàng)建sqlsessionFactory
1.指定mapper配置文件位置2.指定實體所在包位置起別名
d.創(chuàng)建DAO
1.指定DAo接收所在包
e.創(chuàng)建事務(wù)管理器
開啟注解式事務(wù)生效省略
一、在pom.xml 文件引入對應(yīng)依賴
<!--mybatis 依賴--> <!--引入 mybatis-spring-boot-starter 的依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.1</version> </dependency>
二、寫配置
spring: #整合mybatis配置 一 datasource: type: com.alibaba.druid.pool.DruidDataSource #指定數(shù)據(jù)源類型 #指定數(shù)據(jù)驅(qū)動 8.x使用 com.mysql.cj.jdbc.Driver ||5.x com.mysql.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver #驅(qū)動版本 url: jdbc:mysql://localhost:3306/bootssm?characterEncoding=UTF-8 #jdbc:mysql://localhost:3306/{{數(shù)據(jù)庫}}?characterEncoding=UTF-8 username: root password: #整合mybatis配置 二 #指定mapper配置文件位置 mybatis: mapper-locations: classpath:com/mapper/*.xml type-aliases-package: com.baizhi.entity #指定別名 實體類 默認別名:類名 類名首字母小寫
在運行主類添加注解
@MapperScan(“加上對應(yīng)的dao包”) //整合mybatis配置 三 //用在類上 :用來掃描dao接口所在的包
測試: 創(chuàng)建對應(yīng)實體類
創(chuàng)建測試dao接口:
編寫 繼承 測試的UserDao 接口 mybatis dao
編寫測試接口,和實現(xiàn)方法 和控制器
實現(xiàn) 并依賴注入dao
控制器:
測試結(jié)果
到此這篇關(guān)于springboot框架中如何整合mybatis框架的文章就介紹到這了,更多相關(guān)springboot整合mybatis框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring事務(wù)&Spring整合MyBatis的兩種方式
- SpringBoot如何整合mybatis-generator-maven-plugin 1.4.0
- SpringBoot整合Mybatis之各種查詢、模糊查詢、批量刪除、動態(tài)表名操作
- SpringBoot3整合MyBatis出現(xiàn)異常:Property?'sqlSessionFactory'or?'sqlSessionTemplate'?are?required
- SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據(jù)持久化的操作
- springboot整合mybatis的超詳細過程(配置模式+注解模式)
- 詳解SpringBoot整合MyBatis詳細教程
- Spring 整合 MyBatis的實現(xiàn)步驟
相關(guān)文章
Java購物系統(tǒng)設(shè)計與實現(xiàn)
這篇文章主要為大家詳細介紹了Java購物系統(tǒng)設(shè)計與實現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01Spring?Boot實現(xiàn)JWT?token自動續(xù)期的實現(xiàn)
本文主要介紹了Spring?Boot實現(xiàn)JWT?token自動續(xù)期,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12SpringBoot返回前端Long類型字段丟失精度問題及解決方案
Java服務(wù)端返回Long整型數(shù)據(jù)給前端,JS會自動轉(zhuǎn)換為Number類型,本文主要介紹了SpringBoot返回前端Long類型字段丟失精度問題及解決方案,感興趣的可以了解一下2024-03-03Spring?Boot數(shù)據(jù)響應(yīng)問題實例詳解
這篇文章主要給大家介紹了關(guān)于Spring?Boot數(shù)據(jù)響應(yīng)問題的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03SpringBoot?實現(xiàn)自定義的?@ConditionalOnXXX?注解示例詳解
這篇文章主要介紹了SpringBoot?實現(xiàn)自定義的?@ConditionalOnXXX?注解,通過示例代碼介紹了實現(xiàn)一個自定義的?@Conditional?派生注解,Conditional?派生注解的類如何注入到?spring?容器,需要的朋友可以參考下2022-08-08SpringBoot Mybatis批量插入Oracle數(shù)據(jù)庫數(shù)據(jù)
這篇文章主要介紹了SpringBoot Mybatis批量插入Oracle數(shù)據(jù)庫數(shù)據(jù),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08