spring boot整合 beetlSQL和druid

我使用的版本

  • beetl v2.8.5
  • beetlsql v2.10.30
  • spring boot v1.5.12
  • druid-spring-boot-starter v1.1.9

application.yml配置

beet-beetlsql:
  dev: true

spring:
  datasource:
    driver: com.mysql.jdbc.Driver
    druid:
      url: jdbc:mysql://127.0.0.1:3306/my-base?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
      username: root
      password: 123456
      initial-size: 5
      max-active: 20
      min-idle: 10
      max-wait: 10
      filters: stat,wall
      filter.stat.log-slow-sql: true
      filter.stat.slow-sql-millis: 2000

java 配置代码如下:

package top.itczw.beetl.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.beetl.sql.ext.spring4.BeetlSqlScannerConfigurer;
import org.beetl.sql.ext.spring4.SqlManagerFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author czw
 */
@Configuration
public class SqlManagerConfig {

    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "sqlManager")
    @Primary
    public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("dataSource") DataSource master) {
        SqlManagerFactoryBean factoryBean = new SqlManagerFactoryBean();
        BeetlSqlDataSource source = new BeetlSqlDataSource();
        source.setMasterSource(master);
        factoryBean.setCs(source);
        factoryBean.setDbStyle(new MySqlStyle());
        //控制台或者日志系统输出执行的sql语句
        factoryBean.setInterceptors(new Interceptor[]{new DebugInterceptor()});
        //开启驼峰
        factoryBean.setNc(new UnderlinedNameConversion());
        //sql文件路径
        factoryBean.setSqlLoader(new ClasspathLoader("/sql"));
        return factoryBean;
    }

    @Bean(name = "sqlScannerConfigurer")
    public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer() {
        BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
        //mapper路径
        conf.setBasePackage("top.itczw.beetl.dao");
        //后缀
        conf.setDaoSuffix("Dao");
        conf.setSqlManagerFactoryBeanName("sqlManager");
        return conf;
    }

    @Bean(name = "txManager")
    public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("dataSource") DataSource datasource) {
        DataSourceTransactionManager dsm = new DataSourceTransactionManager();
        dsm.setDataSource(datasource);
        return dsm;
    }

}

个人博客:http://www.itczw.top

你可能感兴趣的:(spring,boot,java模板引擎之Beetl,BeetlSQL)