一、Vo(User.java)
package cn.edu.shnu.Vo;
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
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;
}
}
二、Vo与数据库表的映射文件(User.hbm.xml)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.edu.shnu.Vo.User" table="user" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<propery name="age" column="age" />
</class>
</hibernate-mapping>
三、IUserDAO接口(IUserDAO.java)
package cn.edu.shnu.impl;
public class IUserDAO {
public void insert(User user);
public User find(Integer id);
}
四、(UserDAO.java) 实现上面的接口
package cn.edu.shnu.Dao
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class UserDAO implements IUserDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory){
hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public void insert(User user){
hibernateTemplate.save(user);
}
public User find(Integer id){
User user = ()hibernateTemplate.get(User.class, id);
return user;
}
}
五、(beans-config.xml)配置文件
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsl="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.dataSource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yourDBName" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>cn/edu/shnu/Vo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean id="userDAO" class="cn.edu.shnu.Dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="proxyInterfaces">
<list><value>cn.edu.shnu.impl.IUserDAO</value></list>
</property>
<property name="target" ref="userDAO"/>
<property name="transactionAttributes">
<props>
<prop key="insert">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
六、测试(SpringHibernateTest.java)
package cn.edu.shnu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringHibernateTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
//建立DAO对象
IUserDAO userDAO = (IUserDAO)context.getBean("userDAOProxy");
User user = new User();
user.setName("Nemo");
user.setAge(new Integer(26));
userDAO.insert(user);
user = userDAO.find(new Integer(1));
System.out.println("name: " + user.getName());
}
}