SpringBoot集成MyBatis的Bean配置方式

SpringBoot集成MyBatis的Bean配置方式

SpringBoot是一款輕量級開發的框架,簡化了不少原先的xml文件配置方式,接下來就介紹一下如何不適用XML來配置Mybatis

springboot的yml文件

spring:
  profiles:
    active: dev
  application:
    name: service-fishkk


##MySql
  datasource:
    url: jdbc:mysql://47.94.200.0:3306/mybatis?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&userUnicode=true
    username: fishkk
    password: Root.123
    driver-class-name: com.mysql.jdbc.Driver
##redis路徑配置
  redis:
    host: 47.94.200.0
    port: 6379
    database: 0
    password: 123456
    pool:
      max-active:8

##cloud 的配置 配置
  cloud:
    config:
      uri: http://localhost:8888/
      profile: dev
      label: master

##註冊微服務
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
##Mybatis配置
mybatis:
  type-aliases-package: com.boss.hr.train.fishkkmybatis.entity

配置SessionFactory和數據庫鏈接池

package com.boss.hr.train.fishkkmybatis.config.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Valid;
import java.beans.PropertyVetoException;

/**
 *數據庫資源鏈接的配置
 *
 *@author  fishkk
 *@version  1.0.0
 *@since
 *
 * 修改人信息
 *@author
 *@version
 *@since
 *
 */
@Configuration
@MapperScan("com.boss.hr.train.fishkkmybatis.dao")
public class DataSourceConfiguration {
    /**
     *  數據庫驅動
     */
    @Value("${spring.datasource.driver-class-name}")
    private String jdbcDriver;

    /**
     *  數據庫地址
     */
    @Value("${spring.datasource.url}")
    private String jdbcUrl;

    /**
     * 數據庫鏈接名
     */
    @Value("${spring.datasource.username}")
    private String jdbcUsername;

    /**
     * 數據庫鏈接密碼
     */
    @Value("${spring.datasource.password}")
    private String jdbcPassword;

    /**
     * 返回數據庫鏈接池
     */
    @Bean(name = "dataSource")
    public ComboPooledDataSource createDateSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(jdbcDriver);
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(jdbcUsername);
        dataSource.setPassword(jdbcPassword);
        //關閉鏈接後不自動commit
        dataSource.setAutoCommitOnClose(false);
        return dataSource;
    }
}

@Value是經過從yml文件獲取參數信息來配置java

package com.boss.hr.train.fishkkmybatis.config.dao;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;

/**
 * Session工廠的配置文件
 *
 *@author  fishkk
 *@version  1.0.0
 *@since
 *
 * 修改人信息
 *@author
 *@version
 *@since
 *
 */
@Configuration
public class SessionFactoryConfiguration {
    /**
     *  自動裝在數據庫資源bean
     */
    @Resource
    private DataSource dataSource;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactory() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //Mybatis的參數配置
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //啓用Mybatis的所有xml文件,就不須要一個個去打開
        String packageSerchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/mapper/**.xml";
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSerchPath));
        sqlSessionFactoryBean.setDataSource(dataSource);
        //實體類所在的包
        sqlSessionFactoryBean.setTypeAliasesPackage("com.boss.hr.train.fishkkmybatis.entity");
        return sqlSessionFactoryBean;
    }
}

經過上面的兩個Bean就能夠成功的使用Mybatis來對數據庫進行操做了

mapper下就是和dao層對於的數據庫操做語句
mybatis-config.xml是mybatis的參數配置,能夠開啓駝峯,設置編碼等等。mysql

相關文章
相關標籤/搜索