spring整合web的ssh(springMVC、hibernate)

时间:2023-03-09 18:10:05
spring整合web的ssh(springMVC、hibernate)

1. tomcat启动时,加载配置文件,将bean装在

  导入jar包spring-web..jar

spring整合web的ssh(springMVC、hibernate)

2.确定配置文件位置

3.spring整合hibernate

<!-- 加载hibernate.cfg.xml,获取SessionFactory (HibernateTemplate底层也是使用Session) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 注册hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 注入模板 -->
<bean id="userDao" class="com.hgblogs.dao.UserDao">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> <!-- 事务管理 -->
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务详情 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="addUser" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="pc" expression="execution(* com.hgblogs.service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"></aop:advisor>
</aop:config> <!-- 自动扫描 -->
<context:component-scan base-package="com.hgblogs">
<!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

applicationContext.xml

<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 数据库方言 -->
<property name="connection.url">
jdbc:mysql://localhost:3306/spring
</property><!-- 数据库连接URL -->
<property name="connection.username">root</property> <!-- 数据库用户名 -->
<property name="connection.password">123</property> <!-- 数据库用户密码 -->
<property name="connection.driver_class"> <!-- 数据库驱动类 -->
com.mysql.jdbc.Driver
</property> <property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- <property name="hbm2ddl.auto"></property> -->
<property name="hbm2ddl.auto">update</property> <mapping class="com.hgblogs.entity.Users"/> </session-factory>
</hibernate-configuration>

hibernate.cfg.xml