Configuration of springMVC+spring+Hibernate

This example uses springMvc hibernation and spring to integrate, and uses springmvc to replace the struts in the original ssh (struts, spring, hibernate) to play the role of the view layer... The
example is built with reference to the examples of several predecessors: refer to 1, refer to 2, thank you Their sharing, useful blog.
The following describes the diagram of the project structure:
structure one:
structure two:
project structure 1 project structure 2

web.xml code is as follows:
[html] view plain copy and print?
<?xml version="1.0" encoding="utf-8" ?> 
<web version="3.0"  
    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_3_0.xsd”> 
  <displayname></displayname >  
  <welcome file list> 
    <welcome file>index.jsp<//welcome file> 
  </welcome file list> 
   
  <!  
  <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <parameter value> classpath *:config/spring-*.xml</parameter value> 
  </context-param> 
   
   
  <listener> 
     <listening Loader class>org.springframework.web.context.ContextLoaderListener</listener class> 
  </listener> 
   
   
  <!--config SpringMVC --> 
  <servlet> 
    <servlet-name>spring</servlet-name> 
    < servlet class>org.springframework.web.servlet.DispatcherServlet</servlet class> 
    <initialization> 
      <param-name>contextConfigLocation</param-name> 
      <parameter value>classpath *:config/spring-servlet.xml</parameter value > 
    </initialization>   
    <load-on-startup>1< /load-on-startup> 
  < /servlet> 
   
  <servlet映射> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern> 
  </servlet-mapping> 
   
  <!--set charset--> 
  <filter> 
    <filter name>encodingFilter</filter filter name> 
    <filter class>org.springframework.web.filter.CharacterEncodingFilter</filter class> 
    <init> 
      <param-name>encoding</param-name> 
      <parameter value>utf-8</parameter value> 
    </init> 
    <init> 
      <param-name>forceEncoding</param-name> 
      <parameter value>true</parameter value> 
    </init> 
  </filter> 
  <filter-mapping> 
    <filter name>encodingFilter </filter name> 
    <url-pattern>/*</url-pattern> 
  < /filter-mapping> 
  
   
  <!--switch to control session --> 
  <filter> 
        <filter name>openSession</filter name> 
        <filter class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</ filter class> 
  </filter> 
   
  <filter-mapping> 
    <filter name>openSession </filter name> 
    <url-pattern>/*</url-pattern> 
  </filter-mapping> 
</web>  The




spring-user.xml
code is as follows:
(java) view plain copy print?
<pre name=" code" class="html"> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE bean public" -- // SPRING // DTD bean 2.0 // EN" "http:// www.springframework.org/dtd/spring-beans-2.0.dtd" (
<!entity contextInclude system "org/springframework/web/context/WEB-INF/contextInclude.xml"> 
]> 
 
<bean>
< !——Spring Bean——>
<bean id="userDao" class="com.edw.dao.UserDao"> 
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
     
<bean id="userManagerBase" class=" com.edw.service.UserManager”> 
<property name="userDao" ref="userDao"> </property>
</bean>
     
<!--parent is transactionBese, indicating support for transactions -->
<bean id="userManager "parent="transactionBese"> 
<property name="target" ref="userManagerBase"> </property>
</bean>
     
</bean>

hibernate-cfg.xml code is as follows:
(java) view plain copy print?
 
( java) view plain copy print?
<pre name="code" class="html"> <!DOCTYPE hibernate config
        " -- // Hibernate / Hibernate config 3.0 DTD // EN " 
        "http://www.hibernate .  org/dtd/hibernate-configuration-3.0.dtd”> 
 
< hibernate配置>
<session factory>
<!--Introduce the class that needs to be mapped -->
<mapping class="com.edw.entity.User" />
</session factory>
</hibernate configuration>



spring-core.xml The code is as follows:
[ html] view plain copy print?
<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"   
xmlns:context="http ://www.springframework.org/schema/context"   
xmlns:p="http://www.springframework.org/schema/p"   
xmlns:mvc="http://www.springframework.org/schema/mvc "   
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-3.0.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context.xsd   
      http://www.springframework.org/schema/mvc   
http://www .springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
      
    <!--Introduce additional configuration files, can be multiple --> 
    <import-resources="classpath*:config/spring/spring-user. xml" /> 
     
</beans> 

spring-hibernate. The xml code is as follows:
[html] view plain copy print?
<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http:/ /www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework. org/schema/context/spring-context.xsd   
      http://www.springframework.org/schema/mvc   
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd”> 
     
    <!— --config datasource --> 
    <bean id="datasource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${jdbc.driverClassName}" /> 
        <property name="url" value="${jdbc.url}" /> 
        <property name="username" value="${jdbc.username}" /> 
        <property name="password" value =" ${ jdbc.password } " / > 
    </ bean > 
    < ! -- import the jdbc.properties file in the Src directory -- > 
    < background: property-placeholder location = "classpath: jdbc.properties" /> 
    < !--config hibernate SessionFactory --> 
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="datasource" ref="datasource" /> 
        <property's name="hibernateProperties"> 
            <props> 
                <props key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</props> 
                <prop key="auto">update</prop> 
                <prop key="hibernate.show_sql">true</prop> 
                <prop key="hiberante.format_sql">true</prop> 
            </prop> 
        </property> 
        <property name="configLocations"> 
            < list> 
                <value> 
classpath*:config/hibernate/hibernate.cfg.xml
                </value> 
            </list> 
        </property> 
    </bean> 
 
    <!--transaction-manager --> 
    <bean id="transactionManager" 
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
     
    <!--TransactionBese--> 
    <bean id="transactionBese" 
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
        lazy-init="true" digest="true"> 
        <property name="transactionManager" ref="transactionManager"></property> 
        <property name ="transactionAttributes"> 
            <props> 
                <props key="add*">PROPAGATION_REQUIRED, exception</props> 
                <props key="update*">PROPAGATION_REQUIRED, exception</props> 
                <props key="insert*">PROPAGATION_REQUIRED , exception</prop> 
                <prop key="modify*">PROPAGATION_REQUIRED, exception</prop> 
                <prop key="delete*">PROPAGATION_REQUIRED, exception</prop> 
                <prop key="del*">PROPAGATION_REQUIRED, exception </props> 
                <prop key="*">PROPAGATION_NEVER</prop> 
            </props> 
        </property> 
    </beans> 
 
</beans>   

spring-serlvet.xml code is as follows:
[html] view plain copy print?
<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework .org/schema/p" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context.xsd   
      http://www.springframework.org/schema/mvc   
http://www.springframework.org/schema/mvc/spring-mvc- 3.0.xsd”> 
 
    < !--Annotation scan package--> 
    <background:component-scan base package="com.edw.controller" /> 
 
    < !--Open annotation scheme 1--> 
    < !--Annotation method handler--> 
<!<beans="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  
        />---> 
    <!--annotation-class-mapping-handle--> 
    <!--<bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" > </bean>--> 
 
    <!--open annotation scheme 2--> 
    <mvc:annotation-driven /> 
 
    <!--Static Resource Access, Scheme 1--> 
    <mvc:resourcelocation="/img/" mapping="/img/* *" /> 
    <mvc:resourcelocation="/js/" mapping="/js/**" /> 
 
    <! -- static resources Access, Scenario 2 --> 
    <!--<mvc:default-servlet-handler/>> 
 
    <!--view-interpreter-class--> 
    <bean id="viewResolver" 
        class="org.springframework.web.servlet. view.InternalResourceViewResolver”> 
        <property name=”prefix” value=”/web-inf/jsp/”></property> 
        <!——can be null, it is convenient for the implementation to choose the view interpretation class according to the extension logic --> 
        <property name="suffix" value=.jsp"></property> 
    </bean> 
 
    <!--upload file bean --> 
<!<bean id="multipartResolver" class="org. springframework.web.multipart.commons.CommonsMultipartResolver” >  
        <   property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize"
        value="10485760000" /> <property name="maxInMemorySize" value="40960" />  
        </bean>---> 
 
</bean>   

jdbc.properties (test is the database name, change it according to your own database)
(Normal) view plain copy print?
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939554&siteId=291194637