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