Spring Boot Mybatis 获取sqlSession 报空指针不知道哪错了,求各位大神帮忙看看

萌萌哒丶 发布于 2015/06/03 15:55
阅读 5K+
收藏 0

package com.eric.demo.config;

import javax.sql.DataSource;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableTransactionManagement
public class DataBaseConfiguration implements EnvironmentAware {

    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment,
                "spring.datasource.");
    }
    @Bean(destroyMethod="shutdown")
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setDataSourceClassName(propertyResolver
                .getProperty("dataSourceClassName"));
        
        config.addDataSourceProperty("serverName",
                propertyResolver.getProperty("serverName"));
        config.addDataSourceProperty("port",
                propertyResolver.getProperty("port"));
        config.addDataSourceProperty("databaseName",
                propertyResolver.getProperty("databaseName"));
        config.addDataSourceProperty("user",
                propertyResolver.getProperty("username"));
        config.addDataSourceProperty("password",
                propertyResolver.getProperty("password"));
        config.addDataSourceProperty("cachePrepStmts",
                propertyResolver.getProperty("cachePrepStmts"));
        config.addDataSourceProperty("prepStmtCacheSize",
                propertyResolver.getProperty("prepStmtsCacheSize"));
        config.addDataSourceProperty("prepStmtCacheSqlLimit",
                propertyResolver.getProperty("prepStmtCacheSqlLimit"));
        config.addDataSourceProperty("userServerPrepStmts",
                propertyResolver.getProperty("userServerPrepStmts"));
        
        config.setPoolName(propertyResolver.getProperty("poolName"));
        config.setReadOnly(Boolean.valueOf(propertyResolver
                .getProperty("readOnly")));
        config.setConnectionTestQuery(propertyResolver
                .getProperty("connectionTestQuery"));
        return new HikariDataSource(config);
    }

}

--------------------------------------------------------------------------------------------

package com.eric.demo.config;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ConditionalOnClass({ EnableTransactionManagement.class, EntityManager.class })
@AutoConfigureAfter({ DataBaseConfiguration.class })
@MapperScan("com.eric.demo.**.dao")
@ConditionalOnMissingBean(name = { "transactionManager", "sqlSessionFactory" })
public class MybatisConfiguration implements EnvironmentAware {

    private static Log logger = LogFactory.getLog(MybatisConfiguration.class);

    private RelaxedPropertyResolver propertyResolver;

    @Autowired
    private DataSource dataSource;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment,
                "Mybatis.");
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() {
        try {
            SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
            sessionFactory.setDataSource(dataSource);
            sessionFactory.setTypeAliasesPackage(propertyResolver
                    .getProperty("typeAliasesPackage"));
            sessionFactory
                    .setMapperLocations(new PathMatchingResourcePatternResolver()
                            .getResources(propertyResolver
                                    .getProperty("mapperLocations")));
            sessionFactory
                    .setConfigLocation(new DefaultResourceLoader()
                            .getResource(propertyResolver
                                    .getProperty("configLocation")));

            return sessionFactory.getObject();
        } catch (Exception e) {
            logger.warn("Could not confiure mybatis session factory");
            return null;
        }

    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}

---------------------------------------------------------------------------------------------------------

# SPRING PROFILES
spring:
    profiles:
       active:dev
       
    #Datasource specifics
    datasource:
            dataSourceClassName: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
            poolName: boot_db
            port: 3306
            serverName: 127.0.0.1
            databaseName: test
            username: root
            password: admin
            readOnly: false
            connectionTestQuery: SELECT 1
            cachaPrepStmts: true
            prepStmtCacheSize: 250
            prepStmtCacheSqlLimit: 2048
            userServerPrepStmts: true

-------------------------------------------------------------------------------------------------------------

Caused by: java.lang.NullPointerException
    at java.util.Hashtable.put(Hashtable.java:514)
    at com.zaxxer.hikari.HikariConfig.addDataSourceProperty(HikariConfig.java:309)
    at com.eric.demo.config.DataBaseConfiguration.dataSource(DataBaseConfiguration.java:32)
    at com.eric.demo.config.DataBaseConfiguration$$EnhancerBySpringCGLIB$$a59c08e6.CGLIB$dataSource$1(<generated>)
    at com.eric.demo.config.DataBaseConfiguration$$EnhancerBySpringCGLIB$$a59c08e6$$FastClassBySpringCGLIB$$c7bf710.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
    at com.eric.demo.config.DataBaseConfiguration$$EnhancerBySpringCGLIB$$a59c08e6.dataSource(<generated>)
    at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 69 more

加载中
返回顶部
顶部