日期:2014-05-16  浏览次数:20821 次

spring的jdbcTemplate 返回值为list






UserDaoImpl.java


package com.qieast.platform.admincp.dao.impl;

import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.qieast.platform.admincp.dao.IUserDao;
import com.qieast.platform.model.HomeMember;

public class UserDaoImpl implements IUserDao {
	public UserDaoImpl() {
		super();
	}
	
	 
	 
	private JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	

	@Override
	public boolean saveUser(HomeMember homeMember) {
		
		
		String sql = "insert into uchome_member(username,password,salt) values(?,?,?)";
		
		Object[] args = new Object[] { homeMember.getUsername(),homeMember.getPassword(),homeMember.getSalt() };
		
		int[] argTypes = new int[] { Types.VARCHAR ,Types.VARCHAR,Types.VARCHAR};
		jdbcTemplate.update(sql, args, argTypes);
		return true;
	}

	@Override
	public boolean deleteUser(HomeMember homeMember) {
	
		return false;
	}

	@Override
	public boolean updateUser(HomeMember homeMember) {
	
		return false;
	}

	@Override
	public List<HomeMember> findAllUser() {
		
		List<HomeMember> list = getJdbcTemplate().query("select * from uchome_member", new BeanPropertyRowMapper(HomeMember.class));
		System.out.println(list);
		return list;
		
	}

	@Override
	public HomeMember findUserById(int id) {
	
		return null;
	}



}
UserDaoImplTest.java



package com.qieast.platform.admincp.dao.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qieast.platform.admincp.dao.impl.UserDaoImpl;
import com.qieast.platform.model.HomeMember;

public class UserDaoImplTest {
	private BeanFactory beanFactory = null;
	private UserDaoImpl dao = null;

	@Before
	public void setUp() throws Exception {

		beanFactory = new ClassPathXmlApplicationContext(
				"file:WebContent/WEB-INF/applicationContext.xml");
			dao =  (UserDaoImpl) beanFactory.getBean("userDao");
	}

	@After
	public void tearDown() throws Exception {
		dao = null;
		beanFactory = null;
	}

	@Test
	public void saveUserTest() {

		HomeMember homeMember = new HomeMember();

		homeMember.setUsername(""+Math.random());
		homeMember.setPassword("password");
		homeMember.setSalt("Salt");
		dao.saveUser(homeMember);
	}
	@Test
	public void findAllTest(){
		dao.findAllUser();
	}
}

applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

		<bean id="userDao" class="com.qieast.platform.admincp.dao.impl.UserDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
	</bean>
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
		<property name="url" value="jdbc:mysql://localhost:3306/test" /> 
		<property name="username" value="root" />
		 <property name="password" value="123" /> 
	</bean> 

	<bean id="jdbcTemplate" class