spring-boot集成4:集成mybatis,druid和tk.mybatis

Why mybatis?

 mybatis提供了ORM功能,相比於其餘ORM框架,其須要編寫更多的sql,也給了咱們編寫特殊/複雜sql和進行sql優化的機會。java

Why druid?

Druid是阿里巴巴開發的號稱爲監控而生的數據庫鏈接池,Druid是目前最好的數據庫鏈接池。 在功能、性能、擴展性方面,都超過其餘數據庫鏈接池,同時加入了日誌監控, 能夠很好的監控DB池鏈接和SQL的執行狀況。mysql

Why tk.mybatis?

 tk.mybatis是github上一個開源的組件,封裝mybatis,提供通用Mapper封裝了普通的增刪改查和example操做,使咱們不用再每一個業務單獨開發增刪改查等的樣板代碼。git

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構造基礎架構

如上圖:github

BaseEntity是基礎實體,提供通用的字段如id,add_date等;spring

SysUser是普通業務實體,繼承BaseEntity,擴展業務字段;sql

IBaseService使用繼承自BaseEntity的泛型,來規定通用的增刪改查接口;數據庫

AbstractBaseService繼承IBaseService接口,並使用Mapper根據Entity類型操做對應的表;mybatis

SysUserService實現ISysUserService接口,繼承AbstractBaseService抽象類,得到通用數據庫操做的功能;架構

BaseController經過實現了IBaseService的接口和Entity類型,調用業務邏輯。app

相關文章
相關標籤/搜索