1. 程式人生 > >springboot 配置mybatis通用mapper

springboot 配置mybatis通用mapper

宣告:

此處為springboot 配置mybatis的通用mapper方

一共步其他多餘操作不要有

1新增mapper依賴

一定要有以下依賴的jar包

注意jar包版本,太高會導致功能不可用

 <!-- Spring Boot Mybatis 依賴 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.1.7</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>3.4.6</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>

2application.yml.檔案

配置資料來源,myabtis  logging和elastic-job可忽略

spring:
  datasource:
    jdbc-url: jdbc:mysql://172.20.94.39:3306/marketing?characterEncoding=utf-8&verifyServerCertificate=false&useSSL=false&requireSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: marketing
    password: marketing
    type: com.zaxxer.hikari.HikariDataSource
mybatis:
  type-aliases-package: com.renrenche.marketing.ads_scheduler.domain
#  mapper-locations: classpath:mapper/*.xml


logging:
  config: classpath:log4j2-development.yml

elastic-job:
  zookeeper-servers: 172.20.94.39:2181
  namespace: marketing-ads-scheduler


3編寫MyMapper通用工具類 放在util包下,不要與正常的dao層mapper放到一起!!!

package com.renrenche.marketing.ads_scheduler.common.utils;

/**
 * Created by Administrator on 2017/8/4.
 */

import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;

/**
 * 繼承自己的MyMapper
 *
 * @author junwen
 * @since 2017年8月22日11:50:27
 */
public interface MyMapper<T> extends Mapper<T>,
        MySqlMapper<T>,
        ConditionMapper<T>,
        InsertListMapper<T> {

    //TODO
    //FIXME 特別注意,該介面不能被掃描到,否則會出錯
}

4配置mapper配置檔案

package com.renrenche.marketing.ads_scheduler.config;

import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

@Configuration
public class MapperConfig {
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.renrenche.marketing.ads_scheduler.dao");//掃描該路徑下的dao
        Properties properties = new Properties();
        properties.setProperty("mappers", "com.renrenche.marketing.ads_scheduler.common.utils.MyMapper");//通用dao
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);
        return mapperScannerConfigurer;
    }

}

5不需要在啟動類上新增@MapperScan註解
6不需要在application.yml檔案新增mapper配置

配好之後使用方法    dao.select()無報錯並有返回值就代表已經配好了。
通用mapper的好處,可以省區增刪改查的程式碼開發,加快開發速度,有很好的通用方法。省去一些值需要寫簡單增刪改查的xml檔案。