Spring整合JDBC实现简单的增删改续-实现基于RowMaper的查询:
在上面一篇 Spring整合JDBC实现简单的增删改 的基础上完成对数据库的查询操作。
首先查看Spring文档资料,Spring文档及镜像下载参考文章:Spring简单学习计划 ,里面包含了简单的学习计划!
根据以上文档,编写查询方法,示例代码如下:
package org.oms.spring.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import java.util.List; import javax.annotation.Resource; import javax.sql.DataSource; import org.oms.spring.model.Group; import org.oms.spring.model.User; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; @Repository("userJdbcDao") public class UserDao implements IUserDao { private JdbcTemplate JdbcTemplate; /** * Set the JDBC DataSource to obtain connections from. */ @Resource public void setDataSource(DataSource dataSource) { JdbcTemplate = new JdbcTemplate(dataSource); } @Override public void add(User user, int gid) { JdbcTemplate .update("insert into user(username,password,gid,age,birthday,createdate) value (?,?,?,?,?,?)", user.getUsername(), user.getPassword(), gid, user.getAge(), user.getBirthDay(), user.getCreateDate()); } @Override public void update(User user, int gid) { JdbcTemplate .update("update user set username=?,password=?,gid=?,age=?,birthday=?,createdate=? where id=?", user.getUsername(), user.getPassword(), gid, user.getAge(), user.getBirthDay(), user.getCreateDate(), user.getId()); } @Override public void delete(int id) { JdbcTemplate.update("delete from user where id =?", id); } @Override public User load(int id) { String sql="select t1.id uid ,t1.*,t2.* from user t1 left join `group` t2 on (t1.gid=t2.id) where t1.id=?"; /** * 第一个参数是SQL语句 * 第二个参数是SQL语句中参数值,需要传入一个对象数组 * 第三个是一个RowMapper,这个RowMapper可以完成一个对象和数据库字段的对应,实现这个RowMapper需要实现mapRow方法, * 在这个mapRow方法中有rs这个参数,通过rs可以有效的获取数据库的字段 * 如果这个方法在DAO中会被重复使用,建议通过内部类来解决,而不要使用匿名的内部类 */ User u=(User)JdbcTemplate.queryForObject(sql,new Object[]{id} ,new RowMapper<User>(){ @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { Group group=new Group(); group.setName(rs.getString("name")); group.setId(rs.getInt("gid")); User user=new User(); user.setGroup(group); user.setId(rs.getInt("uid")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setAge(rs.getInt("age")); user.setBirthDay(rs.getDate("birthday")); user.setCreateDate(rs.getDate("createdate")); return user; } }); return u; } /** * 上面方法简化 * @param id * @return */ public User loadOther(int id) { String sql="select t1.id uid ,t1.*,t2.* from user t1 left join `group` t2 on (t1.gid=t2.id) where t1.id=?"; User u=(User)JdbcTemplate.queryForObject(sql,new Object[]{id} ,new UserMapper()); return u; } @Override public List<User> list(String sql,Object[] args) { //查询总条数,获取整数值 String sqlCount="select count(1) from user"; int count=JdbcTemplate.queryForInt(sqlCount); System.out.println(count); //查询单列,获取String类型的列表 String sqlCol="select username from user"; List<String> cols=JdbcTemplate.queryForList(sqlCol, String.class); for (int i = 0; i < cols.size(); i++) { System.out.println(cols.get(i)); } //查询多列 String sqlCols="select username,password from user"; List<User> users=JdbcTemplate.query(sqlCols,new RowMapper<User>(){ @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user=new User(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); return user; } }); for (int i = 0; i < users.size(); i++) { System.out.println(users.get(i).toString()); } //查询列表 return JdbcTemplate.query(sql, args,new UserMapper()); } /** * 建议通过内部类来解决,而不要使用匿名的内部类 * @author sunlight * */ private class UserMapper implements RowMapper<User>{ @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { Group group=new Group(); group.setName(rs.getString("name")); group.setId(rs.getInt("gid")); User user=new User(); user.setGroup(group); user.setId(rs.getInt("uid")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setAge(rs.getInt("age")); user.setBirthDay(rs.getDate("birthday")); user.setCreateDate(rs.getDate("createdate")); return user; } } }
这部分内容是我们经常查询遇到的,所以我在上面直接添加到查询列表方法中了:
//查询总条数,获取整数值 String sqlCount="select count(1) from user"; int count=JdbcTemplate.queryForInt(sqlCount); System.out.println(count); //查询单列,获取String类型的列表 String sqlCol="select username from user"; List<String> cols=JdbcTemplate.queryForList(sqlCol, String.class); for (int i = 0; i < cols.size(); i++) { System.out.println(cols.get(i)); } //查询多列 String sqlCols="select username,password from user"; List<User> users=JdbcTemplate.query(sqlCols,new RowMapper<User>(){ @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user=new User(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); return user; } }); for (int i = 0; i < users.size(); i++) { System.out.println(users.get(i).toString()); }
package org.oms.spring.test; import static org.junit.Assert.*; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.oms.spring.dao.IGroupDao; import org.oms.spring.dao.IUserDao; import org.oms.spring.model.Group; import org.oms.spring.model.User; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 当使用了以下注释之后,就可以直接在Test中进行依赖注入 * * @author sunlight * */ // 让Junit运行在Spring的测试环境中 @RunWith(SpringJUnit4ClassRunner.class) // 加载beans.xml文件 @ContextConfiguration("/beans.xml") public class TestJdbc { @Resource(name = "userJdbcDao") private IUserDao userJdbcDao; @Resource(name="groupJdbcDao") private IGroupDao groupJdbcDao; @Test public void testAdd() { Group group=new Group("test1"); groupJdbcDao.add(group); System.out.println(group.getId()); User user = new User("我是Spring","123", 20, new Date(), new Date()); userJdbcDao.add(user,group.getId()); } @Test public void testUpdate() { User user = new User("Spring","123", 20, new Date(), new Date()); user.setId(2); userJdbcDao.update(user,1); } @Test public void testDel() { userJdbcDao.delete(2); } @Test public void testLoad() { User user=userJdbcDao.load(1); System.out.println(user); } @Test public void testList() { String sql="select t1.id uid ,t1.*,t2.* from user t1 left join `group` t2 on (t1.gid=t2.id)"; List<User> users=userJdbcDao.list(sql, null); for (int i = 0; i < users.size(); i++) { System.out.println(users.get(i).toString()); } } }
上面是JdbcTemplate实现的增删改操作(也就是参数带 “?”),下面的文档有使用 NamedParameterJdbcTemplate 实现,此方法以后实现!
以上就是JDBC简单实现增删改的操作,下节实现用模板设计模式实现此功能!
如需转载,请注明出处!