springboot1.5.16整合jdk1.6_使用c3p0连接池_使用pagehelper分页插件搭建项目

在网上找了很多,一直在错了又试,试了又错,最终终于找到一个可以用的,而且启动不报错的版本,现在贴上来
pom 因为用到了webservice,所以还有webservice的依赖,只看自己用的就可以了



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.16.RELEASE
         
    
    com.are.yuecloud.cpsp
    ms_spa_service
    0.0.1-SNAPSHOT
    ms_spa
    sp采集服务

    
        
        1.6
        7.0.59
        4.2.1.RELEASE
        

    


    

        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.1.0
            
                
                    org.mybatis.spring.boot
                    mybatis-spring-boot-starter
                
            
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            mysql
            mysql-connector-java
        
        
        
            c3p0
            c3p0
            0.9.1.2
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.apache.tomcat
            tomcat-juli
            ${tomcat.version}
        
        
        
        
            org.apache.axis2
            axis2-transport-http
            1.7.7
        
        
            org.apache.axis2
            axis2-transport-local
            1.7.7
        
        
            org.apache.axis2
            axis2-adb
            1.7.7
        
        
        
            org.apache.axis2
            axis2-xmlbeans
            1.7.7
        
  

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.6
                    1.6
                    utf8
                
            

            
                org.springframework.boot
                spring-boot-maven-plugin
            
			
            
                org.jvnet.jaxb2.maven2
                maven-jaxb2-plugin
                0.13.3
                
                    
                        
                            generate
                        
                    
                
                
                    WSDL
                    com.htairlines
                    ${basedir}/src/main/java
                    
                        
                            
                                
                                ${basedir}/src/main/resources/wsdl
                                
                                
                                    *.wsdl
                                
                            
                        
                    
                
            
            
        
    



下面放application.yml

server:
  port: 21081
  servlet:
    context-path: /cpsp/spa
spring:
  application:
    name: msspa
  profiles:
    active: sy
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

logging:
  level:
    root: info

application-sy.yml

spring:
  datasource:
    type: com.mchange.v2.c3p0.ComboPooledDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/airdynamic
    username: root
    password: root

mapper:
  package:
    path: com.are.yuecloud.cpsp.spa.module.mapper #mapper的接口的包名
  xml:
    config:
      path: mybatis/mapper/xml/*.xml #mapper的xml的路径,注意用/代替.

logging:
  level:
    root: debug
    com.are.yuecloud.app.yuef.module.mapper: DEBUG

连接池配置类

package com.are.yuecloud.cpsp.spa.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @ClassName DataSource
 * @Description:
 * @Author: YangJunyi
 * @Copyright: Copyright (c) 2019 All rights reserved.
 * @Company: ARE Corporation, China, Ltd.
 * @Date 2020/1/2
 **/
@SpringBootConfiguration
public class DataSourceConfiguration {
	@Value("${spring.datasource.driver-class-name}")
	private String jdbcDriver;
	@Value("${spring.datasource.url}")
	private String jdbcUrl;
	@Value("${spring.datasource.username}")
	private String jdbcUser;
	@Value("${spring.datasource.password}")
	private String jdbcPassword;

	@Bean
	public DataSource createDataSource() throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();

		dataSource.setDriverClass(jdbcDriver);
		dataSource.setJdbcUrl(jdbcUrl);
		dataSource.setUser(jdbcUser);
		dataSource.setPassword(jdbcPassword);
		// 关闭连接后不自动提交
		dataSource.setAutoCommitOnClose(false);

		return dataSource;
	}
}

sessionFactory配置类

package com.are.yuecloud.cpsp.spa.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @ClassName SessionFactoryConfiguration
 * @Description:
 * @Author: YangJunyi
 * @Copyright: Copyright (c) 2019 All rights reserved.
 * @Company: ARE Corporation, China, Ltd.
 * @Date 2020/1/2
 **/
@SpringBootConfiguration
public class SessionFactoryConfiguration {
	@Value("${mapper.xml.config.path}")
	private String mapperXMLConfigPath;
	@Value("${mapper.package.path}")
	private String mapperPackagePath;
	@Autowired
	private DataSource dataSource;

	@Bean
	public SqlSessionFactoryBean createSqlSessionFactory() throws IOException {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		String packageXMLConfigPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperXMLConfigPath;

		// 设置 mapper 对应的 XML 文件的路径
		sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageXMLConfigPath));
		// 设置数据源
		sqlSessionFactoryBean.setDataSource(dataSource);
		// 设置 mapper 接口所在的包
		sqlSessionFactoryBean.setTypeAliasesPackage(mapperPackagePath);

		return sqlSessionFactoryBean;
	}
}

最后在启动类上测试一下

        ConfigurableApplicationContext context = SpringApplication.run(SPAApplication.class, args);
        DataSource dataSource = context.getBean(DataSource.class);

        System.out.println(dataSource.getClass());
        System.out.println("========================================>");

最后在控制台看到c3p0连接池就可以了,说明配置有效

你可能感兴趣的:(springboot)