1124刘润杭hibernate与spring整合Word格式文档下载.docx
《1124刘润杭hibernate与spring整合Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《1124刘润杭hibernate与spring整合Word格式文档下载.docx(7页珍藏版)》请在冰点文库上搜索。
xsi:
schemaLocation="
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx-3.2.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop-3.2.xsd"
<
!
--引入属性文件-->
bean
class="
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
<
propertyname="
locations"
value="
classpath:
jdbc.properties"
/>
/bean>
--配置数据源-->
beanname="
dataSource"
class="
mons.dbcp.BasicDataSource"
destroy-method="
close"
driverClassName"
${jdbc.driver}"
url"
${jdbc.url}"
username"
${jdbc.username}"
password"
${jdbc.password}"
--初始化连接大小-->
initialSize"
0"
--连接池最大使用连接数量-->
maxActive"
20"
--连接池最大空闲-->
maxIdle"
--连接池最小空闲-->
minIdle"
--获取连接最大等待时间-->
maxWait"
60000"
validationQuery"
${validationQuery}"
testOnBorrow"
false"
testOnReturn"
testWhileIdle"
true"
--配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒-->
timeBetweenEvictionRunsMillis"
--配置一个连接在池中最小生存的时间,单位是毫秒-->
minEvictableIdleTimeMillis"
25200000"
--打开removeAbandoned功能-->
removeAbandoned"
--1800秒,也就是30分钟-->
removeAbandonedTimeout"
1800"
--关闭abanded连接时输出错误日志-->
logAbandoned"
--配置hibernatesession工厂-->
beanid="
sessionFactory"
org.springframework.orm.hibernate4.LocalSessionFactoryBean"
ref="
/property>
hibernateProperties"
<
props>
<
propkey="
hibernate.dialect"
${hibernate.dialect}<
/prop>
hibernate.hbm2ddl.auto"
${hibernate.hbm2ddl.auto}<
hibernate.show_sql"
${hibernate.show_sql}<
hibernate.format_sql"
${hibernate.format_sql}<
/props>
--扫描hibernate注解配置的entity
packagesToScan"
com.whxh.news.model"
-->
--下面是配置文件的方式-->
mappingLocations"
value>
/com/whxh/news/model/*.hbm.xml<
/value>
--配置事务管理器-->
transactionManager"
org.springframework.orm.hibernate4.HibernateTransactionManager"
--注解方式配置事物-->
tx:
annotation-driventransaction-manager="
--拦截器方式配置事务-->
adviceid="
transactionAdvice"
transaction-manager="
attributes>
methodname="
add*"
propagation="
REQUIRED"
save*"
update*"
modify*"
edit*"
delete*"
remove*"
get*"
SUPPORTS"
read-only="
find*"
*"
/tx:
advice>
--配置aop-->
aop:
config>
pointcutexpression="
execution(*com.whxh.service.*.*(..))"
id="
transactionPointcut"
advisoradvice-ref="
pointcut-ref="
/aop:
/beans>
Web.xml中
servlet>
servlet-name>
spring<
/servlet-name>
servlet-class>
org.springframework.web.servlet.DispatcherServlet<
/servlet-class>
init-param>
param-name>
contextConfigLocation<
/param-name>
param-value>
classpath*:
applicationContext.xml,
classpath*:
applicationContext-mvc.xml,
spring-hibernate.xml<
/param-value>
/init-param>
load-on-startup>
1<
/load-on-startup>
/servlet>
servlet-mapping>
url-pattern>
/<
/url-pattern>
/servlet-mapping>
建立实体类的xml
DOCTYPEhibernate-mappingPUBLIC
"
-//Hibernate/HibernateMappingDTD3.0//EN"
//www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
hibernate-mappingpackage="
classname="
AdminEntity"
table="
t_admin"
>
--id此id不是数据库种的字段,而是对当前数据的唯一标识-->
idname="
id"
column="
--指明自增的方式-->
generatorclass="
native"
/generator>
/id>
upass"
name"
status"
/class>
/hibernate-mapping>