http://blog.sina.com.cn/s/blog_5198c7370100oti1.html
Spring整合Hibernate的方案如果细分有很多种,以下为本人配置的其中一种
此方式不用Hibernate的配置文件,统一集成在spring的配置文件中beans.xml,
另外该环境是普通java环境而非web环境
实体类
public class Tiger {
Integer id;
String name;
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;
}
}
dao操作类
public interface TigerDAO {
public void save(Tiger tiger);
public void save2(Tiger tiger1,Tiger tiger2);
}
public class TigerDaoImpl extends HibernateDaoSupport implements TigerDAO {
public void save(Tiger tiger){
getHibernateTemplate().save(tiger);
}
public void save2(Tiger tiger1,Tiger tiger2){
System.out.println("insert begin");
getHibernateTemplate().save(tiger1);
System.out.println("insert 1");
getHibernateTemplate().save(tiger2);
System.out.println("insert 2");
}
}
Tiger的Tiger.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ssh2.entity">
<class name="Tiger" table="Tiger">
<id name="id" column="id">
<generator class="assigned" />
</id>
<property name="name" column="name" not-null="true"
length="20" />
</class>
</hibernate-mapping>
spring的配置文件(将其放于Src根目录下(本人工程为MyEclipse工程))
<?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/zoon"></property>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">