mybatis开发团队为Spring Boot 提供了 MyBatis-Spring-Boot-Starter

首先,MyBatis-Spring-Boot-Starter will:

  • Autodetect an existing DataSource.
  • Will create and register an instance of a SqlSessionFactoryBean passing that DataSource as an input.
  • Will create and register an instance of a SqlSessionTemplate got out of the SqlSessionFactoryBean.
  • Autoscan your mappers, link them to the SqlSessionTemplate and register them to Spring context so they can be injected into your beans.
 
就是说,使用了该Starter之后,只需要定义一个DataSource即可,它会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,并注册到Spring上下文中。
 
MyBatis-Spring-Boot-Application的配置参数也是保存在application.properties文件中,使用前缀 mybatis 。
Property Description
config-location MyBatis xml config file (optional)
mapper-locations Mapper xml config files (optional)
type-aliases-package Package to search for type aliases (optional)
type-handlers-package Package to search for type aliases (optional)
executor-type Executor type: SIMPLE, REUSE, BATCH (optional)
configuration A MyBatis Configuration bean. About available properties see the MyBatis reference page. NOTE This property cannot use at the same time with the config-location.       
(Starter)设置mapper有两种方法:
①使用config-location指定一个config xml,在里面设置 mapper 和 alias 。见例子1。
②使用type-aliases-package,需要配合自动扫描Mappers使用。
 
针对第二种,需要注意的是,如果想要自动扫描Mappers,需要在Mapper接口上标注@Mapper,否则失败。另外,还需要在application.properties文件中声明:mybatis.type-aliases-package 。
 
mapper-locations这个配置参数仅当mapper xml与mapper class不在同一个目录下时有效。所以一般可以忽略。
 
 
例子1(通过 mybatis.config-location 指定config xml,然后在里面设置别名和mapper包):
#application.properties
mybatis.config-location=mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="sample.mybatis.domain"/>
</typeAliases>
<mappers>
<mapper resource="sample/mybatis/mapper/CityMapper.xml"/>
<mapper resource="sample/mybatis/mapper/HotelMapper.xml"/>
</mappers>
</configuration>
 
例子2(通过 mybatis.type-aliases-package 和 @Mapper 来配置mybatis):
#application.properties
mybatis.type-aliases-package=com.expert.pojo
package com.expert.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.expert.pojo.User;
@Mapper
public interface UserMapper {
// @Select("SELECT * FROM user WHERE id = #{ id }")
User getById(String id); @Select("SELECT * FROM user WHERE id = #{ id }")
User getById2(String id);
}
注意:@Alias("xxx") 是用来设置别名,而非用于扫描。
 
 
 
 

MyBatis-Spring-Boot 使用总结的更多相关文章

  1. mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql. 业务逻辑关系:controlle ...

  2. spring boot:配置shardingsphere(sharding jdbc)使用druid数据源(druid 1.1.23 / sharding-jdbc 4.1.1 / mybatis / spring boot 2.3.3)

    一,为什么要使用druid数据源? 1,druid的优点 Druid是阿里巴巴开发的号称为监控而生的数据库连接池 它的优点包括: 可以监控数据库访问性能 SQL执行日志 SQL防火墙 但spring ...

  3. spring boot:使用分布式事务seata(druid 1.1.23 / seata 1.3.0 / mybatis / spring boot 2.3.2)

    一,什么是seata? Seata:Simpe Extensible Autonomous Transcaction Architecture, 是阿里中间件开源的分布式事务解决方案. 前身是阿里的F ...

  4. mybatis spring boot深入

    [size=large]头一篇博文说了整合了spring boot和mybatis大家都可以对后台数据进行操作但是如何才能进行高效的操作数据库节省代码,下面来一起谈论哈mybatis的通用mapper ...

  5. Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:

    Parameter 0 of method orderSqlSessionFactory in com.config.MultipleDBConfig required a single bean, ...

  6. mybatis+spring boot+vue

    参考https://www.cnblogs.com/wlovet/p/8317282.html

  7. spring boot(六):如何优雅的使用mybatis

    *:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...

  8. 超简单,spring boot 添加mybatis

    看了很多人写的博客,要么太复杂,要么没有添加xml的方式,自己亲自配置了一下,供各位参考. 项目截图 1.添加pom文件 <!-- 设置mybatis --> <dependency ...

  9. Spring Boot 4 MyBatis

    SpringBoot内使用MyBatis,可以不使用xml映射配置,通过注解方式映射. pom.xml添加依赖 <dependency> <groupId>org.mybati ...

  10. Spring Boot MyBatis 连接数据库

    最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...

随机推荐

  1. android的开发 华为手机上不显示menu键

    android的开发,华为手机上不显示menu键解决办法: 在AndroidManifest.xml中讲targetSdkVersion改为9. <uses-sdk android:minSdk ...

  2. javascript - return

    return 使用,建议使用vsCode编译器. /** * return:中断语句运行. * * 1.return;和return false是一样的 * 2.return只能返回一个参数,可以是值 ...

  3. Spring整合JDBC实现简单的增删改

    Spring整合JDBC实现简单的增删改: 1.导入Spring的包和数据库的驱动包: 2.选择一个数据源(dbcp和C3P0) 3.导入数据源的包(这里我们使用dbcp) <span styl ...

  4. 排序算法及其java实现

    各种排序算法:冒择路(入)兮(稀)快归堆,桶式排序,基数排序 冒泡排序,选择排序,插入排序,稀尔排序,快速排序,归并排序,堆排序,桶式排序,基数排序 一.冒泡排序(BubbleSort) 1. 基本思 ...

  5. UNIX网络编程卷2进程间通信读书笔记(一)—概述

    http://blog.chinaunix.net/uid-12868584-id-92807.html 写的灰常好,我就转载了 一.什么是进程间通信 IPC是进程间通信的简称,所谓进程通信,就是不同 ...

  6. 【BIRT】Format Number下的Round Mode中的各项解释

    页面展示 从上图我们可以才看出,共有Half Up.Half Down.Half Even.Up.Down.Celling.Floor.Unnecessary 下面一一介绍每一个的意思 Half Up ...

  7. 15-spring学习-集合表达式

    spring认为,数组和List集合是等价的. 要想操作list集合,利用“{内容,内容...}”的形式就可以完成了(类似数组). package com.Spring.ELDemo; import ...

  8. 转:发一个自己用过的makefile .

    #gcc test.cpp -L. -Wl,-Bdynamic -ltestlib -Wl,-Bstatic -ltestlib  -Wl,-Bdynamic #make clean; make in ...

  9. js加强小结

    一)回顾JavaScript基础 (1)函数的定义方式 *>>正常方式 function add(num1,num2){...} >>构造器方式 var add = new F ...

  10. ASP.NET Web API 控制器执行过程

    http://www.cnblogs.com/jin-yuan/p/3952605.html