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类型,调用业务逻辑。

 

posted @ 2018-11-21 13:52  zhya_hopeful  阅读(1500)  评论(0编辑  收藏  举报