簡單的spring3.2.9和hibernate3的整合配置,有demo供下載。shTest下載 第一步
jdbc.properties配置
driverClassName=com.mysql.jdbc.Driverurl=jdbc\:mysql\://localhost\:3306/shtestusername=rootpassword=123456prototypeCount=1maxActive=100 houseKeepingSleepTime=60000minimumConnectionCount=2maximumConnectionCount=8simultaneousBuildThrottle=4alias=springProxool
第二步
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "><context:annotation-config/> <context:component-scan base-package="com.cqut"/> <!-- 指定資料庫設定檔的位置 --> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver" value="${driverClassName}"/> <property name="driverUrl" value="${url}"/> <property name="user" value="${username}"/> <property name="password" value="${password}"/> <!-- 測試的SQL執行語句 --> <property name="houseKeepingTestSql" value="select CURRENT_DATE"/> <!-- 最少保持的空閑串連數 (預設2個) --> <property name="prototypeCount" value="${prototypeCount}"/> <!-- proxool自動偵察各個串連狀態的時間間隔(毫秒),偵察到閒置串連就馬上回收,逾時的銷毀 預設30秒) --> <property name="houseKeepingSleepTime" value="${houseKeepingSleepTime}"/> <!-- 最大啟用時間(超過此時間軸程將被kill,預設為5分鐘) <property name="maximumActiveTime" value="${maximumActiveTime}"/>--> <!-- 串連最長時間(預設為4個小時) <property name="maximumConnectionLifetime" value="${maximumConnectionLifetime}"/>--> <!-- 最小串連數 (預設2個) --> <property name="minimumConnectionCount" value="${minimumConnectionCount}"/> <!-- 最大串連數 (預設5個) --> <property name="maximumConnectionCount" value="${maximumConnectionCount}"/> <!--統計 <property name="statistics" value="${statistics}"/> --> <!-- 別名 --> <property name="alias" value="${alias}"/> <!--同時最大串連數 --> <property name="simultaneousBuildThrottle" value="${simultaneousBuildThrottle}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="packagesToScan"><list> <value>com.cqut.entity</value> </list></property><!--<property name="annotatedPackages"><list><value>com.cqut.entity.Department</value><value>com.cqut.entity.MyClass</value><value>com.cqut.entity.Student</value></list></property>--><property name="hibernateProperties"> <props> <!-- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hiberante.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property></bean><bean id="baseDao" class="com.cqut.dao.BaseDao"><property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref local="sessionFactory" /></property> </bean> <!-- 通過註解的方式,進行實物管理 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
第三步
建立實體,配置註解
package com.cqut.entity;import java.io.Serializable;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;@Entitypublic class Student implements Serializable{/** * */private static final long serialVersionUID = 1L;@Id@GeneratedValueprivate int stuId;@Column(length=20,nullable=false)private String name;@Columnprivate int age;@ManyToOne(cascade=CascadeType.ALL)private MyClass myClass;<span style="white-space:pre"></span>//get set方法省略}
第四步
配置basedao,直接繼承hibernate的daosupport
package com.cqut.dao;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class BaseDao extends HibernateDaoSupport{}
第五步
配置studentDao,及添加註解和配置事務
package com.cqut.dao;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;import com.cqut.entity.Student;@Transactional@Component("studentDao")public class StudentDao {@Resourceprivate BaseDao baseDao;public BaseDao getBaseDao() {return baseDao;}public void setBaseDao(BaseDao baseDao) {this.baseDao = baseDao;} @Transactional(rollbackFor={Exception.class})public void add(Student student) throws Exception{baseDao.getHibernateTemplate().save(student);throw new Exception("異常");}public List<Student> getStudents(String hql){return (List<Student>)baseDao.getHibernateTemplate().find(hql);}}注意:“異常”是為了測試事務的復原
第六步
測試
package com.cqut.test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cqut.dao.StudentDao;import com.cqut.entity.Student;public class Test {public static void main(String[] args) {Student student = new Student();student.setName("張三");student.setAge(19);ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");StudentDao studentDao = context.getBean(StudentDao.class);try {studentDao.add(student);} catch (Exception e) {e.printStackTrace();}List<Student> students = studentDao.getStudents("from Student");for (Student student2 : students) {System.out.println(student2.getName());}}}
最後是web.xml的配置
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SHTest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- proxool資料庫統計 --><servlet><servlet-name>Admin</servlet-name><servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class></servlet><servlet-mapping><servlet-name>Admin</servlet-name><url-pattern>/adminProxool</url-pattern></servlet-mapping><!-- spring 啟動 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- spring 字元過濾 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>