personDaoImpl.java
package jdbc; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.stereotype.Repository; public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao { @Override public void savePerson() { this.getJdbcTemplate().execute("insert into person values(1,'万个')"); System.out.println("savePerson"); } @Override public List<Person> getAllPerson() { String sql = "select * from person"; List list = this.getJdbcTemplate().queryForList(sql); // list中为map类型数据 // List list = this.getJdbcTemplate().queryForList(sql, Person.class); // // 当返回基本类型的时候可以这么用,返回对象的时候需用RowMapper return list; } @Override public List<Person> getAllPersonByRowMapper() { String sql = "select * from person"; List<Person> personList = this.getJdbcTemplate().query(sql, new PersonRowMapper()); System.out.println(personList.size()); return personList; } @Override public Person getPerson() { String sql = "select * from Person where pid=?"; Person person = (Person) this.getJdbcTemplate().query(sql, new Object[]{2}, new PersonRowMapper()); return person; } }
personRowMapper.java
package jdbc; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class PersonRowMapper implements RowMapper { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Person person = new Person(); person.setPid(rs.getInt("pid")); person.setUsername(rs.getString("pname")); return person; } }
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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配置数据库连接池 --> <bean id="dateSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3399/db" /> <property name="username" value="root" /> <property name="password" value="3333456" /> </bean> <bean id="personDao" class="jdbc.PersonDaoImpl"> <property name="dataSource"> <ref bean="dateSource"/> </property> </bean> </beans>
DataSourceTest.java
package jdbc; import java.util.List; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DataSourceTest { @Test public void testDataSource() { ApplicationContext context = new ClassPathXmlApplicationContext( "jdbc/applicationContext.xml"); DataSource ds = (DataSource) context.getBean("dateSource"); System.out.println(ds); } @Test public void testDa() { ApplicationContext context = new ClassPathXmlApplicationContext( "jdbc/applicationContext.xml"); PersonDao personDao = (PersonDao) context.getBean("personDao"); personDao.savePerson(); } @Test public void testGetAllPerson() { ApplicationContext context = new ClassPathXmlApplicationContext( "jdbc/applicationContext.xml"); PersonDao personDao = (PersonDao) context.getBean("personDao"); List list = personDao.getAllPerson(); } @Test public void testGetAllPersonByRowMapper() { ApplicationContext context = new ClassPathXmlApplicationContext( "jdbc/applicationContext.xml"); PersonDao personDao = (PersonDao) context.getBean("personDao"); List<Person> personList = personDao.getAllPersonByRowMapper(); for (Person person : personList) { System.out.println(person.getUsername()); } } }
浙公网安备 33010602011771号