Spring的DAO模块提供了对JDBC、Hibernate、JDO等DAO层的支持。本节先介绍DAO模块对JDBC技术的支持
实例:保存Person实体:
1.实体类Person代码:
package com.spring.bean;
import java.util.Date;
public class Person {
private Integer id;
private String name;
private String sex;
private int age;
private String birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
2.DAO曾的接口:
package com.spring.inter;
import java.util.List;
import com.spring.bean.Person;
public interface IPersonDao {
public String getPersonName(Integer id);
public void addPerson(Person person);
public int getPersonCount();
public List<Person> listPersons();
}
3.Dao层接口的实现,继承JdbcDaoSupport
package com.spring.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.spring.bean.Person;
import com.spring.inter.IPersonDao;
public class PersonDaoImpl extends JdbcDaoSupport implements IPersonDao {
@Override
public String getPersonName(Integer id) {
String sql="select name from tb_person where id="+id;
return getJdbcTemplate().queryForObject(sql, String.class).toString();
}
@Override
public void addPerson(Person person) {
String sql="insert into tb_person(name,sex,age,birthday) values (?,?,?,?)";
Object [] params = {person.getName(),person.getSex(),person.getAge(),person.getBirthday()};
getJdbcTemplate().update(sql);
}
@Override
public int getPersonCount() {
String sql="select count(*) from tb_person";
return getJdbcTemplate().queryForInt(sql);
}
@Override
public List<Person> listPersons() {
String sql="select id, name,sex,age,birthday from tb_person";
List<Map<String,Object>> list= getJdbcTemplate().queryForList(sql);
List<Person> personList = new ArrayList<Person>();
for(Map<String,Object> map:list )
{
Person person = new Person();
person.setId((Integer)map.get("id"));
person.setAge((Integer) map.get("age"));
//person.setBirthday((String)map.get("birthday"));
person.setName((String) map.get("name"));
person.setSex((String)map.get("sex"));
personList.add(person);
}
return personList;
}
public void initDatabase()
{
String sql="create table if not exists tb_person"
+"(id int auto_increment ,name varchar(255),sex varchar(10),age int ,birthday timestamp ,primary key (id) ) ";
getJdbcTemplate().execute(sql);
}
}
4.Spring的配置:(我是单独建立了一个配置文件)
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ibatis?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="personDao" class="com.spring.impl.PersonDaoImpl" depends-on="dataSource" init-method="initDatabase">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
5.测试:
package com.spring.junit;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.spring.bean.Person;
import com.spring.inter.IPersonDao;
public class TestSpringDao {
@Test
public void test() {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext_dao.xml"));
IPersonDao personDao = (IPersonDao)beanFactory.getBean("personDao");
Person p = new Person();
p.setName("吴彩虹");
p.setAge(21);
p.setSex("女");
p.setBirthday("2011-12-01");
//添加人员
// personDao.addPerson(p);
//查询Person的总数
System.out.println("count:"+personDao.getPersonCount());
//查询Person的名字
String name = personDao.getPersonName(1);
System.out.println("name:"+name);
//获取所有的信息
List<Person> personList = personDao.listPersons();
for(Person ps:personList )
{
System.out.println("name:"+ps.getName());
}
}
}