Spring3 集成 Hibernate4 配置

来源:互联网 发布:网络女主播经常唱的 编辑:程序博客网 时间:2024/05/22 00:20

1.创建动态web工程

2.添加jar包

3.在WEB-INF下编写配置文件web.xml

4.编写关于数据库连接的配置,配置文件database.properties 及 applicationContext-spring.xml

5.编写配置文件spring-mvc.xml


1.创建动态web工程

2.添加jar包

spring相关jar包    11


集成Hibernate相关jar包  11


集成aop  相关jar包 5


转json jar包  3


jstl相关jar包  2


上传 jar包 1


3.在WEB-INF下编写配置文件web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"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>SpringMVC</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:conf/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 如果spring容器的某个bean的scope作用域为:request、session和global session,在需要添加下面的监听器 --><listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><!-- 解决内存泄漏的问题 --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><servlet>   <servlet-name>springMVC</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   <init-param>       <param-name>contextConfigLocation</param-name>       <param-value>classpath:spring-mvc.xml</param-value>   </init-param>   <load-on-startup>1</load-on-startup></servlet><servlet-mapping>       <servlet-name>springMVC</servlet-name>       <url-pattern>/</url-pattern></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>   <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>

4.编写关于数据库连接的配置,配置文件database.properties 及 applicationContext-spring.xml

database.properties 

jdbc.driver = com.mysql.jdbc.Driverjdbc.url = jdbc:mysql://localhost:3306/java_base?autoReconnect=true&useUnicode=true&characterEncoding=utf-8jdbc.user = rootjdbc.password = leiwenkang

applicationContext-spring.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:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd    http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">         <!-- 定义受环境影响易变的变量 -->    <context:property-placeholder location="classpath:/conf/database.properties" />           <context:component-scan base-package="com.eshore.*">        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /><!-- 将Controller的注解排除掉,在spring-mvc.xml单独扫描Controller注解 -->    </context:component-scan>        <!--数据源  -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <!-- 基本属性 url、user、password -->        <property name="driverClass" value="${jdbc.driver}" />        <property name="jdbcUrl" value="${jdbc.url}" />        <property name="user" value="${jdbc.user}" />        <property name="password" value="${jdbc.password}" />   </bean>      <!--会话工厂  -->   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">      <property name="dataSource" ref="dataSource"/>      <property name="hibernateProperties">          <props>              <prop key ="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>      <prop key ="hbm2ddl.auto">none</prop>      <prop key ="show_sql">true</prop>      <prop key ="format_sql">true</prop>      <!--hibernate 4以上版本需要以下配置  -->      <prop key ="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>          </props>      </property>      <property name="packagesToScan" value="com.eshore.*" />   </bean>       <!-- 事务管理器配置, Jpa单数据源事务 -->    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <!-- 使用annotation扫描定义事务 -->    <tx:annotation-driven transaction-manager="transactionManager" />    </beans>

5.编写配置文件spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">            <!--配置静态资源信息  -->       <mvc:resources mapping="/resources/**" location="/resources/" />        <!--配置扫描Controller的路径  -->        <context:component-scan base-package="com.eshore.*">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/><!-- 在springMVC配置文件中将Service注解给去掉,而由applicationContext.xml父容器进行初始化以保证service的事务的增强处理 -->        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>    </context:component-scan>        <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射,解决@ResponseBody乱码问题, 需要在annotation-driven之前,否则乱码问题同样无法解决 -->    <!-- Spring3.1推荐使用RequestMappingHandlerAdapter -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="stringHttpMessageConverter" />                <ref bean="mappingJacksonHttpMessageConverter" />            </list>        </property>    </bean>         <mvc:annotation-driven />         <!-- 处理JSON数据转换,为了处理返回的JSON数据的编码,默认是ISO-88859-1的,这里把它设置为UTF-8,解决有乱码的情况 -->    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html;charset=UTF-8</value>  <!-- IE FireFox -->                 <value>application/json;charset=UTF-8</value> <!-- Chrome -->            </list>        </property>    </bean>        <!-- 处理字符串数据 -->    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html;charset=UTF-8</value>            </list>        </property>    </bean>        <!-- 视图解析器    id名必须为"viewResolver" -->     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <property name="prefix" value="/WEB-INF/pages/"/>          <property name="suffix" value=".jsp"/>     </bean>         <!-- 文件上传    id名必须为"multipartResolver"-->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <property name="defaultEncoding" value="UTF-8"/>     <property name="maxUploadSize" value="-1"/><!-- 不限制上传大小 -->      <property name="maxInMemorySize" value="409600"/><!--在内存中的最大大小  -->    </bean>     <!--    拦截器 -->    <mvc:interceptors><mvc:interceptor><mvc:mapping path="/**" /><mvc:exclude-mapping path="/resources/**"/><mvc:exclude-mapping path="/"/><!-- mvc3.2以后版本才可使用exclude --> <mvc:exclude-mapping path="/doLogin"/><mvc:exclude-mapping path="/logout"/><bean class="com.eshore.sh.interceptor.LoginInterceptor" /></mvc:interceptor></mvc:interceptors>    </beans>





0 0
原创粉丝点击