//applicationContext.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
">
<!-- 指定读取配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!--1 将连接池放进spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2 将jdbc模板放进连接池 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3将userDaoImp放进连接池 -->
<bean name="userDaoImp" class="cn.jy.domain.UserDaoImp">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
//UserDaoImp继承于UserDao接口
package cn.jy.domain;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//import org.apache.tomcat.util.digester.RuleSet;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import cn.jy.dao.UserDao;
//使用JDBC模板实现增删改查
public class UserDaoImp extends JdbcDaoSupport implements UserDao {
public void save(User u) {
String sql="insert into user1 values(2,?)";
super.getJdbcTemplate().update(sql, u.getName());
}
public void delete(Integer id) {
String sql="delete from user1 where id=?";
super.getJdbcTemplate().update(sql, id);
}
public void update(User u) {
String sql="update user1 set name=? where id=?";
super.getJdbcTemplate().update(sql,u.getName(), u.getId());
}
public User getById(Integer id) {
String sql="select * from user1 where id=?";
return super.getJdbcTemplate().queryForObject(sql, new RowMapper<User>(){
public User mapRow(ResultSet rs,int arg) throws SQLException{
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}
},id);
}
public int getTotalCount() {
String sql ="select count(*) from user1";
Integer num = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return num ;
}
public List<User> getAll() {
String sql="select * from user1";
List<User> list= super.getJdbcTemplate().query(sql, new RowMapper<User>(){
public User mapRow(ResultSet rs,int arg) throws SQLException{
User u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
System.out.println(u);
return u;
}
});
return list;
}
}
//测试类Demo.java
package cn.jy.dao;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//连接配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
//与 Spring框架下配置UserDaoImp进行连接
@Resource(name="userDaoImp")
//声明要调用的对象
private UserDao us;
@Test
//存储数据
// public void fun1(){
// User u=new User();
// u.setName("tong");
// us.save(u);
// }
//获得id为2的name值
// public void fun1(){
// System.out.println(us.getById(2));
// }
//插入数据
// public void fun1(){
// User u = new User();
// u.setId(2);
// u.setName("tiger");
// us.update(u);
// }
//获得所有数据
// public void fun1(){
// us.getAll();
// }
//获得数据总数
// public void fun1(){
// System.out.println(us.getTotalCount());
//
// }
//删除数据
public void fun1(){
us.delete(3);
}
}