SpringBoot + Druid数据源 Mysql数据库 通用Mapper 等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39338799/article/details/82496350

springboot项目使用自己配置Druid数据源,mysql数据库,通用mapper插件,pagehelper分页,

DruidConfiguration.class
 



import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
import com.test.test.test.common.util.EncryptionPropertyUtil;

@Configuration
@MapperScan(basePackages = {"com.test.test.test.persist.mapper"}, sqlSessionFactoryRef = "mybatisSqlSessionFactory")
public class DruidConfiguration {

	@Value("${druid.driver}")
	private String driver;
	
    @Value("${druid.jdbc_url}")
    private String url;
    
    @Value("${druid.jdbc_username}")
    private String userName;
    
    @Value("${druid.jdbc_password}")
    private String passWord;
    
    @Value("${druid.validationQuery}")
    private String validationQuery;
    
    @Value("${druid.isEncrypt}")
    private boolean isEncrypt;

    private static Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);
    
	@Bean(name = "mybatisDataSource")
	public DataSource getDataSource() throws SQLException {	
		return createDataSource();
	}

	private DataSource createDataSource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		if(isEncrypt){
			try{
				dataSource.setUsername(EncryptionPropertyUtil.decryptProperty(userName));
				dataSource.setPassword(EncryptionPropertyUtil.decryptProperty(passWord));
			}catch(Exception e ){
				e.printStackTrace();
				logger.error("DruidConfiguration createDataSource, decryptProperty userName or password error,userName=[{}], passWord=[{}]", userName, passWord, e);
			}
		}else{
			dataSource.setUsername(userName);
			dataSource.setPassword(passWord);
		}
		
		dataSource.setInitialSize(10);
		dataSource.setMinIdle(0);
		dataSource.setMaxActive(20);
		dataSource.setMaxWait(60000);
		
		dataSource.setTimeBetweenEvictionRunsMillis(60000);
		dataSource.setMinEvictableIdleTimeMillis(25200000);
		
		dataSource.setValidationQuery(validationQuery);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(1800);
        dataSource.setLogAbandoned(true);
        dataSource.setFilters("stat");
		return dataSource;
	}
	
	@Bean(name = "mybatisTransactionManager")
    public DataSourceTransactionManager mybatisTransactionManager(@Qualifier("mybatisDataSource") DataSource dataSource) throws SQLException {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "mybatisSqlSessionFactory")
    public SqlSessionFactory mybatisSqlSessionFactory(@Qualifier("mybatisDataSource") DataSource mybatisDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(mybatisDataSource);
        
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/*Mapper.xml"));
        
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLazyLoadingEnabled(false);
        configuration.setUseGeneratedKeys(true);
        configuration.setDefaultStatementTimeout(30);
        configuration.setLogImpl(Slf4jImpl.class);
        sessionFactory.setConfiguration(configuration);
        
        return sessionFactory.getObject();

    }
}

application-local.xml

server:
  servlet:
    context-path: /test
  port: 7002
  
 

spring: 
  mvc: 
    view: 
      prefix: /WEB-INF/views/
      suffix: .jsp
  messages: 
    basename: i18n/messages
    encoding: UTF-8 

  redis:
    database: 6
    host: 127.0.0.1
    port: 6379
    password: asdf
    jedis:
      pool:
        max-active: 100
        max-wait: 10000
        max-idle: 8
        min-idle: 0
    timeout: 5000 
    
    
############ env  ###########
png_user_username: admin
png_user_password: EA9B568D06C8E2AA32C1DAD99FBAFBB0790603AB18C1F6ACEDE84EB93C61FBAF039960C72DEBBBB014E6FDE7627DD7FB3F81C66A1EA79C9E84360BE8255A078D

 ########## db ###############
druid: 
  driver: com.mysql.jdbc.Driver
  jdbc_url: jdbc:mysql://127.0.0.1:3306/test_test_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
  jdbc_username: 69A95B102D641CF2
  jdbc_password: 4359E07060E57855C00EEA2DF80F1411
  validationQuery: select 1
  isEncrypt: true
  

parent pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test.test</groupId>
  <artifactId>test-test</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.14.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
  </parent>
  
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<project.version>1.0.0-SNAPSHOT</project.version>
		<sps.security.version>1.0.0-SNAPSHOT</sps.security.version>
		<logstash.logback.encoder.version>4.7</logstash.logback.encoder.version>
		<standard-version>1.1.2</standard-version>
		<kaptcha-version>0.0.9</kaptcha-version>
		<mybatis.spring.boot.version>1.2.2</mybatis.spring.boot.version>
		<mapper.spring.boot.version>1.1.7</mapper.spring.boot.version>
		<pagehelper.spring.boot.version>1.2.3</pagehelper.spring.boot.version>
		<alibaba.druid.version>1.1.0</alibaba.druid.version>
		<fastJson.version>1.2.8</fastJson.version>
		<servlet.api.version>2.5</servlet.api.version>
  </properties>
  
  <dependencies>
  	<!--logstash-logback -->
	<dependency>
		<groupId>net.logstash.logback</groupId>
		<artifactId>logstash-logback-encoder</artifactId>
		<version>${logstash.logback.encoder.version}</version>
	</dependency>
	<!--test -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
  </dependencies>
	
  <modules>
   
    <module>test-test-common</module>
    <module>test-test-core</module>
    <module>test-test-persist</module>
  </modules>
  
</project>

common pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.test.test</groupId>
    <artifactId>test-test</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <artifactId>test-test-common</artifactId>
  
  <dependencies>
  	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-redis</artifactId>
	</dependency>
 	<dependency>
		<groupId>joda-time</groupId>
		<artifactId>joda-time</artifactId>
	</dependency>
	<!-- Json -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>${fastJson.version}</version>
	</dependency>
 	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>${servlet.api.version}</version>
	</dependency>
	
	<dependency>
		<groupId>com.shiji.png</groupId>
		<artifactId>sps-security</artifactId>
		<version>${sps.security.version}</version>
	</dependency>
  </dependencies>
</project>

core pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.test.test</groupId>
    <artifactId>test-test</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <artifactId>test-test-core</artifactId>
  <packaging>jar</packaging>
  <properties>
	<profiles.dir>src/profiles</profiles.dir>
	<maven_deploy_skip>false</maven_deploy_skip>
  </properties>  
  <dependencies>
    <dependency>
		<groupId>com.test.test</groupId>
		<artifactId>test-test-common</artifactId>
		<version>${project.version}</version>
		<exclusions>
			<exclusion>
				<artifactId>servlet-api</artifactId>
				<groupId>javax.servlet</groupId>
			</exclusion>
		</exclusions>
    </dependency>
     <dependency>
		<groupId>com.test.test</groupId>
		<artifactId>test-test-persist</artifactId>
		<version>${project.version}</version>
	</dependency> 
   	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<!--  kaptcha-->
	<dependency>
		<groupId>com.github.axet</groupId>
		<artifactId>kaptcha</artifactId>
		<version>${kaptcha-version}</version>
	</dependency>
	
  </dependencies>
  
   <build>
<!--     <finalName>test-test-web</finalName> -->

<!-- 	<resources> -->
<!-- 		<resource> -->
<!--                <directory>src/main/webapp</directory> -->
<!--                <targetPath>META-INF/resources</targetPath> -->
<!--                <includes> -->
<!--                    <include>**/**</include> -->
<!--                </includes> -->
<!--            </resource> -->
<!-- 		<resource> -->
<!-- 			<directory>src/main/resources</directory> -->
<!-- 		</resource> -->
<!-- 	</resources> -->
	
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
               <version>1.4.2.RELEASE</version>
               <executions>
                   <execution>
                       <goals>
                           <goal>repackage</goal>
                       </goals>
                   </execution>
               </executions>
               <configuration>
               <mainClass>com.test.test.test.Application</mainClass>
                   <executable>true</executable>
               </configuration>
           </plugin>
           <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-eclipse-plugin</artifactId>
			<configuration>
				<projectNameTemplate>[artifactId]</projectNameTemplate>
				<wtpmanifest>true</wtpmanifest>
				<wtpapplicationxml>true</wtpapplicationxml>
				<wtpversion>2.0</wtpversion>
				<manifest>${basedir}/src/main/resources/META-INF/MANIFEST.MF</manifest>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-deploy-plugin</artifactId>
			<configuration>
				<skip>true</skip>
			</configuration>
		</plugin>
       </plugins>
   </build>
   
   <profiles>
	<profile>
		<id>local</id>
		<build>
			<resources>
				<resource>
					<directory>${profiles.dir}/local</directory>
				</resource>
			</resources>
		</build>
	</profile>
	<profile>
		<id>dev</id>
		<build>
			<resources>
				<resource>
					<directory>${profiles.dir}/dev</directory>
				</resource>
			</resources>
		</build>
	</profile>
	<profile>
		<id>beta</id>
		<build>
			<resources>
				<resource>
					<directory>${profiles.dir}/beta</directory>
				</resource>
			</resources>
		</build>
	</profile>
	<profile>
		<id>betab</id>
		<build>
			<resources>
				<resource>
					<directory>${profiles.dir}/betab</directory>
				</resource>
			</resources>
		</build>
	</profile>
	<profile>
		<id>betac</id>
		<build>
			<resources>
				<resource>
					<directory>${profiles.dir}/betac</directory>
				</resource>
			</resources>
		</build>
	</profile>
	<profile>
		<id>betad</id>
		<build>
			<resources>
				<resource>
					<directory>${profiles.dir}/betad</directory>
				</resource>
			</resources>
		</build>
	</profile>
	<profile>
		<id>product</id>
		<build>
			<resources>
				<resource>
					<directory>${profiles.dir}/prod</directory>
				</resource>
			</resources>
		</build>
	</profile>
 </profiles>
</project>

persist pom.xml

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.test.test</groupId>
		<artifactId>test-test</artifactId>
		<version>1.0.0-SNAPSHOT</version>
	</parent>
	<artifactId>test-test-persist</artifactId>

	<dependencies>
		<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis.spring.boot.version}</version>
		</dependency>
		<!--mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>${mapper.spring.boot.version}</version>
		</dependency>

		<!--pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>${pagehelper.spring.boot.version}</version>
		</dependency>

		<!--mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

		<!-- Database connection pool -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>${alibaba.druid.version}</version>
		</dependency>
		<!-- End -->

	</dependencies>
</project>

猜你喜欢

转载自blog.csdn.net/qq_39338799/article/details/82496350