spring boot整合mybatis

1,加入依赖

如果你想在springboot中使用MyBatis-Spring-Boot-Starter模块,你只需要加入 mybatis-spring-boot-autoconfigure.jar就可以了,因为它已经加入对(mybatis.jar, mybatis-spring.jar等等这些的依赖。

工程依赖管理采用个人比较熟悉的Maven(事实上SpringBoot与Groovy才是天生一对),这里添加maven依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.1-SNAPSHOT</version>
</dependency>

2,整合步骤

在之前的项目中,我们知道在使用spring整合mybatis时,需要配配sqlSessionFactory和mapper映射接口,MyBatis-Spring-Boot-Starter中帮我们做了以下几件事:

  • 自动检测到存在的dataSource
  • 通过发现的dataSource,自动创建并注册一个sqlSessionFactoryBean
  • 通过sqlSessionFactoryBean自动创建和注册一个sqlSessionTemplate
  • 自动扫描你的mapper,关联到sqlSessionTemplate然后交给spring容器中,然后你就可以注入到你的bean

添加yml配置文件:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/sb_jdbc
    username: root
    password: ****
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

mybatis:
  mapper-locations: classpath*:mapper/*.xml
  check-config-location: true
  type-aliases-package: org.larry.springboot.entity

添加mapper:

@Mapper
public interface UserDao {

    @Select("select id,name from user where id=#{id}")
    User findById(@Param("id") int id) ;

    @Select("select * from user")
    List<User> queryAll() ;

    ....
}

在这里你也可以像正常的spring boot application注入你的mapper(比如set方法,构造方法等,代码略)

在项目中,我们通常使用注解扫描的方式:

  • MyBatis-Spring-Boot-Starter会全局查找通过使用@Mapper注解的mapper(既上面代码所示)
  • 你也可以通过使用@MapperSacn设置指定的扫描对象
  • 在没有实例化MapperFactoryBean 时将不会开启扫描,如果扫描在spring中未开启的时候,你可以通过在方法上使用@bean方式去注册。

使用@mapper需要每mapper类上都要加此注解,我们也可以通过指定扫描的方法:

@SpringBootApplication
@MapperScan("org.larry.springboot.dao")
public class Springboot02Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot02Application.class, args);
    }
}

UserDaoMapper.xml

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.larry.springboot.dao.UserDao">
    <select id="queryAll" resultType="User">
        select id,name from user
    </select>
    .......
</mapper>

测试代码就不贴出来了。

3,mybatis相关配置

跟以前使用spring一样,spring boot应该可以将mybatis的配置参数存放在application.properties下(或者application.yml),mybatis属性配置使用以mybatis前缀的模式(在这里有个小插曲,就是网上有很多文章说到使用idea是获取不到mapper文件路径将会报unbind错误,本人使用16版本的没有此问题)。

  • **config-location: **mybatis配置文件
  • mapper-locations: mapper xml配置路径
  • check-config-location: 配置校验(true/false)
  • type-aliases-package:别名
  • type-handlers-package:类型处理器
  • executor-type: SIMPLE, REUSE, BATCH (optional)

例如(application.yml):

mybatis:
  mapper-locations: classpath*:mapper/*.xml
  check-config-location: true
  type-aliases-package: org.larry.springboot.entity

关于事务使用会在接下来的章节介绍。
具体代码请参考:https://github.com/larrychina/skills/tree/master/springboot02
未完待续。。。。。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值