Spring DAO

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());
		}
	}

}


 

因为添加人员的方法一直报语法错误,所以,我把它给注释掉了,而且获取所有人信息的中,birthday属性的转换存在问题,解决中。


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值