我的笔记 Spring+SpringMVC+hibernate整合

    xiaoxiao2021-04-14  38

    Spring+SpringMVC+hibernate整合 wed.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   <display-name></display-name>   <welcome-file-list>     <welcome-file>index.jsp</welcome-file>   </welcome-file-list>   <!-- 以下为配置spring  -->   <!-- 配置spring listener -->   <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>       <!-- 上下文环境 配置文件位置 -->   <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>   </context-param>      <!-- 以下为配置spring mvc -->   <servlet>   <servlet-name>mvc</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>       <load-on-startup>1</load-on-startup> </servlet>   <servlet-mapping>   <servlet-name>mvc</servlet-name>   <url-pattern>*.do</url-pattern><!-- springmvc开发一般只拦.do文件 -->   </servlet-mapping>   <!-- 配置编码控制器 -->   <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> <!-- 配置OpenSessionInViewFilter,解决Hibernate的Session的关闭与开启问题--> <filter> <filter-name>openSessionInView</filter-name> <filter-class>     org.springframework.orm.hibernate3.support.OpenSessionInViewFilter                </filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> XX-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 让spring 去扫描类  建立关联 --> <!-- 是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用 --> <mvc:annotation-driven/> <!-- 扫苗controll包即下面的控制器 --> <context:component-scan base-package="com.hw.controller"></context:component-scan> <!-- 试图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <!-- 前缀 -->  <property name="prefix" value="/WEB-INF/per/"></property>  <!-- 后缀-->  <property name="suffix" value=".jsp"></property> </bean> <!-- 文件上传解析器 -->     <bean id="multipartResolver"         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">         <!-- one of the properties available; the maximum file size in bytes -->        <property name="defaultEncoding" value="utf-8" />         <property name="maxUploadSize" value="104857600"/>         <property name="maxInMemorySize" value="4096"/>     </bean> </beans> application.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" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-3.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.hw"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/cc3?useUnicode=true&characterEncoding=utf8"></property> <property name="username" value="root"></property> <property name="password" value="root" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="3" /> <!-- 连接池的最大值 --> <property name="maxActive" value="300" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 分开整合<property name="configLocation" value="classpath:hibernate.cfg.xml">  </property> --> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingResources"><!-- hibernate映射文件 --> <list> <value>com/hw/entity/Dept.hbm.xml</value> <value>com/hw/entity/Person.hbm.xml</value> <value>com/hw/entity/User.hbm.xml</value> </list> </property> </bean> <!-- xml方式配置 --> <bean id="transactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 激活自动代理功能 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <aop:config><!-- 定义一个切面,并将事务通知和切面组合 --> <aop:pointcut expression="execution(* com.hw.service.impl.*.*(..))" id="trPointcut" /> <aop:advisor advice-ref="trcut" pointcut-ref="trPointcut" /> </aop:config> <!-- 定义事务通知 --> <tx:advice id="trcut" transaction-manager="transactionManager"> <!-- 定义事务传播规则 --> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> <!-- 也可以对所有方法都应用REQUIRED事务规则 <tx:method name="*" propagation="REQUIRED"/> --> </tx:attributes> </tx:advice> <!-- 配置spring事务 基于全注解开发,只需在所需类前加上:@Transactional            不需要事务的方法前加上: @Transactional(propagation = Propagation.NOT_SUPPORTED)  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  <property name="sessionFactory" ref="sessionFactory"></property> </bean>  <bean id="transactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager">  <property name="sessionFactory" ref="sessionFactory"></property> </bean>  <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> --> </beans>
    转载请注明原文地址: https://ju.6miu.com/read-670144.html

    最新回复(0)