国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

【springboot系列】springboot整合獨(dú)立模塊Druid + mybatis-plus

RobinTang / 3323人閱讀

摘要:申請(qǐng)連接時(shí)執(zhí)行檢測連接是否有效,做了這個(gè)配置會(huì)降低性能。作者在版本中使用,通過監(jiān)控界面發(fā)現(xiàn)有緩存命中率記錄,該應(yīng)該是支持。允許和不允許單條語句返回多個(gè)數(shù)據(jù)集取決于驅(qū)動(dòng)需求使用列標(biāo)簽代替列名稱。需要驅(qū)動(dòng)器支持。將自動(dòng)映射所有復(fù)雜的結(jié)果。

項(xiàng)目github地址:https://github.com/5-Ason/aso...
具體可看 ./db/db-mysql 模塊

本文主要實(shí)現(xiàn)的是對(duì)數(shù)據(jù)操作進(jìn)行獨(dú)立模塊得整合,詳情請(qǐng)看我的另一篇博文:
【技術(shù)雜談】springcloud微服務(wù)之?dāng)?shù)據(jù)操作獨(dú)立模塊化

1、本篇目標(biāo)

獨(dú)立部署mysql數(shù)據(jù)操作模塊,整合 Druid + mybatis-plus ,實(shí)現(xiàn)SpringBoot項(xiàng)目中依賴數(shù)據(jù)模塊進(jìn)行數(shù)據(jù)操作,并進(jìn)行簡單測試。

2、引入依賴

    org.springframework.boot
    spring-boot-starter-jdbc
    1.5.6.RELEASE


    mysql
    mysql-connector-java
    5.1.36


    com.baomidou
    mybatis-plus
    2.1-gamma


    com.alibaba
    druid
    1.0.13
3、配置屬性

因?yàn)槲业捻?xiàng)目使用的是springcloud分布式配置
所以配置文件在 ./config-server/config-repo/data-dev.yml,具體配置如下:

# ====================mysql====================
connection:
  url: jdbc:mysql://ason-hostname:3306/rms_db?useUnicode=true&characterEncoding=utf8
  username: ason
  password: ason


# ====================druid====================
druid:
  # 初始化時(shí)建立物理連接的個(gè)數(shù)。初始化發(fā)生在顯示調(diào)用init方法,或者第一次getConnection時(shí)
  initialSize: 1
  #    最小連接池?cái)?shù)量
  minIdle: 1
  # 最大連接池?cái)?shù)量
  maxActive: 10
  #    配置獲取連接等待超時(shí)的時(shí)間
  maxWait: 10000
  #  配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
  timeBetweenEvictionRunsMillis: 60000
  #  配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
  minEvictableIdleTimeMillis: 300000
  #  驗(yàn)證連接有效與否的SQL,不同的數(shù)據(jù)配置不同
  validationQuery: select 1
  #  建議配置為true,不影響性能,并且保證安全性。
  #  申請(qǐng)連接的時(shí)候檢測,如果空閑時(shí)間大于
  #  timeBetweenEvictionRunsMillis,
  #  執(zhí)行validationQuery檢測連接是否有效。
  testWhileIdle: true
  #  申請(qǐng)連接時(shí)執(zhí)行validationQuery檢測連接是否有效,做了這個(gè)配置會(huì)降低性能。
  #  這里建議配置為TRUE,防止取到的連接不可用
  testOnBorrow: true
  #  歸還連接時(shí)執(zhí)行validationQuery檢測連接是否有效,做了這個(gè)配置會(huì)降低性能
  testOnReturn: false
  #  是否緩存preparedStatement,也就是PSCache。
  #  PSCache對(duì)支持游標(biāo)的數(shù)據(jù)庫性能提升巨大,比如說oracle。
  #  在mysql5.5以下的版本中沒有PSCache功能,建議關(guān)閉掉。
  #  作者在5.5版本中使用PSCache,通過監(jiān)控界面發(fā)現(xiàn)PSCache有緩存命中率記錄,
  #  該應(yīng)該是支持PSCache。
  #  打開PSCache,并且指定每個(gè)連接上PSCache的大小
  poolPreparedStatements: true
  maxPoolPreparedStatementPerConnectionSize: 20
  #  屬性類型是字符串,通過別名的方式配置擴(kuò)展插件,
  #  常用的插件有:
  #  監(jiān)控統(tǒng)計(jì)用的filter:stat
  #  日志用的filter:log4j
  #  防御sql注入的filter:wall
  filters: stat
  # 訪問的用戶名
  loginUsername: ason
  # 訪問的密碼
  loginPassword: ason

