欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot框架中如何整合mybatis框架思路詳解

 更新時間:2022年12月20日 09:08:19   作者:求道之愚者  
這篇文章主要介紹了springboot框架中如何整合mybatis框架,本文通過示例圖文相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論