传统的JDBC编程中,总是需要与一些类打交道,比如:Connection等,而且处理完持久化后还需要手动的关闭连接,并且,事务的管理也不是跟方便,这些琐碎的问题都需要我们花费些时间去处理,但是,现在“大管家”Spring给我们带来了方便,Spring框架对JDBC进行了二次封装,抛弃了JDBC API与数据库连接并且事务处理也交给Spring去打理,大大方便了开发者。
实体类User有两个属性:id与username,分别对应User表中的id与username字段。
五、测试代码:
下面,就Spring框架中的DAO模块中的JdbcDaoSupport类的使用进行介绍
一、创建实体类:
package com.spr.model;
public class User {
private int id;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
实体类User有两个属性:id与username,分别对应User表中的id与username字段。
二、在applicationContext.xml中创建数据源bean
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hibernate"/>
<property name="username" value="root"></property>
<property name="password" value="7756"></property>
</bean>
三、创建DAO层(为了方便就直接创建实现类,没有创建相应的接口),并且该类继承JdbcDaoSupport类(import org.springframework.jdbc.core.support.JdbcDaoSupport;)
package com.spr.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.spr.model.User;
public class UserDao extends JdbcDaoSupport{
//增加用户
public void saveUser(User user){
String sql = "insert into user (username) values (?)";
Object[] args = {user.getUsername()};
this.getJdbcTemplate().update(sql, args);
}
//删除用户
public void deleteUser(int id){
String sql = "DELETE FROM user WHERE (id=?)";
Object[] args = {id};
this.getJdbcTemplate().update(sql, args);
}
//更新用户
public void updateUser(User user){
String sql = "UPDATE user SET username=? WHERE (id=?)";
Object[] args = {user.getUsername(), user.getId()};
if(this.getJdbcTemplate().update(sql, args) > 0){
System.out.println("更新成功!");
}
}
//查找用户名
public String findUser(int id){
String sql = "select username from user where id=?";
Object[] args = {id};
return this.getJdbcTemplate().queryForObject(sql, args, String.class);
}
//查找用户资料
public User findUserInfo(int id){
String sql = "select id,username from user where id=?";
Object[] args = {id};
Map<String, Object> map = this.getJdbcTemplate().queryForMap(sql,args);
User user = new User();
user.setId((Integer)map.get("id"));
user.setUsername((String)map.get("username"));
return user;
}
//查找所有用户
public List<User> findAll(){
String sql = "select * from user";
List<User> userList = new ArrayList<User>();
List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql);
for(Map<String, Object> map : list){
User user = new User();
user.setId((Integer)map.get("id"));
user.setUsername((String)map.get("username"));
userList.add(user);
}
return userList;
}
}
四、在applicationContext.xml中创建UseDao类的bean并把之前的dataSource bean注入该类的dataSource属性中(该属性石子类继承父类的)
<bean name="userDao" class="com.spr.dao.UserDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
五、测试代码:
package com.spr.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spr.dao.UserDao;
import com.spr.model.User;
public class Test {
private UserDao userDao;
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = new User();
user.setId(2);
user.setUsername("bbb");
UserDao dao = (UserDao) context.getBean("userDao");
// dao.saveUser(user);
// dao.deleteUser(1);
// dao.updateUser(user);
// System.out.println(dao.findUser(2));
// User user2 = dao.findUserInfo(2);
// System.out.println("id:"+user2.getId()+" username:"+user2.getUsername());
List<User> list = dao.findAll();
for(User users : list){
System.out.println("id:"+users.getId()+" username:"+users.getUsername());
}
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
源码下载:http://download.csdn.net/detail/yezis/7343209