Spring学习-27:Spring中的JDBC Template(JDBC模板):完成查询的操作

来源:互联网 发布:名字竞技场 源码 编辑:程序博客网 时间:2024/06/06 23:51

我们在上一讲没有去做查询的操作,因为这里面的查询呢,要稍微麻烦一点点儿,因为我们说过,JDBC Template跟DBUtils比较类似,DBUtils提供了一系列handler,但是JDBC模板里面没有提供handler,需要我们自己去封装,这就给查询操作增加了一些复杂性。

下面,我们来进行查询工作,我们先人为的把查询分为两类:

1、简单查询(返回的是一些比较简单的数字、字符串,而不是一个对象或集合)

select count(*) from user;--queryForInt(String sql)

select name from user where id=?;--queryForObject(String sql, Class requiredType, Object args)

2、复杂查询(返回的是对象和对象集合,这种情况下需要我们自己去封装)

     select * from user where id=?;

--public <T> List<T> queryForObject(String sql, RowMapper<T> rowMapper, Object... args)

【注】RowMapper类似于DBUtils中的handler,需要自己去封装。例如我们下面自己封装的UserRowMapper内部类。

select * from user;

--public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) 

首先,我们来看简单查询。

在user表中添加几条记录。

修改上一讲的UserDao:

package com.js.demo2;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class UserDao extends JdbcDaoSupport{public void add(User user){String sql="insert into user values (null,?)";this.getJdbcTemplate().update(sql, user.getName());}public void update(User user){String sql="update user set name=? where id=?";this.getJdbcTemplate().update(sql, user.getName(),user.getId());}public void delete(User user){String sql="delete from user where id=?";this.getJdbcTemplate().update(sql, user.getId());}public int findCount(){String sql="select count(*) from user";return  this.getJdbcTemplate().queryForInt(sql);}}
编写测试类:

package com.js.demo2;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContextCRUD.xml")public class SpringTest {@Autowired@Qualifier("userDao")private UserDao userDao;@Testpublic void demo2(){int count=userDao.findCount();System.out.println(count);}}
运行测试,得到统计结果。


对于根据id获取名字的简单查询,具体实现如下:

UserDao:

package com.js.demo2;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class UserDao extends JdbcDaoSupport{public void add(User user){String sql="insert into user values (null,?)";this.getJdbcTemplate().update(sql, user.getName());}public void update(User user){String sql="update user set name=? where id=?";this.getJdbcTemplate().update(sql, user.getName(),user.getId());}public void delete(User user){String sql="delete from user where id=?";this.getJdbcTemplate().update(sql, user.getId());}public int findCount(){String sql="select count(*) from user";return  this.getJdbcTemplate().queryForInt(sql);}public String findNameById(int id){String sql = "select name from user where id=?";return this.getJdbcTemplate().queryForObject(sql, String.class, id);}}
测试类:

package com.js.demo2;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContextCRUD.xml")public class SpringTest {@Autowired@Qualifier("userDao")private UserDao userDao;@Testpublic void demo3(){String name=userDao.findNameById(3);System.out.println(name);}}
运行测试,得到查询结果:



接下来,我们来看复杂查询,比如返回一个对象或者list集合。
修改UserDao:

package com.js.demo2;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;public class UserDao extends JdbcDaoSupport{public void add(User user){String sql="insert into user values (null,?)";this.getJdbcTemplate().update(sql, user.getName());}public void update(User user){String sql="update user set name=? where id=?";this.getJdbcTemplate().update(sql, user.getName(),user.getId());}public void delete(User user){String sql="delete from user where id=?";this.getJdbcTemplate().update(sql, user.getId());}public int findCount(){String sql="select count(*) from user";return  this.getJdbcTemplate().queryForInt(sql);}public String findNameById(int id){String sql = "select name from user where id=?";return this.getJdbcTemplate().queryForObject(sql, String.class, id);}public User findById(int id){String sql="select * from user where id=?";User user=(User)this.getJdbcTemplate().queryForObject(sql, new UserRowMapper(), id);return user;}class UserRowMapper implements RowMapper<User>{public User mapRow(ResultSet rs, int rowNum) throws SQLException {/** * rs:结果集 * rowNum:记录行号 * 行号不用我们手动操作,方法会帮我们遍历结果集 * 我们只需要把结果集中的数据放到对象的属性中 */User user = new User();user.setId(rs.getInt("id"));user.setName(rs.getString("name"));return user;}}}

编写测试类:

package com.js.demo2;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContextCRUD.xml")public class SpringTest {@Autowired@Qualifier("userDao")private UserDao userDao;@Testpublic void demo6(){User user=userDao.findById(3);System.out.println(user);}}
运行测试:


如果想查询多个呢?

在UserDao中添加如下函数:

public List<User> findAll(){String sql = "select * from user";return this.getJdbcTemplate().query(sql, new UserRowMapper());}

在测试类中编写测试:

@Testpublic void demo6(){List<User> list=userDao.findAll();for(User user:list)System.out.println(user);}
运行测试:


【注:】注意内部类在这里的使用。

0 0
原创粉丝点击