Spring MVC+hibernate+Spring的框架搭建中遇到的问题总结

来源:互联网 发布:js window.onload事件 编辑:程序博客网 时间:2024/05/27 16:42

每个本地的环境都会大同小异,也许一个小小的配置不同,就会导致运行结果不一样。

先报一下我本地的配置,以供参考:

jdk1.7(刚开始我用的是1.8,但在运行中和spring3不兼容,我半路换的1.7);

spring3

hibernate4

springmvc3

oracle12C

tomcat7

jdbc驱动包:jdbc7(支持oracle12c)

首先理下思路:配置文件之间的关系:

Spring的xml:spring-context.xml

springmvc的xml:spring-mvc.xml

hibernate的xml:hibernate.cfg.cml

 

1、新建工程的首先读取的配置文件是全局文件:web.xml,启动一个项目的时候,首先被装配的是web.xml,所以web.xml中的配置很重要。在web.xml中配置代码

 

web.xml------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
  <display-name>SpringMVC</display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- 配置Spring -->  
  <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath*:config/spring-*.xml</param-value>   //关于spring文件的引入规则
  </context-param>
  <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener> 
   
   
  <!-- 配置SpringMVC --> 
  <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*:config/spring-mvc.xml</param-value>   //这个文件要跟你config下的spring文件名相同
    </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> 
  
   
  <!-- 控制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-app>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

目录结构如下:

 

spring-mvc.xml--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?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"> 
 
    <!-- 注解扫描的包 --> 
    <context:component-scan base-package="com.mail" /> 
 
    <!-- 开启注解方案1 --> 
    <!-- 注解方法处理 --> 
    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  
        /> --> 
    <!-- 注解类映射处理 --> 
    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> --> 
 
    <!-- 开启注解方案2 --> 
    <mvc:annotation-driven /> 
 
    <!-- 静态资源访问,方案1 --> 
    <mvc:resources location="/img/" mapping="/img/**" /> 
    <mvc:resources location="/js/" mapping="/js/**" /> 
 
    <!-- 静态资源访问,方案2 --> 
    <!-- <mvc:default-servlet-handler/> --> 
 
    <!-- 视图解释类 --> 
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/"></property> 
        <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> 
        <property name="suffix" value=".jsp"></property> 
    </bean> 
 
    <!-- 上传文件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> --> 
</beans>

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

spring-hibernate.xml----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?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"> 
     
    <!-- 配置数据源 --> 
    <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" /> 
        <property name="username" value="C##MAILSHEMATEST" /> 
        <property name="password" value="m123456" /> 
    </bean> 
 
    <!--  配置hibernate SessionFactory--> 
    <bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <property name="hibernateProperties"> 
            <props> 
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
                <prop key="hibernate.hbm2ddl.auto">update</prop> 
                <prop key="hibernate.show_sql">true</prop> 
                <prop key="hiberante.format_sql">true</prop> 
            </props> 
        </property> 
        <property name="configLocations"> 
            <list> 
                <value> 
                    classpath*:config/hibernate/hibernate.cfg.xml 
                </value> 
            </list> 
        </property> 
    </bean> 
 
    <!-- 事务管理器 --> 
    <bean id="transactionManager" 
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
     
    <!-- 事务代理类 --> 
    <bean id="transactionBese" 
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
        lazy-init="true" abstract="true"> 
        <property name="transactionManager" ref="transactionManager"></property> 
        <property name="transactionAttributes"> 
            <props> 
                <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> 
                <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> 
                <prop 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</prop> 
                <prop key="get*">PROPAGATION_NEVER</prop> 
            </props> 
        </property> 
    </bean> 
 
</beans>   

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

spring-core.xml----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?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"> 
      
    <!-- 引入其他配置文件,可以为多个 --> 
   
    <import resource="classpath*:config/spring-hibernate.xml"/> 
    <import resource="classpath*:config/spring-mvc.xml"/> 
    <import resource="classpath*:config/spring/spring-mail.xml"/>
     
 </beans>    

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

spring-mail.xml---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 3.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"

<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> 
]> 
 
<beans> 
    <!-- Spring Bean --> 
    <bean id="mailDao" class="com.mail.dao.impl.MailDaoImpl"> 
        <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
     
    <bean id="mailServiceImpl" class="com.mail.service.impl.MailServiceImpl"> 
        <property name="mailDao" ref="mailDao"></property> 
    </bean> 
     
    <!-- parent为transactionBese,表示支持事务 --> 
    <bean id="mailService" parent="transactionBese"> 
        <property name="target" ref="mailServiceImpl"></property> 
    </bean> 
     
</beans> 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

hibernate.cfg.xml-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 引入需要映射的类 -->
        <mapping class="com.mail.entity.Mail"/>
    </session-factory>
</hibernate-configuration>

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

jdbc.properties-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.username=C##MAILSHEMATEST
jdbc.password=m123456

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

log4j.properties-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

# This is the configuring for logging displayed in the Application Server
log4j.rootCategory=DEBUG,stdout,logfile

#stdout configure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n

#logfile configure
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/mail.log
#log4j.appender.logfile.File=/WEB-INF/logs/ssm.log
#\u65E5\u5FD7\u6587\u4EF6\u7684\u8DEF\u5F84\u548C\u540D\u79F0
#log4j.appender.R.File=./../logs/ssm.log
log4j.appender.logfile.MaxFileSize=10M
log4j.appender.logfile.MaxBackupIndex=100
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n

#debug log for spring
log4j.logger.org.springframework=DEBUG

# debug log for ibatis
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

0 0
原创粉丝点击