# ====================MybatisPlus====================
mybatisPlus:
  globalConfig:
    #主鍵類型  0:"數(shù)據(jù)庫ID自增", 1:"用戶輸入ID",2:"全局唯一ID (數(shù)字類型唯一ID)", 3:"全局唯一ID UUID";
    idType: 0
    #字段策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷"
    fieldStrategy: 2
    #駝峰下劃線轉(zhuǎn)換
    dbColumnUnderline: true
    #刷新mapper 調(diào)試神器
    isRefresh: true
    #數(shù)據(jù)庫大寫下劃線轉(zhuǎn)換
    isCapitalMode: true
    #邏輯刪除配置
    logicDeleteValue: 0
    logicNotDeleteValue: 1
4、DruidConf配置
package com.ason;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Ason on 2017/9/26.
 */
@Configuration
public class DruidConf {
    private static final Log log = LogFactory.getLog(DruidConf.class);

    @Value("${connection.url}")
    private String connectionUrl;
    @Value("${connection.username}")
    private String username;
    @Value("${connection.password}")
    private String password;
    @Value("${druid.initialSize}")
    private Integer initialSize;
    @Value("${druid.minIdle}")
    private Integer minIdle;
    @Value("${druid.maxActive}")
    private Integer maxActive;
    @Value("${druid.maxWait}")
    private Integer maxWait;
    @Value("${druid.timeBetweenEvictionRunsMillis}")
    private Integer timeBetweenEvictionRunsMillis;
    @Value("${druid.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;
    @Value("${druid.validationQuery}")
    private String validationQuery;
    @Value("${druid.testWhileIdle}")
    private Boolean testWhileIdle;
    @Value("${druid.testOnBorrow}")
    private Boolean testOnBorrow;
    @Value("${druid.testOnReturn}")
    private Boolean testOnReturn;
    @Value("${druid.poolPreparedStatements}")
    private Boolean poolPreparedStatements;
    @Value("${druid.maxPoolPreparedStatementPerConnectionSize}")
    private Integer maxPoolPreparedStatementPerConnectionSize;
    @Value("${druid.filters}")
    private String filters;

    @Value("${druid.loginUsername}")
    private String loginUsername;
    @Value("${druid.loginPassword}")
    private String loginPassword;

    //    配置數(shù)據(jù)源
    @Bean(name = "basisDataSource", initMethod = "init", destroyMethod = "close")
    public DruidDataSource initDataSource() {
        log.info("初始化DruidDataSource");
        DruidDataSource dds = new DruidDataSource();
        dds.setDriverClassName("com.mysql.jdbc.Driver");
        dds.setUrl(connectionUrl);
        dds.setUsername(username);
        dds.setPassword(password);
        dds.setInitialSize(initialSize);
        dds.setMinIdle(minIdle);
        dds.setMaxActive(maxActive);
        dds.setMaxWait(maxWait);
        dds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dds.setValidationQuery(validationQuery);
        dds.setTestWhileIdle(testWhileIdle);
        dds.setTestOnBorrow(testOnBorrow);
        dds.setTestOnReturn(testOnReturn);
        dds.setPoolPreparedStatements(poolPreparedStatements);
        dds.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            dds.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dds;
    }

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //設(shè)置登錄查看信息的賬號(hào)密碼.
        servletRegistrationBean.addInitParameter("loginUsername",loginUsername);
        servletRegistrationBean.addInitParameter("loginPassword",loginPassword);
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

5、MybatisPlusConf
package com.ason;

import com.alibaba.druid.pool.DruidDataSource;
import com.ason.utils.BlankUtil;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * Created by Ason on 2017/8/15.
 */
@Configuration
public class MybatisPlusConf {

    private static final Log log = LogFactory.getLog(DruidConf.class);

    //    mybatisPlus全局配置
    @Bean(name = "globalConfig")
    public GlobalConfiguration globalConfig(
            @Value("${mybatisPlus.globalConfig.idType}") Integer idType, //主鍵類型  0:"數(shù)據(jù)庫ID自增", 1:"用戶輸入ID",2:"全局唯一ID (數(shù)字類型唯一ID)", 3:"全局唯一ID UUID";
            @Value("${mybatisPlus.globalConfig.fieldStrategy}") Integer fieldStrategy, //字段策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷"
            @Value("${mybatisPlus.globalConfig.dbColumnUnderline}") Boolean dbColumnUnderline, //駝峰下劃線轉(zhuǎn)換
            @Value("${mybatisPlus.globalConfig.isRefresh}") Boolean isRefresh, //刷新mapper 調(diào)試神器
            @Value("${mybatisPlus.globalConfig.isCapitalMode}") Boolean isCapitalMode, //數(shù)據(jù)庫大寫下劃線轉(zhuǎn)換
            @Value("${mybatisPlus.globalConfig.logicDeleteValue}") String logicDeleteValue, //邏輯刪除配置
            @Value("${mybatisPlus.globalConfig.logicNotDeleteValue}") String logicNotDeleteValue //邏輯刪除配置
    ) {
        log.info("初始化GlobalConfiguration");
        GlobalConfiguration globalConfig = new GlobalConfiguration();
        if ( !BlankUtil.isBlank(idType)) {
            globalConfig.setIdType(idType);  //主鍵類型
        }
        if ( !BlankUtil.isBlank(fieldStrategy)) {
            //        globalConfig.setFieldStrategy(fieldStrategy); //字段策略
        }
        if ( !BlankUtil.isBlank(dbColumnUnderline)) {
            globalConfig.setDbColumnUnderline(dbColumnUnderline);  //駝峰下劃線轉(zhuǎn)換
        }
        if ( !BlankUtil.isBlank(isRefresh)) {
            //        globalConfig.setRefresh(isRefresh); //刷新mapper 調(diào)試神器
        }
        if ( !BlankUtil.isBlank(isCapitalMode)) {
            globalConfig.setCapitalMode(isCapitalMode); //數(shù)據(jù)庫大寫下劃線轉(zhuǎn)換
        }
        if ( !BlankUtil.isBlank(logicDeleteValue)) {
            //        globalConfig.setLogicDeleteValue(logicDeleteValue);  //邏輯刪除配置
        }
        if ( !BlankUtil.isBlank(logicNotDeleteValue)) {
            //        globalConfig.setLogicNotDeleteValue(logicNotDeleteValue);  //邏輯刪除配置
        }
        return globalConfig;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier(value = "globalConfig")GlobalConfiguration globalConfig,
                                               @Qualifier(value = "basisDataSource")DruidDataSource dataSource) throws Exception {
        log.info("初始化SqlSessionFactory");
        String mapperLocations = "classpath:db-ason/sql/**/*.xml";
        String configLocation = "classpath:db-ason/mybatis/mybatis-sqlconfig.xml";
        String typeAliasesPackage = "com.ason.entity.**";
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource); //數(shù)據(jù)源
        sqlSessionFactory.setGlobalConfig(globalConfig); //全局配置
        Interceptor[] interceptor = {new PaginationInterceptor()};
        sqlSessionFactory.setPlugins(interceptor); //分頁插件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            //自動(dòng)掃描Mapping.xml文件
            sqlSessionFactory.setMapperLocations(resolver.getResources(mapperLocations));
            sqlSessionFactory.setConfigLocation(resolver.getResource(configLocation));
            sqlSessionFactory.setTypeAliasesPackage(typeAliasesPackage);
            return sqlSessionFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //    MyBatis 動(dòng)態(tài)掃描
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        log.info("初始化MapperScannerConfigurer");
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        String basePackage = "com.ason.db.mapper";
        mapperScannerConfigurer.setBasePackage(basePackage);
        return mapperScannerConfigurer;
    }

    //    配置事務(wù)管理
    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier(value = "basisDataSource")DruidDataSource dataSource) {
        log.info("初始化DataSourceTransactionManager");
        return new DataSourceTransactionManager(dataSource);
    }
}
6、mybatis-sqlconfig.xml





    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

