1.数据库脚本
create table user ( id int not null, name varchar(20), primary key(id) );
2.实体类
package org.monday.springjdbc; public class User { private int id; private String name; public User() { } public User(int id, String name) { this.id = id; this.name = name; } 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; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
3.Dao接口
package org.monday.springjdbc; import java.util.List; public interface UserDao { public void insert(User user); public void update(User user); public void delete(User user); public void delete(int id); public User findById(int id); public int findByName(String name); public List<User> findAll(); public int count(); public void batchInsert(List<User> users); }
4.Dao的实现类
package org.monday.springjdbc; import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper; import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport; public class UserDaoImpl extends SimpleJdbcDaoSupport implements UserDao { @Override public void insert(User user) { String sql = "insert into user(id,name)values(?,?)"; getSimpleJdbcTemplate().update(sql, user.getId(), user.getName()); } @Override public void update(User user) { String sql = "update user set name=? where id=?"; getSimpleJdbcTemplate().update(sql, user.getName(), user.getId()); } @Override public void delete(User user) { String sql = "delete from user where id=?"; getSimpleJdbcTemplate().update(sql, user.getId()); } @Override public void delete(int id) { String sql = "delete from user where id=?"; getSimpleJdbcTemplate().update(sql, id); } @Override public User findById(int id) { String sql = "select * from user where id=?"; return getSimpleJdbcTemplate().queryForObject(sql, ParameterizedBeanPropertyRowMapper.newInstance(User.class), id); // 下面的方法只能返回一列,所以行不通 // return getSimpleJdbcTemplate().queryForObject(sql, User.class, id); } @Override public int findByName(String name){ String sql = "select id from user where name=?"; return getSimpleJdbcTemplate().queryForObject(sql, Integer.class, name); // Integer.class 改成int.class 不行 // 貌似只能用包装类 } @Override public List<User> findAll() { String sql = "select * from user"; return getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(User.class)); } @Override public int count() { String sql = "select count(*) from user"; return getSimpleJdbcTemplate().queryForInt(sql); } @Override public void batchInsert(List<User> users) { String sql = "insert into user(id,name)values(?,?)"; List<Object[]> parameters = new ArrayList<Object[]>(); for (User u : users) { parameters.add(new Object[] { u.getId(), u.getName() }); } getSimpleJdbcTemplate().batchUpdate(sql, parameters); } }
5.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:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> <!-- 配置DAO --> <bean id="userDao" class="org.monday.springjdbc.UserDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
6.jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring jdbc.username=root jdbc.password=root
7.测试
package org.monday.springjdbc; import java.util.ArrayList; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { private ApplicationContext ctx = new ClassPathXmlApplicationContext( "beans.xml"); private UserDao userDao=(UserDao) ctx.getBean("userDao"); @Test public void testInsert() { User user = new User(1,"zy_1"); userDao.insert(user); } @Test public void testUpdate() { User user = new User(5,"zy_test"); userDao.update(user); } @Test public void testDeleteUser() { User user =new User(); user.setId(5); userDao.delete(user); } @Test public void testDeleteInt() { userDao.delete(4); } @Test public void testFindById() { User user=userDao.findById(1); System.out.println(user); } @Test public void testFindByName() { int id=userDao.findByName("zy_1"); System.out.println(id); } @Test public void testFindAll() { List<User> list=userDao.findAll(); System.out.println(list); } @Test public void testCount() { int count = userDao.count(); System.out.println(count); } @Test public void testBatchInsert() { List<User> users = new ArrayList<User>(); users.add(new User(2,"zy_2")); users.add(new User(3,"zy_3")); users.add(new User(4,"zy_4")); users.add(new User(5,"zy_5")); userDao.batchInsert(users); } }
附:自己写这篇文章的时候,用的是Spring2.5.而今天用了Spring3.1后发现,SimpleJdbcTemplate和SimpleJdbcDaoSupport都被标记为@Deprecated (过时了),后来一看源码,有这么一句话,就明白了。
@deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.JdbcTemplate} and
{@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. The JdbcTemplate and NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
大意就是: 从Spring 3.1开始,JdbcTemplate和NamedParameterJdbcTemplate提供了SimpleJdbcTemplate的功能。
那既然这样的话,SimpleJdbcTemplate和SimpleJdbcDaoSupport被标记为过时,就明白了。