spring-boot集成4:集成mybatis,druid和tk.mybatis
Why mybatis?
mybatis提供了ORM功能,相比于其他ORM框架,其需要编写更多的sql,也给了我们编写特殊/复杂sql和进行sql优化的机会。
Why druid?
Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。 在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控, 可以很好的监控DB池连接和SQL的执行情况。
Why tk.mybatis?
tk.mybatis是github上一个开源的组件,封装mybatis,提供通用Mapper封装了普通的增删改查和example操作,使我们不用再每个业务单独开发增删改查等的样板代码。
1.maven依赖
<!-- jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!--tk.mybatis 封装了mybatis-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency> <!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency> <!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
2.配置
spring:
application:
name: monocase-framework # 应用名称
jackson:
date-format: yyyy-MM-dd HH:mm:ss # 日期格式
datasource:
druid: # 连接池别名
url: jdbc:mysql://${MYSQL_HOST:192.168.1.200}:${MYSQL_PORT:3306}/zhya-monocase-framework?useUnicode=true&characterEncoding=utf8
username: root
password: root@123
type: com.alibaba.druid.pool.DruidDataSource # 连接池类型
driver-class-name: com.mysql.jdbc.Driver
poolPreparedStatements: true
maxOpenPreparedStatements: 100
maxActive: 100
maxWait: 5000 mybatis:
basepackage: com.zhya.mapper
xmlLocation: classpath:mapper/**/*.xml
mapper-locations: "classpath*:mapper/*.xml"
# config-location: classpath:mybatis-config.xml
configuration:
map-underscore-to-camel-case: true # 下划线转驼峰 # 分页配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql logging:
level:
# tk.mybatis: DEBUG
com.zhya: DEBUG # 输出sql日志
3.druid和mapper配置
package com.zhya.config; import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; /**
* druidl连接池配置
*
* @author: zhangyang
* @create: 2018/11/21 9:48
**/
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "myDataSource")
@ConfigurationProperties("spring.datasource.druid")
public DruidDataSource dataSource() {
return new DruidDataSource();
}
}
package com.zhya; import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement; /**
* 启动类
*
* @Author zhangyang
* @Date 下午 8:23 2018/11/20 0020
**/
@Slf4j
// 开启注解驱动的事务管理功能
@EnableTransactionManagement(proxyTargetClass = true)
// 扫描mapper
@MapperScan(basePackages = "com.zhya.mapper")
// 使用druid data source,排除默认的datasource
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MonocaseFrameworkApplication {
public static void main(String[] args) {
SpringApplication.run(MonocaseFrameworkApplication.class, args);
log.info("MonocaseFrameworkApplication started......");
}
}
4.使用tk.mybatis构造基础架构

如上图:
BaseEntity是基础实体,提供通用的字段如id,add_date等;
SysUser是普通业务实体,继承BaseEntity,扩展业务字段;
IBaseService使用继承自BaseEntity的泛型,来规定通用的增删改查接口;
AbstractBaseService继承IBaseService接口,并使用Mapper根据Entity类型操作对应的表;
SysUserService实现ISysUserService接口,继承AbstractBaseService抽象类,获得通用数据库操作的功能;
BaseController通过实现了IBaseService的接口和Entity类型,调用业务逻辑。
spring-boot集成4:集成mybatis,druid和tk.mybatis的更多相关文章
- 14、Spring Boot 2.x 集成 Druid 数据源
14.Spring Boot 2.x 集成 Druid 数据源 完整源码: Spring-Boot-Demos
- 6、Spring Boot 2.x 集成 MyBatis
1.6 Spring Boot 2.x 集成 MyBatis 简介 详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 完整源码: 1.6.1 创建 spring-bo ...
- spring boot rest 接口集成 spring security(2) - JWT配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot rest 接口集成 spring security(1) - 最简配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- 7、Spring Boot 2.x 集成 Redis
1.7 Spring Boot 2.x 集成 Redis 简介 继续上篇的MyBatis操作,详细介绍在Spring Boot中使用RedisCacheManager作为缓存管理器,集成业务于一体. ...
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
- Spring Boot HikariCP 一 ——集成多数据源
其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...
- Spring Boot系列——如何集成Log4j2
上篇<Spring Boot系列--日志配置>介绍了Spring Boot如何进行日志配置,日志系统用的是Spring Boot默认的LogBack. 事实上,除了使用默认的LogBack ...
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作
spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ...
- 15、Spring Boot 2.x 集成 Swagger UI
1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...
随机推荐
- QOpenGLWidget
QOpenGLWidget描述 QOpenGLWidget类是用于渲染OpenGL图形. 除了可以选择使用QPainter和标准的OpenGL渲染图形,QOpenGLWidget类提供了在Qt应用程序 ...
- 差分约束详解&&洛谷SCOI2011糖果题解
差分约束系统: 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统(system of difference const ...
- error LNK2019 : unresolved external symbol Zbar配置问题
原文链接:https://blog.csdn.net/MengchiCMC/article/details/77871714 出现error LNK2019 : unresolved external ...
- Spring——AOP
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- JS 数组相关的操作函数
// 1.数组拼接 concat() var a = [1, 2]; var b = [3, 4]; console.log(a.concat(b)); // [1, 2, 3, 4] // 2.数组 ...
- BZOJ1601 [Usaco2008 Oct]灌水[最小生成树]
显然分析可知这个图最后连起来是一个森林,每棵树有一个根再算一个代价.那么这些跟需要连向某一点一个建立水库的代价,且根可以有多个但不能没有,则考虑用超级源点0向所有点连虚边,Prim跑MST即可保证有至 ...
- maven打断点报错
- Word:高亮显示文档中的所有英文字符
造冰箱的大熊猫,本文适用于Microsoft Office 2007@cnblogs 2019/4/2 文中图片可通过点击鼠标右键查看大图 1.场景 某天在阅读一个中英文混编的Word文档时,希望将 ...
- MessagePack Java Jackson Dataformat 在 Map 中不使用 String 为 Key 的序列化
当你希望在 Map 中不使用 String 为 Key,那么你需要使用 MessagePackKeySerializer 来为 key 进行序列化. 本测试方法,可以在 https://github. ...
- BeautifulSoup4 提取数据爬虫用法详解
Beautiful Soup 是一个HTML/XML 的解析器,主要用于解析和提取 HTML/XML 数据. 它基于 HTML DOM 的,会载入整个文档,解析整个 DOM树,因此时间和内存开销都会大 ...