使用配置方式进行ssh的整合以及管理员管理的案例

发表于:2015-8-25 13:45

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:sf.zeng    来源:51Testing软件测试网采编

  一、Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接url地址 -->
<property name="connection.url">
jdbc:mysql://localhost:3306/spring
</property>
<!-- 数据库连接的用户名 -->
<property name="connection.username">root</property>
<!-- 数据库连接的用户密码 -->
<property name="connection.password">root</property>
<!-- 数据库连接驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- c3p0的配置 -->
<!-- hibernate4.org.hibernate.c3p0.internal.C3P0ConnectionProvider -->
<property name="hibernate.connection.provider_class">
org.hibernate.c3p0.internal.C3P0ConnectionProvider
</property>
<!-- 最小数量 -->
<property name="hibernate.c3p0.min_size">3</property>
<!-- 最大数量 -->
<property name="hibernate.c3p0.max_size">10</property>
<!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="hibernate.c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位 -->
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- 当连接池耗尽并接到获得连接的请求,则新增加连接的数量 -->
<property name="hibernate.c3p0.acquire_increment">5</property>
<!-- 缓存 Statement 对象的数量 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 是否每次连接都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<!-- 显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- hbm2ddl -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射文件 -->
<mapping resource="com/buslines/domain/Admin.hbm.xml" />
</session-factory>
</hibernate-configuration>
  二、Spring相关配置
  (1)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- hibernate c3p0 sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 创建模板对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 封装dao对象 -->
<bean id="hibernateDaoSupport"
class="org.springframework.orm.hibernate4.support.HibernateDaoSupport"
abstract="true">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<!--事务管理器 -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务管理器的通知 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.buslines.service..*.*(..))"
id="txpoint" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpoint" />
</aop:config>
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-action.xml"/>
</beans>
  (2)spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置dao对象 -->
<bean id="baseDaoImpl" class="com.buslines.dao.impl.BaseDaoImpl" parent="hibernateDaoSupport"/>
<bean id="adminDaoImpl" class="com.buslines.dao.impl.AdminDaoImpl" parent="baseDaoImpl"/>
</beans>
  (3)spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置dao对象 -->
<bean id="baseServiceImpl" class="com.buslines.service.impl.BaseServiceImpl">
<property name="baseDao" ref="baseDaoImpl" />
</bean>
<bean id="adminServiceImpl" class="com.buslines.service.impl.AdminServiceImpl" parent="baseServiceImpl">
<property name="adminDao" ref="adminDaoImpl" />
</bean>
</beans>
41/41234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号