首页 > 代码库 > springmvc-hibernate

springmvc-hibernate

技术分享

---------------------------------以上是整合两个框架需要的jar包------------------------------------------

 

 

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    id="WebApp_ID" version="2.5">    <display-name>hibernate-Spring</display-name>    <welcome-file-list>        <welcome-file>index.html</welcome-file>        <welcome-file>index.htm</welcome-file>        <welcome-file>index.jsp</welcome-file>        <welcome-file>default.html</welcome-file>        <welcome-file>default.htm</welcome-file>        <welcome-file>default.jsp</welcome-file>    </welcome-file-list>    <!-- 监听器-->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>            <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>        /WEB-INF/classes/applicationContext.xml        </param-value>    </context-param>    <servlet>        <servlet-name>mySpring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>              /WEB-INF/classes/mySpring-servlet.xml          </param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>mySpring</servlet-name>        <url-pattern>*.do</url-pattern>    </servlet-mapping>    <!-- hibernate4.0必须 sessionFactory-->    <filter>        <filter-name>hibernateFilter</filter-name>        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>        <init-param>            <param-name>sessionFactoryBeanName</param-name>            <param-value>sessionFactory</param-value>        </init-param>        <init-param>            <param-name>singleSession</param-name>            <param-value>true</param-value>        </init-param>        <init-param>            <param-name>flushMode</param-name>            <param-value>AUTO</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>hibernateFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>        <!-- 编码过滤,所有编码集改为utf.8 -->    <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>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>

---------------------------------以上web.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/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.2.xsd">    <!--启用自动扫描,注解,在指定位置dao|service.impl -->    <context:component-scan base-package="com.wode">         <context:exclude-filter type="regex" expression=".(dao|service.impl)"             />     </context:component-scan>        <!-- 开启切面注解-->    <aop:aspectj-autoproxy />        <!-- 配置C3P0数据源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"        destroy-method="close">        <property name="driverClass" value="http://www.mamicode.com/com.mysql.jdbc.Driver" />        <property name="jdbcUrl"            value="jdbc:mysql://localhost:3306/j116?characterEncoding=utf-8" />        <property name="user" value="http://www.mamicode.com/root" />        <property name="password" value="http://www.mamicode.com/admin" />        <property name="minPoolSize" value="http://www.mamicode.com/1" />        <property name="maxPoolSize" value="http://www.mamicode.com/20" />        <property name="maxIdleTime" value="http://www.mamicode.com/60" />        <property name="initialPoolSize" value="http://www.mamicode.com/1" />        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->        <property name="acquireIncrement" value="http://www.mamicode.com/5" />        <property name="automaticTestTable" value="http://www.mamicode.com/c3p0TestTable" />        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->        <property name="idleConnectionTestPeriod" value="http://www.mamicode.com/60" />        <property name="checkoutTimeout" value="http://www.mamicode.com/3000" />    </bean>    <!-- 配置SessionFactory -->    <bean id="sessionFactory"        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <!-- 向SessionFactory中注入数据源 -->        <property name="dataSource" ref="dataSource" />        <property name="hibernateProperties">            <props>                <!-- 定义Hibernate的方言 -->                <prop key="hibernate.dialect">                    org.hibernate.dialect.MySQLDialect                </prop>                <!-- 是否根据需要每次自动更新数据库 <prop key="hibernate.hbm2ddl.auto">update</prop> -->                <!-- 控制台显示SQL -->                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.format_sql">true</prop>                <!-- 使用SQL注释 -->                <prop key="hibernate.use_sql_comments">true</prop>            </props>        </property>        <!-- 浏览entitys包下的所有使用Hibernate注解的JavaBean -->        <property name="packagesToScan">            <list>                <value>com.wode.bean</value>            </list>        </property>     </bean>    <bean id="transactionManager"        class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <!-- 定义个通知,指定事务管理器 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="delete*" propagation="REQUIRED" read-only="false"                rollback-for="java.lang.Exception" />            <tx:method name="save*" propagation="REQUIRED" read-only="false"                rollback-for="java.lang.Exception" />            <tx:method name="update*" propagation="REQUIRED" read-only="false"                rollback-for="java.lang.Exception" />            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />        </tx:attributes>    </tx:advice>        <!--切面 -->    <aop:config>        <aop:pointcut id="userPointCut" expression="execution(* com.wode.service.*.*(..))" />        <aop:aspect id="logAspect" ref="userLogger">            <aop:before method="testLogger" pointcut-ref="userPointCut"></aop:before>        </aop:aspect>    </aop:config>    </beans>

---------------------------------以上applicationContext.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:p="http://www.springframework.org/schema/p"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.2.xsd    http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">        <!-- 开启springmvc注解 -->    <mvc:annotation-driven />        <!-- 扫描所有的管理器 -->    <context:component-scan base-package="com.wode.controller" />            <!-- ViewResolver视图解析器 用于将返回的ModelAndView对象进行分离 -->    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">        <property name="viewClass"            value="org.springframework.web.servlet.view.JstlView"></property>        <property name="prefix" value="http://www.mamicode.com/"></property>        <property name="suffix" value="http://www.mamicode.com/.jsp"></property>    </bean></beans>

---------------------------------以上springmvc-servlet.xml需要配置的东西------------------------------------------

 

springmvc-hibernate