开发者

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

开发者 https://www.devze.com 2022-12-20 10:42 出处:网络 作者: 求道之愚者
目录springboot框架中如何整合myBATis框架? 一、在pom.XML 文件引入对应依赖二、写配置springboot框架中如何整合mybatis框架?
目录
  • springboot框架中如何整合myBATis框架?
  •  一、在pom.XML 文件引入对应依赖
  • 二、写配置

springboot框架中如何整合mybatis框架?

思路: 1.引入依赖

springboot-web

mysgl相关mysql驱动druid数据源

mybatis相关的(mybatis-spring-boot-stater)依赖(mybatis mybatis-spring)

2.书写配置

a.开启js注解扫描@SpringBootApplication @Componentscan 省略 b.创建数据源

1.指定数据源类型2.指定数据库驱动3.指定url

4.指定username5.指定password

c.创建sqlsess开发者_Go学习ionFactory

1.指定mapper配置文件位置2.指定实体所在包位置起别名

d.创建DAO

1.指定DAo接收所在包

e.创建事务管理器

开启注解式事务生效省略

 一、在pom.xml 文件引入对应依赖

   <!--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</androidversion>
        </dependency>

二、写配置

spring:
  #整合mybatis配置  一
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource   #指定数据源类型
    #指定数据驱动   8.x使用 com.mysql.cj.jdbc.Driver ||5.x  com.mysql.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver  #驱动版本
    url: jdbc:mysql://localhost:3306/bootssm?characterEncoding=UTF-8  #jdbc:mysql://localhost:3306/{{数据库}}?charactxjFfLJerEncoding=UTF-8
    username: root
    password:
 
#整合mybatis配置  二
#指定mapper配置文件位置
mybatis:
  mapper-loandroidcations: classpath:com/mapper/*.xml
  type-aliases-package: com.baizhi.entity   #指定别名  实体类   默认别名:类名 类名首字母小写

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

在运行主类添加注解

@MapperScan(“加上对应的dao包”) //整合mybatis配置  三  //用在类上 :用来扫描dao接口所在的包

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

测试: 创建对应实体类 

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

 创建测试dao接口:

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

编写 继承 测试的UserDao 接口 mybatis dao

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

编写测试接口,和实现方法 和控制器

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

实现 并依赖注入dao

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

 控制器:

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

测试xjFfLJ结果

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

到此这篇关于springboot框架中如何整合mybatis框架的文章就介绍到这了,更多相关springboot整合mybatis框架内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号