博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis-Spring-Boot 使用总结
阅读量:4686 次
发布时间:2019-06-09

本文共 2320 字,大约阅读时间需要 7 分钟。

  。
 
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 . 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.propertiesmybatis.config-location=mybatis-config.xml
 
例子2(通过 mybatis.type-aliases-package 和 @Mapper 来配置mybatis):
#application.propertiesmybatis.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;@Mapperpublic 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") 是用来设置别名,而非用于扫描。
 
 
 
 

转载于:https://www.cnblogs.com/larryzeal/p/5874107.html

你可能感兴趣的文章
通过扩展让ASP.NET Web API支持W3C的CORS规范
查看>>
开源项目Html Agility Pack实现快速解析Html
查看>>
docker和kubernetes中hostname的使用和常见问题
查看>>
js调用百度地图接口
查看>>
.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能
查看>>
JDBC/XML的一些基本使用
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(32)-swfupload多文件上传[附源码]...
查看>>
[CLR via C#]5.4 对象哈希码和dynamic基元类型
查看>>
Contoso 大学 - 1 - 为 ASP.NET MVC 应用程序创建 EF 数据模型
查看>>
Lucene.net 实现近实时搜索(NRT)和增量索引
查看>>
Objective-C学习篇09—NSNumber与笑笑语法
查看>>
模式字符串匹配问题(KMP算法)
查看>>
读书笔记---《Docker 技术入门与实践》---为镜像添加SSH服务
查看>>
Code as IaaS for Azure : Terraform 做多一点事情
查看>>
[537.A]2019-08-03(星期六)登顶梧桐山邀请
查看>>
HDOJ,水题继续,杭电1215,七夕节。关于运算优化的数学题。
查看>>
js 递归 汉诺塔的例子
查看>>
向现有的数据库中添加文件组和数据文件
查看>>
Centos7下安装Oracle11g r2
查看>>
Centos6.7搭建ISCSI存储服务器
查看>>