1. 程式人生 > >Spring +MyBatis 整合訪問資料庫出現錯誤

Spring +MyBatis 整合訪問資料庫出現錯誤

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Exception in thread "main"org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbcppool' defined in class path resource [spring_mybatis.xml]: Error setting property values; nested exception isorg.springframework.beans.NotWritablePropertyException: Invalid property 'uname' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'uname' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?


 這裡是檔案叫 spring_mybatis.xml檔案,是整合過程中的訪問資料以及註冊sqlSessionFactory,註冊事務等

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="peopertyPlcaceHolder"

 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

 <property name="location" value="classpath:db_mysql.properties"/>

 </bean>

 <!-- 註冊連線池 配置引數 -->

 <bean id="dbcppool" class="org.apache.commons.dbcp.BasicDataSource"

 destroy-method="close">

 <property name="driverClassName" value="${driver}"/>

 <property name="url" value="${url}"/>

 <property name="username" value="${username}"/>

 <property name="password" value="${passw}"/>

    

 <!-- 伸縮性配置 -->

 <property name="initialSize" value="${initialSize}"/>

 <property name="maxActive" value="${maxActive}"/>

 <property name="maxIdle" value="${maxIdle}"/>

 <property name="minIdle" value="${minIdle}"/>

 <property name="maxWait" value="${maxWait}"/>

 

 </bean>

 <!-- sqlSessionFactory物件 -->

<bean id="sqlSessionFactory" 

      class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource" ref="dbcppool"/>

      <property name="configLocation" value="classpath:mybatis-config.xml"/>

</bean>

<!-- 註冊一個建立Mapper物件的bean類 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="dao"/>

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

</bean>

<!-- 配置事務管理 -->

</beans>


driver=com.mysql.jdbc.Driver

url=jdbc\:mysql\://localhost\:3306/jsd1790

username=root

upass=qwe

#dbcp pool parameters

initialSize=2

maxActive=20

maxIdle=10

minIdle=2

maxWait=15000

通過多次試驗

Dbcp 的方法中 dataSource的屬性是固定的

通過測試  在我們使用dbcp產品的時候應該設定和人家已經設定好的引數一致

import java.sql.Connection;

import java.sql.SQLException;

 

import org.apache.commons.dbcp.BasicDataSource;

 

public class DBCP_demo {

 

public static void main(String[] args) throws SQLException {

/*

 * 實現DBCP資料庫連線池

 */

//1.建立資料庫連線池物件(緩衝區物件)

BasicDataSource dataSource = new BasicDataSource();

//2.設定JDBC的四大引數

dataSource.setDriverClassName("com.mysql.jdbc.Driver");

dataSource.setUrl("jdbc:mysql://localhost:3306/class90");

dataSource.setUsername("root");

dataSource.setPassword("111");

//3.設定池引數(可選,資料庫連線池物件都有預設引數值)

dataSource.setInitialSize(10);//連線池的初始化連線物件10個

dataSource.setMaxActive(10);//最大並打訪問數10個

dataSource.setMaxIdle(5);//最大空閒數5個

dataSource.setMinIdle(2);//最小空閒數2個

dataSource.setMaxWait(300);//最大等待時間300毫秒

//4.從連線池中獲取連線物件

Connection con = dataSource.getConnection();

/*

 * 5.使用連線物件完成jdbc操作:

 * jdbc:mysql://localhost:3306/class90, 
[email protected]
, MySQL-AB JDBC Driver */ System.out.println(con); //6.歸還連線物件到池中 con.close(); } }


<!--修改  這裡dbcppool的四大引數的名字應該是固定的 name=”” 無論你的.properties檔案裡面的引數名是什麼樣都沒關係,只和你的dbcp的四大引數有關-->

driverClassName

Url

Username

password

<bean id="dbcppool" class="org.apache.commons.dbcp.BasicDataSource"

 destroy-method="close">

<property name="driverClassName" value="${driver}"/>

<property name="url" value="${url}"/>

<property name="username" value="${username}"/>

<property name="password" value="${passw}"/>