7、具體使用

因?yàn)槭菍?shù)據(jù)庫的配置多帶帶作為一個(gè)模塊的,所以我的rms-service微服務(wù)想進(jìn)行數(shù)據(jù)操作,需依賴db-mysql模塊:


    com.ason
    db-mysql
    0.0.1-SNAPSHOT

同時(shí),還需要讀取數(shù)據(jù)配置的屬性,上面我的配置是放在 ./config-server/config-repo/data-dev.yml 下,所以在rms-service微服務(wù)下的 bootstrap.yml 配置文件中,需要指定配置中心服務(wù)的地址以及配置文件的name和profile:

spring:
  # 配置中心服務(wù)的地址
  cloud:
    config:
      name: data
      profile: ${spring.profiles.active} # 要讀取的配置文件profile屬性
#      uri: http://127.0.0.1:7001
      #label: ${spring.profiles.active}
      discovery:
        enabled: true                                 # 默認(rèn)false,設(shè)為true表示使用注冊(cè)中心中的configserver配置而不自己配置configserver的uri
        serviceId: config-server
  profiles:
    active: dev

在 rms-service 的 RmsUserController 下,添加:

/**
 * 查詢單個(gè)用戶
 */
@GetMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
public String findUserById(@PathVariable("id") Integer id) throws Exception {
    return ResultBody.success(rmsUserService.selectUserById(id));
}

