SpringBoot2.0(一) mybatis
使用mybatis
springboot使用mybatis主要依赖 mybatis-spring-boot-starter 来实现。其提供了2中解决方案,一种是使用注解;另一种是简化后的传统的xml方式。
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
application.properties相关配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.xxxxxx.domain
mybatis.mapper-locations=classpath*:sql/*/*.xml spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hr?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=
在启动类上添加对mapper包的扫描@MapperScan
@SpringBootApplication
@MapperScan(
basePackages = "com.xxxxxxx.dao"
)
public class XXApplication { public static void main(String[] args) {
SpringApplication.run(XXApplication.class, args);
}
}
sql脚本的处理方式有如下2种:
注解
public interface UserMapper {
@Select("SELECT * FROM users")
@Results({
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class),
@Result(property = "nickName", column = "nick_name")
})
List<UserEntity> getAll();
@Select("SELECT * FROM users WHERE id = #{id}")
@Results({
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class),
@Result(property = "nickName", column = "nick_name")
})
UserEntity getOne(Long id);
@Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})")
void insert(UserEntity user);
@Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}")
void update(UserEntity user);
@Delete("DELETE FROM users WHERE id =#{id}")
void delete(Long id);
}
xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxxxx.Dao"> <select id="queryXxxx" parameterType="Query" resultType="Result">
select * from xxx
<where>
<if test="xxx != null">
and xxx = #{xxx}
</if>
</where>
</select>
</mapper>
多数据源
上述配置在单数据源下可以正常使用。在多数据源下,配置有些变化。
在使用springboot打包构建可执行jar时,mybatis 默认的vfs读取xml资源会存在问题,其不支持从多层级Jar文件中读取相关配置信息。
故在声明sqlSessionFactory时,指定vfs为springboot提供的VFS实现。
如下code:
@Configuration
@MapperScan(basePackages = "com.xxx.daohistory", sqlSessionTemplateRef = "sqlSessionTemplate")
public class HistoryConfig { @Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setVfs(SpringBootVFS.class);
factoryBean.setTypeAliasesPackage("com.xxxxx.domain"); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:sqlhistory/*.xml")); org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true); factoryBean.setConfiguration(configuration); return factoryBean.getObject();
} @Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate sbtSqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
多个数据源的时候,上述配置配置多份,相应的name做下处理即可。
SpringBoot2.0(一) mybatis的更多相关文章
- Spring Boot 鉴权之—— springboot2.0.4+mybatis 整合的完整用例
自上一篇文章的基础上,Spring Boot 鉴权之—— JWT 鉴权我做了一波springboot2.0.4+mybatis 的整合. 参考文章: Spring Boot+Spring Securi ...
- SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis
如何整合MyBatis 1.pom依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <ar ...
- SpringBoot2.0整合mybatis、shiro、redis实现基于数据库权限管理系统
转自https://blog.csdn.net/poorcoder_/article/details/71374002 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管 ...
- IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis反向工程;mybatis+springboot逆向工程
一.搭建环境 采用IDE自动建立项目方式 然后,next next,配置导入依赖包 项目就生成了,在项目下导入配置文件GeneratorMapper.xml(项目结构如图所示) 配置文档,建立数据库和 ...
- springBoot2.0 配置 mybatis+mybatisPlus+redis
一.Idea新建springBoot项目 next到完成,然后修改使用自己的maven 等待下载包 二.pom.xml文件 <?xml version="1.0" encod ...
- IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis+Redis处理高并发,穿透问题
一.当采用reddis缓存的时候,如果同时,一万次访问,那么就会有10000次访问数据库所以就会对数据库造成巨大压力,这时候,就要用到线程 1.方法体上加锁(优点,防护住了并发锁,缺点降低了内存效率) ...
- IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis 自动部署的细节问题
一.加载pom依赖包 <!--spring-boot开发热部署--> <dependency> <groupId>org.springframework.boot& ...
- SpringBoot2.0之四 简单整合MyBatis
从最开始的SSH(Struts+Spring+Hibernate),到后来的SMM(SpringMVC+Spring+MyBatis),到目前的S(SpringBoot),随着框架的不断更新换代,也为 ...
- springBoot2.0 配置shiro实现权限管理
一.前言 基于上一篇springBoot2.0 配置 mybatis+mybatisPlus+redis 这一篇加入shiro实现权限管理 二.shiro介绍 2.1 功能特点 Shiro 包含 10 ...
随机推荐
- cocos2d中锚点概念
这两天看了下锚点的概念. /** * Sets the anchor point in percent. * * anchorPoint is the point around which all t ...
- 剑指offer题目系列三(链表相关题目)
本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...
- 成都Uber优步司机奖励政策(3月14日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- Awesome Django
Awesome Django If you find Awesome Django useful, please consider donating to help maintain it. ...
- PHP调用wsdl接口实例化SoapClient抛出异常
异常:Message:SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://*****?wsdl' : failed to load externa ...
- hdu2544最短路(floyd基础)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- OSG-交互
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- Mac环境下RabbitMq安装与测试教程
RabbitMq安装与测试教程 Installing on Mac I. 安装 123456789 brew install rabbitmq ## 进入安装目录cd /usr/local/Cella ...
- Linux命令应用大词典-第33章 X Window
33.1 xhost:X服务器的访问控制程序 33.2 xinit:X Window系统初始化 33.3 Xlsclients:在显示器中列出正在运行的客户端应用程序 33.4 xlsfonts:显示 ...