1.SessionFatory注入 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">--
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/demo">
</property>
<property name="username" value="caterpillar"></property>
<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="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/po/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDAO" class="com.po.UserDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
/*
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs">
</property>
<property name="username" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/po/Dep.hbm.xml</value>
<value>com/po/Emp.hbm.xml</value>
<value>com/po/Emplove.hbm.xml</value>
<value>com/po/Love.hbm.xml</value>
</list>
</property>
</bean>
<bean id="EmploveDAO" class="com.po.EmploveDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
*/
</beans>
2.SpringHibernateDemo.java文件
package com.po;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringHibernateDemo
{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
IUserDAO userDAO = (IUserDAO)context.getBean(userDAO);
User user = new User();
user.setName("accp");
user.setAge(new Integer(20));
userDAO.insert(User);
user = userDAO.find(new Integer(1));
System.out.println("name:"+user.getName());
}
}
3.HibernateTemplate UserDAO.java文件
package com.po;
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 = (User) hibernateTemplate.get(User.class,id);
return user;
}
}