日期:2014-05-16  浏览次数:20680 次

SpringJdbc的应用举例
   这几天在研究底层框架的异同,突然想起还有个springJdbc好像以前用过还不错,可是如何在不使用ssh框架的前提下单独使用spring的jdbc这还是头一次,让我头疼了一下,终于还是搞定了,以下是我整个测试过程的全部应用代码、测试代码、及配置文件详细情况。

第一步:先写好一个实体类People:
package com.ego.springjdbc.vo;

import java.io.Serializable;
import java.sql.Date;

public class People implements Serializable{
	
	private int id;  
	private String name;  
	private Date birthDay;  
	private Boolean sex;  
	private Double weight;  
	private float height;
	
	public People(){
		
	}


	public People(int id, String name, Date birthDay, Boolean sex,
			Double weight, float height) {
		super();
		this.id = id;
		this.name = name;
		this.birthDay = birthDay;
		this.sex = sex;
		this.weight = weight;
		this.height = height;
	}




	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthDay() {
		return birthDay;
	}
	public void setBirthDay(Date birthDay) {
		this.birthDay = birthDay;
	}
	public Boolean getSex() {
		return sex;
	}
	public void setSex(Boolean sex) {
		this.sex = sex;
	}
	public Double getWeight() {
		return weight;
	}
	public void setWeight(Double weight) {
		this.weight = weight;
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}


	@Override
	public String toString() {
		return "People [birthDay=" + birthDay + ", height=" + height + ", id="
				+ id + ", name=" + name + ", sex=" + sex + ", weight=" + weight
				+ "]";
	}
}


第二步就是配置一个只有spring的环境:
添加spring的jar包和数据库的驱动包这个就不说了。主要是配置spring.xml和读取数据库连接参数database.properties属性文件。


database.properties属性文件:
#MySql数据库连接参数配置
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/testibatis
username=root
password=111111


spring.xml配置文件:
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	    http://www.springframework.org/schema/tx
	    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	        <property name="locations">
	            <value>classpath:database.properties</value>
	        </property>
	    </bean>
  		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  			<property name="driverClassName" value="${driver}"/>
  			<property name="url" value="${url}"/>
  			<property name="username" value="${username}"/>
  			<property name="password" value="${password}"/>
  		</bean>
     	<!-- 
     	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     		<property name="dataSource" ref="dataSource"/>
     	</bean>
     	-->
     	<bean id="peopleDaoImpl" class="com.ego.springjdbc.dao.impl.PeopleDaoImpl">
     		<property name="dataSource" ref="dataS