• springboot 配置mybatis


    第一步,在application.yml文件里面配置

    server:
    port: 9970

    spring:
    datasource:
    url: jdbc:sqlserver://192.168.209.176:1433;DatabaseName=master_data
    username: db_master_data
    password: 123456
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 1000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 30000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,slf4j
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

    mybatis:
    mapper-locations: classpath:mapping/*Mapper.xml
    type-aliases-package: com.aoyuan.entity

    第二部,增加一个config的配置文件,这里使用阿里的druid连接池

    package com.aoyuan.utils;
    import java.sql.SQLException;

    import javax.sql.DataSource;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;

    import com.alibaba.druid.pool.DruidDataSource;

    /**
    * 数据源配置
    * @author Administrator
    *
    */
    @Configuration
    public class DruidConfiguration {

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("{spring.datasource.connectionProperties}")
    private String connectionProperties;

    @Bean
    @Primary
    public DataSource dataSource() {
    DruidDataSource datasource = new DruidDataSource();

    datasource.setUrl(url);
    datasource.setUsername(username);
    datasource.setPassword(password); //这里可以做加密处理
    datasource.setDriverClassName(driverClassName);

    //configuration
    datasource.setInitialSize(initialSize);
    datasource.setMinIdle(minIdle);
    datasource.setMaxActive(maxActive);
    datasource.setMaxWait(maxWait);
    datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    datasource.setValidationQuery(validationQuery);
    datasource.setTestWhileIdle(testWhileIdle);
    datasource.setTestOnBorrow(testOnBorrow);
    datasource.setTestOnReturn(testOnReturn);
    datasource.setPoolPreparedStatements(poolPreparedStatements);
    datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
    try {
    datasource.setFilters(filters);
    } catch (SQLException e) {

    }
    datasource.setConnectionProperties(connectionProperties);

    return datasource;
    }

    }

    第三步,要在启动类上面加一个注解, mapperScan

    @SpringBootApplication(scanBasePackages="com.aoyuan")
    @MapperScan("com.aoyuan.dao")
    public class RunApplication {
    public static void main(String[] args) {
    SpringApplication.run(RunApplication.class, args);
    }
    }

  • 相关阅读:
    django 模板继承
    redis集群环境配置
    压力测试工具:apache bench(ab)
    php yield关键字以及协程的实现
    php图片木马实现原理
    关于接口幂等性
    关于easyswoole实现websocket聊天室的步骤解析
    php混淆加密解密实战
    关于mysql集群主从服务器搭建
    mysql binlog恢复数据实战
  • 原文地址:https://www.cnblogs.com/handsome1013/p/11069783.html
Copyright © 2020-2023  润新知