使用Spring JDBC,你只需要做四个工作:
1)定义连接参数:也就是定义url,driver,user,password这个几个参数,一般我们会用一个jdbc.properties文件来配置。
2)指定要执行那个sql语句:项目中要用到什么样的SQL语句,Spring是肯定不知道的,只能我们自己设置的。
3)使用PreparedStatement时需要参数,传递哪些参数肯定也是需要我们自己设置的。
4)迭代结果集的过程需要做那些事情,肯定也是需要我们自己写的。
JdbcTemplate 的使用需要有 DataSource 的支持,所以在配置文件中,我们首先要配置一个 DataSource ,
然后在将这个 DataSource 配置到 JdbcTemplate 里。
接着将 JdbcTemplate 配置进 DAO 层,最后将 DAO 配置进 Model 层。
首先是整个案例的结构.....
然后先看配置文件吧...
beans.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: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/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.springframework.docs.test" /> <context:property-placeholder location="jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="userDao" class="com.dao.impl.UserDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <bean id="userService" class="com.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> </beans>
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=1234
model
package com.model; /** * Created by cyx on 2016/3/5. */ public class User { private int id; private String name; private int age; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id =id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.dao; import com.model.User; import java.util.List; /** * Created by cyx on 2016/3/5. */ public interface UserDao { public void add(User user); public void update(User user); public void delete(int id); public List<User> queryAll(); }
package com.dao.impl; import com.dao.UserDao; import com.model.User; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; /** * Created by cyx on 2016/3/5. */ public class UserDaoImpl implements UserDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void add(User user) { String sql = "insert into user_t (name,age) values (?,?)"; Object[] args = {user.getName(), user.getAge()}; int res = jdbcTemplate.update(sql, args); if (res == 1) { System.out.println("add success"); } } public void update(User user) { String sql = "update user_t set name = ?, age = ? where id = ?"; Object[] args = {user.getName(), user.getAge(), user.getId()}; int res = jdbcTemplate.update(sql, args); if (res == 1) { System.out.println("update success"); } } public void delete(int id) { String sql = "delete from user_t where id = ?"; int res = jdbcTemplate.update(sql, id); if (res == 1) { System.out.println("delete success"); } } public List<User> queryAll() { String sql = "select * from user_t"; final List<User> list = new ArrayList<User>(); jdbcTemplate.query(sql, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { User u = new User(); u.setId(rs.getInt("id")); u.setName(rs.getString("name")); u.setAge(rs.getInt("age")); list.add(u); } }); return list; } }
package com.service; import com.model.User; import java.util.List; /** * Created by cyx on 2016/3/5. */ public interface UserService { public void add(User user); public void update(User user); public void delete(int id); public List<User> queryAll(); }
userService 的实现 userServiceImpl
package com.service.impl; import com.dao.UserDao; import com.model.User; import com.service.UserService; import java.util.List; /** * Created by cyx on 2016/3/6. */ public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add(User user) { this.userDao.add(user); } public void update(User user) { this.userDao.update(user); } public void delete(int id) { this.userDao.delete(id); } public List<User> queryAll() { return this.userDao.queryAll(); } }
Test 测试代码
package com.test; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.model.User; import com.service.UserService; public class jdbcTemplateTest { private static ApplicationContext ac; static { ac = new ClassPathXmlApplicationContext("beans.xml"); } @Test public void testAdd(){ UserService us = (UserService)ac.getBean("userService"); us.add(new User("2b",22)); } @Test public void testUpdate(){ UserService us = (UserService)ac.getBean("userService"); us.update(new User(1,"2b",11)); } @Test public void testdelete(){ UserService us = (UserService)ac.getBean("userService"); us.delete(1); } @Test public void testQueryAll(){ UserService us = (UserService)ac.getBean("userService"); List<User> list = us.queryAll(); for(User u :list){ System.out.println(u.getId()+" , "+u.getName()+" , "+u.getAge()); } } }
参考文章: http://item.congci.com/item/spring-jdbctemplate-shili