啟動(dòng)項(xiàng)目,訪問 http://localhost:8888/rms/user/6

訪問druid后臺(tái):http://localhost:8888/druid,顯示登錄界面,輸入配置文件中設(shè)置的賬號(hào)密碼:

至此,已完成Druid + mybatis-plus獨(dú)立數(shù)據(jù)操作模塊的整合
詳情請(qǐng)看github地址:https://github.com/5-Ason/aso...

相關(guān)文章:
【springboot系列】springboot整合獨(dú)立模塊 redis 做緩存

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/70596.html

相關(guān)文章

  • 【技術(shù)雜談】springcloud微服務(wù)之?dāng)?shù)據(jù)操作獨(dú)立模塊

    摘要:而在這個(gè)微服務(wù)下,同樣需要進(jìn)行數(shù)據(jù)操作,我不可能還要在下再一次進(jìn)行集成,這樣大大的增加了代碼量。其次,是將有關(guān)數(shù)據(jù)操作的都單獨(dú)部署成一個(gè)模塊,比如我集成的模塊,集成的模塊,使用作為內(nèi)存緩存模塊。 前言 相對(duì)于 spring 對(duì) mybatis 以及 redis 等的整合所需要的各種配置文件,在 springboot 下,已經(jīng)大大的簡化了,你可能只是需要增加個(gè)依賴,加個(gè)注解,然后在配置文...

    tianyu 評(píng)論0 收藏0
  • springboot系列springboot整合獨(dú)立模塊 redis 做緩存

    摘要:至此,已完成整合獨(dú)立模塊做緩存詳情請(qǐng)看地址相關(guān)文章系列整合獨(dú)立模塊 項(xiàng)目github地址:https://github.com/5-Ason/aso...具體可看 ./db/db-redis 和 ./db/db-cache 兩個(gè)模塊 // TODO 在整合redis之前需要先本地配置好redis環(huán)境,遲點(diǎn)有時(shí)間補(bǔ)一下linux下下載安裝配置redis 本文主要實(shí)現(xiàn)的是對(duì)數(shù)據(jù)操作進(jìn)行獨(dú)立...

    qianfeng 評(píng)論0 收藏0
  • springboot系列springboot整合獨(dú)立模塊 redis 做緩存

    摘要:至此,已完成整合獨(dú)立模塊做緩存詳情請(qǐng)看地址相關(guān)文章系列整合獨(dú)立模塊 項(xiàng)目github地址:https://github.com/5-Ason/aso...具體可看 ./db/db-redis 和 ./db/db-cache 兩個(gè)模塊 // TODO 在整合redis之前需要先本地配置好redis環(huán)境,遲點(diǎn)有時(shí)間補(bǔ)一下linux下下載安裝配置redis 本文主要實(shí)現(xiàn)的是對(duì)數(shù)據(jù)操作進(jìn)行獨(dú)立...

    Jokcy 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<