配置文件很重要,体现了好多知识:IOC,hibernate.cfg.xml里面的东西要怎么填到spring的配置?sessionFactory是如何注入的。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 11 12 <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> 13 <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> 14 <property name="url" value="jdbc:sqlserver://hzz:1433;databaseName=Heros"/> 15 <property name="username" value="sa"/> 16 <property name="password" value="caxa"/> 17 </bean> 18 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 19 <property name="dataSource" ref="dataSource"/> 20 <property name="mappingResources"> 21 <list> 22 <value>db/mapping/Employee.hbm.xml</value> 23 </list> 24 </property> 25 <property name="hibernateProperties"> 26 <props> 27 <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> 28 <prop key="hibernate.show_sql">true</prop> 29 <prop key="hibernate.format_sql">true</prop> 30 </props> 31 </property> 32 </bean> 33 34 <bean id="employeeDAO" class="db.dao.EmployeeDAOImpl"> 35 <property name="sessionFactory" ref="sessionFactory"></property> 36 </bean> 37 38 </beans>
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- 5 Mapping file autogenerated by MyEclipse Persistence Tools 6 --> 7 <hibernate-mapping> 8 <class name="db.pojo.Employee" table="employee" schema="dbo" catalog="Heros"> 9 <id name="id" type="java.lang.Integer"> 10 <column name="id" /> 11 <generator class="native" /> 12 </id> 13 <property name="name" type="java.lang.String"> 14 <column name="name" length="20" /> 15 </property> 16 <property name="deptId" type="java.lang.Integer"> 17 <column name="dept_id" /> 18 </property> 19 </class> 20 </hibernate-mapping>
package db.dao; import java.util.List; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.toplink.SessionFactory; import db.pojo.Employee; public class EmployeeDAOImpl implements EmployeeDAO { private HibernateTemplate template; public void setSessionFactory(org.hibernate.SessionFactory sessionFactory){ template = new HibernateTemplate(sessionFactory); } @Override public void delete(int id) { // TODO Auto-generated method stub } @Override public List<Employee> findAll() { // TODO Auto-generated method stub return template.find("from Employee"); } @Override public void save(Employee emp) { // TODO Auto-generated method stub template.save(emp); } }
package db.pojo; // default package /** * Employee entity. @author MyEclipse Persistence Tools */ public class Employee implements java.io.Serializable { // Fields private Integer id; private String name; private Integer deptId; // Constructors /** default constructor */ public Employee() { } /** full constructor */ public Employee(String name, Integer deptId) { this.name = name; this.deptId = deptId; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Integer getDeptId() { return this.deptId; } public void setDeptId(Integer deptId) { this.deptId = deptId; } }
package db.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import db.dao.EmployeeDAO; import db.pojo.Employee; public class TestHibernate { @Test public void testSave(){ Employee emp = new Employee(); emp.setName("hym"); emp.setDeptId(1); ApplicationContext ac = new ClassPathXmlApplicationContext("spring-hibernate.xml"); EmployeeDAO employeeDAO = (EmployeeDAO) ac.getBean("employeeDAO"); employeeDAO.save(emp); System.out.println(employeeDAO.findAll()); } }
sping整合hibernate小例子,对Employee表实现增删改查
原文:http://www.cnblogs.com/hzzhero/p/5155940.html