【问题标题】:Issue on implementing jdbc dao support in spring application在 Spring 应用程序中实现 jdbc dao 支持的问题
【发布时间】:2014-08-03 17:38:13
【问题描述】:

我试图按照以下示例使用 jdbcdao 访问数据库: http://www.mkyong.com/spring/spring-jdbctemplate-jdbcdaosupport-examples/

userdao、userdaoimpl、daocontext和datacontext.xml如下:

DAOIMPL

public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {    

    /*Creates User */
    public void insertUser(User user){
        String sql = "INSERT INTO Users " +
            "(id, username, password,role) VALUES (?, ?, ?,?)";

        getJdbcTemplate().update(sql, new Object[] { user.getUserId(),
                user.getUserName(),user.getPassWord() 
        });

    }
}

道

import java.util.List;

import spring.web.models.User;

public interface UserDAO {
    public void insertUser(User user);

}

DAOCONTEXT.XML

<beans xmlns="http://www.springframework.org/schema/beans"
    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-4.0.xsd">

    <bean id="userDAO" class="spring.web.dao.impl.UserDAOImpl">
        <property name="primaryDataSource" ref="oracleDataSource" />
    </bean>

</beans>

数据上下文.XML

<?xml version="1.0"?>
<beans
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

    <bean  id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property value="oracle.jdbc.OracleDriver" name="driverClassName" />
        <property value="jdbc:oracle:thin:@192.168.72.68:1521:d2he"
            name="url" />
        <property value="aaryal_1" name="username" />
        <property value="oracle" name="password" />
    </bean>
</beans>

我遇到的错误如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAO' defined in class path resource [dao-context.xml]:
Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: 
Invalid property 'primaryDataSource' of bean class [spring.web.dao.impl.UserDAOImpl]: 
Bean property 'primaryDataSource' is not writable or has an invalid setter method.
 Does the parameter type of the setter match the return type of the getter?

请告诉我我错过了什么。

【问题讨论】:

标签: spring-mvc jdbc dao


【解决方案1】:

您需要在 UserDAOImpl 类中使用 setPrimaryDataSource 方法。错误说明了一切。它期望在您的班级中有一个名为 primaryDataSource 的属性,但找不到它。因此出现错误。

您需要这样做:

private DataSource dataSource;

public void setPrimaryDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

编辑:

如果通过JdbcDaoSupport的API,setDataSource方法已经存在。因此,要解决您的错误,您可以执行上述操作,也可以简单地将您的 DataSource bean 名称重命名为 dataSource

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 2020-05-12
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    相关资源
    最近更新 更多