package cn.itcast.spring3.demo2; 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; //public class UserDao { public class UserDao extends JdbcDaoSupport{ /*在Dao层注入JDBC模板*/ /* private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; }*/ public void add(User user){ String sql = "insert into user values(null,?)"; this.getJdbcTemplate().update(sql, user.getName()); } public void update(User user){ String sql = "update user set name = ? where id = ?"; this.getJdbcTemplate().update(sql, user.getName(), user.getId()); } public void delete(User user){ String sql = "delete from user where id = ?"; this.getJdbcTemplate().update(sql, user.getId()); } public int findCount(){ String sql = "select count(*) from user"; return this.getJdbcTemplate().queryForInt(sql); } public String findNameById(int id){ String sql = "select name from user where id = ?"; return this.getJdbcTemplate().queryForObject(sql, String.class, id); } public User findById(int id){ String sql = "select * from user where id = ?"; User user = (User) this.getJdbcTemplate().queryForObject(sql, new UserRowMapper(), id);//rowMapper和DBUtils的Handler一样. //你需要自己去封装它. //只不过DBUtils给你提供了几个实现类,像beanHandler,beanListHandler,MapHandler,MapList.但是Jdbc模板没有提供实现类,要自己手动去封装它. return user; } public List<User> findAll(){ String sql = "select * from user"; return this.getJdbcTemplate().query(sql, new UserRowMapper()); } //内部类 class UserRowMapper implements RowMapper<User>{//泛型是User /** * rs:结果集. * rowNum:行号 */ public User mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub //现在呢它不用你去遍历结果集了,它已经把结果集的光标移向了某一行的具体的数据了. //你在mapRow()方法中不用自己去遍历结果集,这个mapRow()方法可以帮你遍历结果集. User user = new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); return user; } } }
package cn.itcast.spring3.demo2; 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; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
package cn.itcast.spring3.demo2; import java.util.List; 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; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringTest2 { @Autowired @Qualifier("userDao") private UserDao userDao; @Test public void demo1(){ //web层->业务层->Dao层 User user = new User(); user.setName("童童"); //user.setName("小编"); //user.setName("小胖"); userDao.add(user); } @Test public void demo2(){ //web层->业务层->Dao层 User user = new User(); user.setId(1); user.setName("小编"); userDao.update(user); } @Test public void demo3(){ //web层->业务层->Dao层 User user = new User(); user.setId(1); userDao.delete(user); } @Test public void demo4(){ int count = userDao.findCount(); System.out.println(count); } @Test public void demo5(){ //String name = userDao.findNameById(2);//得到2号客户的名称 String name = userDao.findNameById(3);//得到3号客户的名称 System.out.println(name); } @Test public void demo6(){ //String name = userDao.findNameById(2);//得到2号客户的User对象. //User user = userDao.findById(2);//得到2号客户的User对象. User user = userDao.findById(3);//得到3号客户的User对象. System.out.println(user); } @Test public void demo7(){ //String name = userDao.findNameById(2);//得到2号客户的User对象. //User user = userDao.findById(2);//得到2号客户的User对象. List<User> list = userDao.findAll(); for (User user : list) { System.out.println(user); } } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入beans的头 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置Spring默认的连接池 --> <!-- 这个类由Spring来帮我们创建,它默认情况下只创建一次,因为是单例的. --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///spring3_day02"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <!-- 配置DBCP连接池 --> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///spring3_day02"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties">--><!-- jdbc.properties也是在src下 --><!-- </property> </bean> --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0连接池 --> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- <property name="driverClass" value="com.mysql.jdbc.Driver"></property> --> <property name="driverClass" value="${jdbc.driver}"></property> <!-- <property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"></property> --> <property name="jdbcUrl" value="${jdbc.url}"></property> <!-- <property name="user" value="root"></property> --> <property name="user" value="${jdbc.user}"></property> <!-- <property name="password" value=""></property> --> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 定义jdbctemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource2"></property><!-- 把上面定义好的连接池注入进来了 --> </bean> <bean id="userDao" class="cn.itcast.spring3.demo2.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///spring3_day02
jdbc.user = root
jdbc.password =