扯淡:
30篇啦!从2012-08-15开始的系列,东平西凑将近一年的时间也就这么几篇。目标的100篇,按这个速度也要再搞两年呢。
发博客果然不是件容易的事,怪不得更多的人愿意玩微博,125个字,写一个字也可以发了。
向那些依然坚持稳定码博客的朋友致敬!
主题:
用spring整合hibernate也算是java web开发的入门必学的东西了,多年下来没怎么用过hibernate。
所以记录下基础的整合知识,以及如何构建一些共通的代码,减少dao层的工作量。
项目使用maven构建,关于maven的构建知识可以参考:摸我
整合只使用了一个配置文件,hibernate方面使用注解方式映射数据库表。
data-source.xml:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd " default-autowire="byName"> <context:component-scan base-package="com.sz.sh.dal.dao.hibernate,com.sz.sh.dal.dao.dateobject"> </context:component-scan> <!-- 使用常规的BasicDataSource 来配置DataSource--> <bean id="shDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="${sh.jdbc.url}" /> <property name="username" value="${sh.jdbc.username}" /> <property name="password" value="${sh.jdbc.password}" /> <property name="initialSize" value="1" /> <property name="maxIdle" value="30" /> <property name="maxWait" value="10000" /> <property name="minIdle" value="1" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="180" /> <!-- validate connection 检测配置 --> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="validationQuery"> <value>SELECT @@SQL_MODE</value> </property> <property name="numTestsPerEvictionRun"> <value>30</value> </property> <property name="timeBetweenEvictionRunsMillis"> <value>60000</value> </property> <property name="minEvictableIdleTimeMillis"> <value>1800000</value> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="shDataSource" /> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager" /> </bean> <bean id="shSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="shDataSource" /> <property name="packagesToScan"> <list> <value>com.sz.sh.dal.dao.dateobject</value> </list> </property> <!-- 如果用xml来描述hibernate的bean,就需要以下的配置 <property name="mappingResources"> <list> <value>cn/sh/model/user.hbm.xml</value> </list> </property> --> <property name="hibernateProperties"> <props> <prop key="hibernate.default_schema">wxassistant</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.query.substitutions">false</prop> <prop key="hibernate.default_batch_fetch_size">20</prop> <prop key="hibernate.max_fetch_depth">2</prop> <prop key="hibernate.generate_statistics">true</prop> </props> </property> </bean> <aop:aspectj-autoproxy expose-proxy="true" /><!-- 开启注解事务 只对当前配置文件有效 --> <tx:annotation-driven transaction-manager="txManager" /> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="shSessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="merge*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="put*" propagation="REQUIRED" /> <tx:method name="use*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"><!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.sz.sh.dal.dao.hibernate.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 --> <aop:config expose-proxy="true"> <aop:pointcut id="basePointcut" expression="execution(* com.sz.sh.dal.common.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="basePointcut" /> </aop:config> </beans>