引用
1. BaseDAO.java
public interface BaseDAO { public void updatePerson(Person person); public List<Map<String, Object>> searchPersons(); public Map<String, Object> searchPerson(Person person); }
2. PersonDAO.java
import java.util.Map; import javax.sql.DataSource; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import springinaction.base.BaseDAO; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:section5/sec5.xml"}) public class PersonDAO implements BaseDAO{ @Autowired @Qualifier("dataSource") private DataSource dataSource; private static final String SEARCH_ALL = "select * from person"; private static final String SEARCH_BY_ID = "select * from person where id = ?"; private static final String UPDATEBYID = "update person set age = ? where id = ?"; private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void updatePerson(Person person) { jdbcTemplate.update(UPDATEBYID, new Object[]{person.getAge(), person.getId()}); } public List<Map<String, Object>> searchPersons() { return jdbcTemplate.queryForList(SEARCH_ALL); } public Map<String, Object> searchPerson(Person person) { return jdbcTemplate.queryForMap(SEARCH_BY_ID, person.getId()); } }
3. PersonTest.java
import java.util.HashMap; import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import springinaction.base.BaseDAO; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:section5/sec5.xml"}) public class PersonTest { @Autowired @Qualifier("person") private Person person; @Autowired @Qualifier("personDAO") private BaseDAO personDAO; List<Map<String, Object>> resultList = null; Map<String, Object> resultmap = null; @Test public void updatePersonTest() { person.setId(2); person.setAge(3); personDAO.updatePerson(person); resultmap = personDAO.searchPerson(person); System.out.println(resultmap); } @Test public void searchPersonsTest() { resultList = personDAO.searchPersons(); System.out.println(resultList); } }
4.sec5.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/test" /> <property name="username" value="root" /> <property name="password" value="1111" /> <property name="initialSize" value="5" /> <property name="maxActive" value="10" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="person" class="springinaction.section5.Person"/> <bean id="personDAO" class="springinaction.section5.PersonDAO"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean>