一、先来看代码
1.准备好所需要的Jar包
antlr-2.7.6.jar
aopalliance-1.0.jar
c3p0-0.9.1.2.jar
commons-collections-3.2.1.jar
commons-logging.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.11.0.GA.jar
jta-1.1.jar
ojdbc6.jar
slf4j-api-1.6.1.jar
spring-aop-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-orm-3.2.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
2.准备数据库
create table USER_INFO(
id number(8,0) primary key,
USERNAME varchar2(32),
PASSWORD varchar2(32)
);
3.配置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: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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="net.csdn" />
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="user" value="CNDS" />
<property name="password" value="CNDS" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="10000" />
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="3" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
<property name="acquireIncrement" value="3" />
<property name="maxIdleTimeExcessConnections" value="1800" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan">
<list>
<value>net.csdn.model</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.OracleDialect
</value>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
4.准备对应的实体类
package net.csdn.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
/**
* 对应数据库中的USER_INFO表的实体类
* @author Bowen
*
*/
@Entity
@Table(name="USER_INFO")
@Component("user