第4章 Spring的DAO模块.doc





《第4章 Spring的DAO模块.doc》由会员分享,可在线阅读,更多相关《第4章 Spring的DAO模块.doc(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、。第4章 Spring的DAO模块本章学习目的和要求本章重点和难点Spring的DAO模块提供了对JDBC、Hibernate、JDO等DAO层支持。本节先介绍对DAO模块对JDBC技术的支持。DAO模块依赖于commons-pool.jar、commons-collections.jar,Eclipse自带的Spring DAO类库没有这两个类库,需要自己添加。4.1 实例:保存Person实体传统的JDBC编程中,总免不了与Connection、Statement、PreparedStatement、ResultSet、SQLException等打交道,还要注意打开连接后要释放连接等琐碎的
2、问题。Spring框架对JDBC进行了封装,完全抛弃了JDBC API。数据库连接、事务等也交给了Spring打点,开发者只需要使用封装好的JdbcTemplate执行SQL语句,然后得到需要的结果。看一个例子。4.1.1 实体类Person代码本例的POJO实体类为Person类。本例将使用Spring提供的JdbcTemplate将Person持久化到数据库中,或者将Person从数据库中读取出来。Person类的代码为(getter、setter略):Person.javapackage com.helloweenvsfei.spring.dao;import java.util.Dat
3、e;public class Person/ POJOprivate Integer id;/ IDprivate String name;/ 姓名private String sex;/ 性别private int age;/ 年龄private Date birthday;/ 生日4.1.2 Dao层接口Dao层接口定义了操作Person实体类的方法。接口不会与Spring的Dao模块耦合。代码为:IPersonDao.javapackage com.helloweenvsfei.spring.dao;import java.util.List;public interface IPers
4、onDao/Person的DAO接口public String getPersonName(Integer id);/ 根据id获取Person姓名public void addPerson(Person person);/ 添加Person对象public int getPersonsCount();/ 返回所有Person的数目public List listPersons();/ 返回所有的Person4.1.3 继承JdbcDaoSupportDao层实现IPersonDao接口,继承自Spring的DAO模块的JdbcDaoSupport类。JdbcDaoSupport提供JdbcT
5、emplate对象,封装了常用的JDBC操作。Dao实现代码为:PersonDaoImpl.javapackage com.helloweenvsfei.spring.dao;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class PersonDaoImpl extends JdbcDaoSupport implements IPersonDao/继承Supportpublic void initDatabase()/ 初始化方法,创建表String sql= create table if not ex
6、ists tb_person /SQL语句+ (id int auto_increment,+name varchar(255),+ sex varchar(10),age int,birthday timestamp,primary key (id);getJdbcTemplate().execute(sql);/执行SQLSuppressWarnings(all)public List listPersons()/ 返回所有的PersonString sql = select id, name, sex, age, birthday from tb_person ;/ SQL语句ListM
7、ap list = getJdbcTemplate().queryForList(sql);/ 返回数据List personList = new ArrayList();/ List对象for (Map row : list)/ 遍历数据Person person = new Person();/ 封装为Person对象person.setId(Integer) row.get(id);/ 设置IDperson.setName(String) row.get(name);/ 设置nameperson.setSex(String) row.get(sex);/ 设置sexperson.setA
8、ge(Integer) row.get(age);/ 设置ageperson.setBirthday(Date) row.get(birthday);/ 设置birthdaypersonList.add(person);/ 添加到listreturn personList;/ 返回Listpublic void addPerson(Person person)/保存PersonString sql = insert into tb_person ( name, sex, age, birthday ) values ( ?, ?, ?, ? ) ;getJdbcTemplate().updat
9、e( sql,/ 执行SQL语句new Object person.getName(), person.getSex(),/ 设置SQL参数person.getAge(), person.getBirthday(), );public String getPersonName(Integer id) / 获取person姓名String sql = select name from tb_person where id = + id;/ SQL语句return (String) getJdbcTemplate().queryForObject(sql, String.class);/ 查询并返
10、回public int getPersonsCount() / 返回Person数量String sql = select count(*) from tb_person ;/ SQL语句return getJdbcTemplate().queryForInt(sql);/ 查询总数PersonDaoImpl实现了IPersonDao接口。代码直接继承了Spring提供的JdbcDaoSupport,因此没有使用Connection、Statement等JDBC API。Dao操作数据库是使用的Spring封装好的JdbcTemplate,用它来执行SQL、查询Person列表、查询单个Per
11、son属性值、查询Person的总数。查询Person列表时返回的是一个ListMap类型数据。4.1.4 Spring配置PersonDaoImpl还添加了一个非接口的方法initDatabase(),用于生成表结构。需要将initDatabase配置到Spring中。配置代码为:ApplicationContext.xmlbean id=personDao class=com.helloweenvsfei.spring.dao.PersonDaoImpldepends-on=dataSource init-method=initDatabaseJdbcDaoSupport内置数据源属性,配
12、置时需要设置。4.1.5 运行代码运行程序前要创建数据库spring。下面的代码中先从Spring中获取Dao对象,然后实例化一个Person对象,并保存进数据库。运行代码为:DaoRun.javapackage com.helloweenvsfei.spring.dao;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;public class DaoRunpublic static void main(String
13、args)XmlBeanFactory factory = new XmlBeanFactory(/ 获取Factorynew ClassPathResource(applicationContext.xml);IPersonDao personDao = (IPersonDao) factory.getBean(personDao);/获取DAOPerson person = new Person();/实例化一个对象person.setName(Helloween);/设置姓名person.setAge(30);/设置年龄person.setSex(男);/设置性别person.setBi
14、rthday(new Date();/设置生日personDao.addPerson(person);/添加到数据库System.out.println(Count: + personDao.getPersonsCount();/查询Person总数System.out.println(personDao.getPersonName(1);/查询Person姓名List personList = personDao.listPersons();/查询所有Personfor (Person p : personList)/遍历System.out.println(Name: + p.getNam
15、e();/打印姓名DaoRun分别调用了IPersonDao里的四个方法。运行效果如图4.1。图4.1 DaoRun运行效果4.2 返回实体对象JdbcTemplate有个小小的遗憾,不能直接返回Java对象。Spring中提供了MappingSqlQuery类把查询结果封装为Java对象。4.2.1 MappingSqlQuery实现代码MappingSqlQuery是一个抽象类,开发者需要实现它的mapRow(ResultSet, int)方法实现从ResultSet到Java对象的映射。该方法直接返回实体类对象。例如:PersonMappingQuery.javapackage com.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第4章 Spring的DAO模块 Spring DAO 模块

限制150内