Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'jt' of bean class [cn.itcast.a_jdbctemplate.UserDaoImpl]: Bean property 'jt' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
在家自学Spring,遇到Spring整合jdbc的异常问题:
UserDao.java
package cn.itcast.a_jdbctemplate;
import java.util.List;
import cn.itcast.bean.User;
public interface UserDao {
void save(User u);
void delete(Integer id);
void update(User u);
User getById(Integer id);
int getTotalCount();
List<User> getAll();
}
UserDaoImpl.java如下:
package cn.itcast.a_jdbctemplate;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import cn.itcast.bean.User;
public class UserDaoImpl implements UserDao{
private JdbcTemplate jt;
@Override
public void save(User u) {
String sql ="insert into t_user values(null,?)";
jt.update(sql,u.getName());
}
@Override
public void delete(Integer id) {
String sql="delete from t_user where id=?";
jt.update(sql,id);
}
@Override
public void update(User u) {
String sql = "update t_user set name=? where id=?";
jt.update(sql,u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql = "select * from t_user where id = ? ";
return jt.queryForObject(sql,new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from t_user ";
Integer count = jt.queryForObject(sql, Integer.class);
return count;
}
@Override
public List<User> getAll() {
String sql = "select * from t_user";
List<User> list = jt.query(sql, new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}});
return list;
}
}
User.java如下:
package cn.itcast.bean;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
applicationContext.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!-- 将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value ="jdbc:mysql:///hibernate_32"></property>
<property name="driverClass" value ="com.mysql.jdbc.Driver"></property>
<property name="user" value ="root"></property>
<property name="password" value ="jack1220"></property>
</bean>
<!-- 将JDBCTemplate放入spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 将UserDao放入spring容器 -->
<bean name="userDao" class="cn.itcast.a_jdbctemplate.UserDaoImpl">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>
测试类demo.java如下:
package cn.itcast.a_jdbctemplate;
import java.beans.PropertyVetoException;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import cn.itcast.bean.User;
//演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun1() throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///hibernate_32");
dataSource.setUser("root");
dataSource.setPassword("jack1220");
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(dataSource);
//2.书写sql,并执行
String sql ="insert into t_user values(null,'James')";
jt.update(sql);
}
@Test
public void fun2() throws Exception{
User u = new User();
u.setName("tommy");
ud.save(u);
}
@Test
public void fun4() throws Exception{
ud.delete(2);
}
@Test
public void fun5() throws Exception{
System.out.println(ud.getTotalCount());
}
}package cn.itcast.a_jdbctemplate;
import java.beans.PropertyVetoException;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import cn.itcast.bean.User;
//演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun1() throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///hibernate_32");
dataSource.setUser("root");
dataSource.setPassword("jack1220");
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(dataSource);
//2.书写sql,并执行
String sql ="insert into t_user values(null,'James')";
jt.update(sql);
}
@Test
public void fun2() throws Exception{
User u = new User();
u.setName("tommy");
ud.save(u);
}
@Test
public void fun4() throws Exception{
ud.delete(2);
}
@Test
public void fun5() throws Exception{
System.out.println(ud.getTotalCount());
}
}
报错:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'jt' of bean class [cn.itcast.a_jdbctemplate.UserDaoImpl]: Bean property 'jt' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
改进方法:在UserDaoImpl中加入jdbcTemplate jt的set方法,测试就成功过了