|
@@ -3,57 +3,127 @@ package com.gxzc.zen.config
|
|
|
import com.alibaba.druid.pool.DruidDataSource
|
|
|
import com.alibaba.druid.support.http.StatViewServlet
|
|
|
import com.alibaba.druid.support.http.WebStatFilter
|
|
|
+import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean
|
|
|
+import com.baomidou.mybatisplus.spring.boot.starter.SpringBootVFS
|
|
|
+import org.mybatis.spring.annotation.MapperScan
|
|
|
import org.slf4j.LoggerFactory
|
|
|
import org.springframework.beans.factory.annotation.Autowired
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties
|
|
|
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
|
|
|
+import org.springframework.core.annotation.Order
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver
|
|
|
import java.sql.SQLException
|
|
|
import javax.sql.DataSource
|
|
|
|
|
|
|
|
|
+@Order(1)
|
|
|
@Configuration
|
|
|
-class SysDruidConfig:DruidConfig<SysDruidProperties>()
|
|
|
+@ConfigurationProperties(prefix = "druid.datasource-sys")
|
|
|
+class SysDruidConfig:DruidConfig()
|
|
|
|
|
|
+@Order(2)
|
|
|
+@Configuration
|
|
|
+@ConfigurationProperties(prefix = "mybatis-plus")
|
|
|
+@MapperScan(basePackages = ["com.gxzc.zen"])
|
|
|
+class SysMyBatisPlusConfig:MyBatisPlusConfig<SysDruidConfig>()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------druid数据源bean注册 */
|
|
|
-open class DruidConfig<T:DruidProperties> {
|
|
|
+open class DruidConfig {
|
|
|
+ var url:String?=null
|
|
|
+ var username:String?=null
|
|
|
+ var password:String?=null
|
|
|
+ var filters:String?=null
|
|
|
+
|
|
|
+ var driverClassName:String?="com.mysql.jdbc.Driver"
|
|
|
+ var initialSize:Int?=5
|
|
|
+ var minIdle:Int?=5
|
|
|
+ var maxActive:Int?=20
|
|
|
+ var maxWait:Long?=60000
|
|
|
+
|
|
|
+ var timeBetweenEvictionRunsMillis:Long?=null
|
|
|
+ var minEvictableIdleTimeMillis:Long?=null
|
|
|
+ var validationQuery:String?=null
|
|
|
+ var testWhileIdle:Boolean?=null
|
|
|
+ var testOnBorrow:Boolean?=null
|
|
|
+ var testOnReturn:Boolean?=null
|
|
|
+
|
|
|
private val logger= LoggerFactory.getLogger(javaClass)
|
|
|
|
|
|
- @Autowired
|
|
|
- private val config:T?=null
|
|
|
+ private var datasource:DruidDataSource?=null
|
|
|
|
|
|
// private val name=nm
|
|
|
|
|
|
@Bean
|
|
|
open fun druidDataSource(): DataSource {
|
|
|
- val datasource = DruidDataSource()
|
|
|
- datasource.url = config?.url
|
|
|
- datasource.username = config?.username
|
|
|
- datasource.password = config?.password
|
|
|
- datasource.driverClassName = config?.driverClassName
|
|
|
- datasource.initialSize = config?.initialSize!!
|
|
|
- datasource.minIdle = config?.minIdle!!
|
|
|
- datasource.maxActive = config?.maxActive!!
|
|
|
- datasource.maxWait = config?.maxWait!!
|
|
|
- datasource.timeBetweenEvictionRunsMillis = config?.timeBetweenEvictionRunsMillis!!
|
|
|
- datasource.minEvictableIdleTimeMillis = config?.minEvictableIdleTimeMillis!!
|
|
|
- datasource.validationQuery = config?.validationQuery!!
|
|
|
- datasource.isTestWhileIdle = config?.testWhileIdle!!
|
|
|
- datasource.isTestOnBorrow = config?.testOnBorrow!!
|
|
|
- datasource.isTestOnReturn = config?.testOnReturn!!
|
|
|
+ datasource = DruidDataSource()
|
|
|
+ datasource?.url = url
|
|
|
+ datasource?.username = username
|
|
|
+ datasource?.password = password
|
|
|
+ datasource?.driverClassName = driverClassName
|
|
|
+ datasource?.initialSize = initialSize!!
|
|
|
+ datasource?.minIdle = minIdle!!
|
|
|
+ datasource?.maxActive = maxActive!!
|
|
|
+ datasource?.maxWait = maxWait!!
|
|
|
+ datasource?.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis!!
|
|
|
+ datasource?.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis!!
|
|
|
+ datasource?.validationQuery = validationQuery!!
|
|
|
+ datasource?.isTestWhileIdle = testWhileIdle!!
|
|
|
+ datasource?.isTestOnBorrow = testOnBorrow!!
|
|
|
+ datasource?.isTestOnReturn = testOnReturn!!
|
|
|
try {
|
|
|
- datasource.setFilters(config?.filters)
|
|
|
+ datasource?.setFilters(filters)
|
|
|
} catch (e: SQLException) {
|
|
|
logger.error("druid configuration initialization filter", e)
|
|
|
}
|
|
|
|
|
|
- return datasource
|
|
|
+ return datasource!!
|
|
|
+ }
|
|
|
+
|
|
|
+ fun getDatasource():DruidDataSource{
|
|
|
+ return datasource!!
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+open class MyBatisPlusConfig<T:DruidConfig>{
|
|
|
+ var mapperLocations:String?=null
|
|
|
+ var typeAliasesPackage:String?=null
|
|
|
+ var globalConfigs:Map<String,Any>?=null
|
|
|
+ var configurations:Map<String,Any>?=null
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private val datasource:T?=null
|
|
|
+ private val resolver=PathMatchingResourcePatternResolver()
|
|
|
+ private var sqlSessionFactory:MybatisSqlSessionFactoryBean?=null
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
|
|
|
+ * 配置文件和mybatis-boot的配置文件同步
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ open fun mybatisSqlSessionFactoryBean(): MybatisSqlSessionFactoryBean {
|
|
|
+ sqlSessionFactory = MybatisSqlSessionFactoryBean()
|
|
|
+ sqlSessionFactory?.setDataSource(datasource?.getDatasource())
|
|
|
+ sqlSessionFactory?.vfs = SpringBootVFS::class.java
|
|
|
+ sqlSessionFactory?.setTypeAliasesPackage(typeAliasesPackage)
|
|
|
+ val resources = resolver.getResources(mapperLocations)
|
|
|
+ sqlSessionFactory?.setMapperLocations(resources)
|
|
|
+
|
|
|
+ val conftion= org.apache.ibatis.session.Configuration()
|
|
|
+ conftion.isMapUnderscoreToCamelCase=configurations?.get("map-underscore-to-camel-case") as Boolean
|
|
|
+ conftion.isCacheEnabled=configurations?.get("cache-enabled") as Boolean
|
|
|
+ conftion.isLazyLoadingEnabled=configurations?.get("lazyLoadingEnabled") as Boolean
|
|
|
+ conftion.isMultipleResultSetsEnabled=configurations?.get("multipleResultSetsEnabled") as Boolean
|
|
|
+
|
|
|
+ sqlSessionFactory?.setConfiguration(conftion)
|
|
|
+ return sqlSessionFactory!!
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -62,26 +132,27 @@ open class DruidConfig<T:DruidProperties> {
|
|
|
|
|
|
/*-----------------配置druid显示界面 */
|
|
|
@Configuration
|
|
|
+@ConfigurationProperties(prefix = "druid.view")
|
|
|
class DruidViewConfig{
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private val config:DruidViewProperties?=null
|
|
|
+ var username:String?=null
|
|
|
+ var password:String?=null
|
|
|
+ var slowsql:String?=null
|
|
|
|
|
|
@Bean
|
|
|
fun druidServlet(): ServletRegistrationBean {
|
|
|
-// if(!config?.enable!!)return throw Exception()
|
|
|
+// if(!enable!!)return throw Exception()
|
|
|
val reg = ServletRegistrationBean()
|
|
|
reg.setServlet(StatViewServlet())
|
|
|
reg.addUrlMappings("/druid/*")
|
|
|
- reg.addInitParameter("loginUsername", config?.username)
|
|
|
- reg.addInitParameter("loginPassword", config?.password)
|
|
|
- reg.addInitParameter("logSlowSql", config?.slowsql)
|
|
|
+ reg.addInitParameter("loginUsername", username)
|
|
|
+ reg.addInitParameter("loginPassword", password)
|
|
|
+ reg.addInitParameter("logSlowSql", slowsql)
|
|
|
return reg
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
fun filterRegistrationBean(): FilterRegistrationBean {
|
|
|
-// if(!config?.enable!!)return throw Exception()
|
|
|
+// if(!enable!!)return throw Exception()
|
|
|
val filterRegistrationBean = FilterRegistrationBean()
|
|
|
filterRegistrationBean.filter = WebStatFilter()
|
|
|
filterRegistrationBean.addUrlPatterns("/*")
|