4

我有一个非常简单的要求,它变得复杂了,我花了一天的时间来解决它,但没有任何运气。我有一个名为的属性文件jdbc.properties,其中包含数据库连接详细信息。我需要使用属性文件中的值创建数据源连接。现在没有传递属性值,导致数据库连接错误消息。如果我对 bean 中的属性值进行硬编码,它就可以工作。我的弹簧配置文件myBatis.DataSource.config.xml看起来像这样。

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="newProperty"    
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-  
    init="true">
        <property name="locations" value="jdbc.properties.${env}"/>
    </bean>
    <bean id="newDataSource"  class="org.apache.commons.dbcp.BasicDataSource" depends-
    on="newProperty" destroy-method="close">
        <property name="username" value="${DBUSER}" />
        <property name="password" value="${DBPASSWORD}" />
        <property name="url" value="${DBURL}" />
        <property name="driverClassName" value="${DRIVER}" />
        <property name="poolPreparedStatements" value="false" />
        <property name="defaultAutoCommit" value="false" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
        <property name="defaultTransactionIsolation" value="2" />
        <property name="timeBetweenEvictionRunsMillis" value="10000" />
    </bean>
    <bean id="sqlSessionFactory"   class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation"   
      value="com/automation/config/oneValidation-config.xml" />
        <property name="dataSource" ref="newDataSource" />
    </bean>
    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        <property name="basePackage" value="com.automation.config" />
    </bean>
</beans>

的值${env}作为系统属性传递给类。该类的代码如下:

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MybatisTest {


    public static void main(String[] args) {
        MybatisTest mybatisTest = new MybatisTest();
        mybatisTest.testSelectQuery();
    }

    public void testSelectQuery(){

        String resource = "oneValidation-config.xml";
        SqlSession session = null;
        try {
            ApplicationContext  context = new ClassPathXmlApplicationContext("myBatis.DataSource.config.xml");
            SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)context.getBean("sqlSessionFactory");
            session = sqlSessionFactory.openSession(ExecutorType.BATCH);
            System.out.println("Test" + 
                   session.selectOne("com.automation.config.PostingMapper.
                   countByExample",null));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(session != null)
            session.close();
        }
    }
}

我得到的错误如下所示,是因为没有从属性文件 jdbc.properties 中检索${DBUSER},字段:${DBPASSWORD}

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause:                       
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC 
Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot 
create PoolableConnectionFactory (JZ00L: Login failed.  Examine the SQLWarnings chained 
to this exception for the reason(s).)
4

2 回答 2

3

我也遇到过这个。在这里的 mybatis 文档中,我发现了为什么会发生这种情况。

从源代码:“注意 sqlSessionFactoryBean 和 sqlSessionTemplateBean 属性是 MyBatis-Spring 1.0.2 之前唯一可用的选项,但鉴于MapperScannerConfigurer 在启动过程中较早运行,因此 PropertyPlaceholderConfigurer经常出现错误。为此,属性已被弃用,并且推荐使用新属性 sqlSessionFactoryBeanName 和 sqlSessionTemplateBeanName。"

尝试更改您的 MapperScannerConfigurer bean

<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    <property name="basePackage" value="com.automation.config" />  
</bean>

<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <property name="basePackage" value="com.automation.config" />  
</bean>
于 2015-06-19T11:06:40.253 回答
0

尝试这个:

<property name="locations" value="classpath:/yourFolderName/jdbc.properties"/>
于 2013-08-28T00:07:16.113 回答