使用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的更多相关文章

  1. Spring Boot 鉴权之—— springboot2.0.4+mybatis 整合的完整用例

    自上一篇文章的基础上,Spring Boot 鉴权之—— JWT 鉴权我做了一波springboot2.0.4+mybatis 的整合. 参考文章: Spring Boot+Spring Securi ...

  2. SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis

    如何整合MyBatis 1.pom依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <ar ...

  3. SpringBoot2.0整合mybatis、shiro、redis实现基于数据库权限管理系统

    转自https://blog.csdn.net/poorcoder_/article/details/71374002 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管 ...

  4. IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis反向工程;mybatis+springboot逆向工程

    一.搭建环境 采用IDE自动建立项目方式 然后,next next,配置导入依赖包 项目就生成了,在项目下导入配置文件GeneratorMapper.xml(项目结构如图所示) 配置文档,建立数据库和 ...

  5. springBoot2.0 配置 mybatis+mybatisPlus+redis

    一.Idea新建springBoot项目 next到完成,然后修改使用自己的maven 等待下载包 二.pom.xml文件 <?xml version="1.0" encod ...

  6. IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis+Redis处理高并发,穿透问题

    一.当采用reddis缓存的时候,如果同时,一万次访问,那么就会有10000次访问数据库所以就会对数据库造成巨大压力,这时候,就要用到线程 1.方法体上加锁(优点,防护住了并发锁,缺点降低了内存效率) ...

  7. IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis 自动部署的细节问题

    一.加载pom依赖包 <!--spring-boot开发热部署--> <dependency> <groupId>org.springframework.boot& ...

  8. SpringBoot2.0之四 简单整合MyBatis

    从最开始的SSH(Struts+Spring+Hibernate),到后来的SMM(SpringMVC+Spring+MyBatis),到目前的S(SpringBoot),随着框架的不断更新换代,也为 ...

  9. springBoot2.0 配置shiro实现权限管理

    一.前言 基于上一篇springBoot2.0 配置 mybatis+mybatisPlus+redis 这一篇加入shiro实现权限管理 二.shiro介绍 2.1 功能特点 Shiro 包含 10 ...

随机推荐

  1. 全国Uber优步司机奖励政策 (1月18日-1月24日)

    本周已经公开奖励整的城市有:北 京.成 都.重 庆.上 海.深 圳.长 沙.佛 山.广 州.苏 州.杭 州.南 京.宁 波.青 岛.天 津.西 安.武 汉.厦 门,可按CTRL+F,搜城市名快速查找. ...

  2. sql server 对Geography 的增(insert)和查询(select)

    insert:    Location为    Geography类型                INSERT INTO [oss1].[dbo].[Order] ([Location]) VAL ...

  3. 使用AutoFac实现依赖注入(封装一个注册类)

    public class AutoFacBootStrapper { public static void CoreAutoFacInit() { var builder = new Containe ...

  4. dubbo之监控中心(monitor)

    一.monitor是dubbo框架中的一个监控中心.这个只是针对于消费者和提供者进行一个数据记录,不参与业务和使用.当然当monitor挂掉之后,也不会影响服务的正常运行. 二.在阿里的dubbo中也 ...

  5. cocos2d-x3.7 cclabel文字破碎,异常,变乱

    效果图如下: 无论是按钮(control button),还是普通的label都有小概率出现这种情况. 该问题发现于cocos2d-x3.7 原因: 在3.x中使用ttfconfig创建的label, ...

  6. Linux命令非常全

    最近都在和Linux打交道,感觉还不错.这也是很多人喜欢linux的原因,比较短小但却功能强大.我将我了解到的命令列举一下,仅供大家参考: 系统信息 arch 显示机器的处理器架构(1) uname ...

  7. Linux工作环境搭建

    云主机工作环境搭建 网易云主机 需要申请弹性公网IP,不然需要OpenVPN才可以链接. 低于50块钱时,不能进行云主机创建. 更新yum源 cd /etc/yum.repos.d/ mkdir re ...

  8. http知识点 前端

    前端必须明白的http知识点 对于http的报文格式就不多细说了,做为前端开发,我们需要知道前后端联调时的请求和响应之间请求头和返回头之间的关系和每个字段中的涵意,静态文件资源在加载时我们所观察到可性 ...

  9. ntp-redhat 同步时间配置

    1. 选作一个机器作为ntp 服务端,例如 ip 为192.168.0.1 1)安装 ntp服务 yum install ntp 2) 修改ntp.conf 文件 vi /etc/ntp.conf 注 ...

  10. Vuejs 实现简易 todoList 功能 与 组件

    todoList 结合之前 Vuejs 基础与语法 使用 v-model 双向绑定 input 输入内容与数据 data 使用 @click 和 methods 关联事件 使用 v-for 进行数据循 ...