首页 > 编程语言 > 详细

SpringBoot集成Mybatis

时间:2020-04-02 22:43:45      阅读:64      评论:0      收藏:0      [点我收藏+]

SpringBoot底层是对Spring+SpringMVC的封装,如果使用Mybatis需要与SpringBoot进行整合

Spring整合Mybatis的回顾

技术分享图片

 

1.导入jar

不在dependencies内,独立存在

 

 

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

  导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
<!--mybatis的启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--数据源的驱动-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql的jar包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

   mybatis-spring-boot-starter

    mysql的驱动

  druid数据库的jar

 SpringBoot整合Mybatis

 

2.入口类

 入口类中的配置,启动类中扫描dao文件夹,交给spring工厂管理

技术分享图片

3.配置文件中配置

将mybatis中的核心组件依赖的动态参数告知springboot,由springboot配置完成

注意SpringBoot在与第三方技术集成的过程中,首先要考虑的是该第三方技术有没有提供启动器,如果有,则可以使用自动配置完成

数据源类型  c3p0 jndi  dhco druid

url

driver-class-name

username

password

依赖别名

mapper文件注册

依赖dao包

将动态参数稿纸springboot由springboot完成对象创建的过程称之为自动配置

在Application.yml的配置

server:
  port: 8989          #配置端口号
  context-path: /springboot03   #配置项目名称
#springboot的数据源配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/ems
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  type-aliases-package: com.nylg.entity
  mapper-locations: classpath:com/nylg/mapper/*Mapper.xml

 4.传统操作

建表

实体类

dao

mapper

service

controller

 

SpringBoot集成Mybatis

原文:https://www.cnblogs.com/ghwq/p/12622663.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!