亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

相關(guān)文章

最新評(píng)論