springMVC+spring+Hibernate的配置

本实例采用springMvc冬眠 与 春天进行整合,用springmvc 取代了原先ssh(struts、spring、hibernate)中的struts来扮演视图层的角色……
实例搭建参照了几个前辈的例子:参照1  参照2,感谢他们的分享,很有用的博客。
下面介绍下项目结构的图:
结构一:
结构二:
项目结构1         项目结构2

web . xml代码如下:
[html] 视图平原 复制 打印吗?
< ?xml 版本=“1.0” 编码=“utf - 8”? > 
<web 版本=“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”> 
  <显示名称>< /显示名称>  
  <欢迎文件列表> 
    <欢迎文件>index . jsp< /欢迎文件> 
  < /欢迎文件列表> 
   
  < !——配置弹簧- - >  
  <context-param> 
        <param-name>contextConfigLocation< /param-name> 
        <参数值>classpath *:配置/ spring - * . xml< /参数值> 
  < /context-param> 
   
   
  <侦听器> 
     <侦听器类>org.springframework.web.context.ContextLoaderListener< /侦听器类> 
  < /侦听器> 
   
   
  < !——配置SpringMVC - - > 
  <servlet> 
    <servlet-name>春天< /servlet-name> 
    <servlet类>org.springframework.web.servlet.DispatcherServlet< /servlet类> 
    <初始化> 
      <param-name>contextConfigLocation< /param-name> 
      <参数值>classpath *:配置/ spring-servlet.xml< /参数值> 
    < /初始化> 
    <load-on-startup>1< /load-on-startup> 
  < /servlet> 
   
  <servlet映射> 
    <servlet-name>春天< /servlet-name> 
    <url模式>/< /url模式> 
  < /servlet映射> 
   
  < !——设置字符集- - > 
  <过滤器> 
    <过滤器名称>encodingFilter< /过滤器名称> 
    <筛选器类>org.springframework.web.filter.CharacterEncodingFilter< /筛选器类> 
    <初始化> 
      <param-name>编码< /param-name> 
      <参数值>utf - 8< /参数值> 
    < /初始化> 
    <初始化> 
      <param-name>forceEncoding< /param-name> 
      <参数值>真正的< /参数值> 
    < /初始化> 
  < /过滤器> 
  <filter-mapping> 
    <过滤器名称>encodingFilter< /过滤器名称> 
    <url模式>/ *< /url模式> 
  < /filter-mapping> 
  
   
  < !——控制会话的开关- - > 
  <过滤器> 
        <过滤器名称>openSession< /过滤器名称> 
        <筛选器类>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter< /筛选器类> 
  < /过滤器> 
   
  <filter-mapping> 
    <过滤器名称>openSession< /过滤器名称> 
    <url模式>/ *< /url模式> 
  < /filter-mapping> 
< /web> 




spring-user.xml
代码如下:
(java) 视图平原 复制 打印吗?
< pre的名字=“代码” 类=“html”> < ? xml version =“1.0”编码=“utf - 8”? >
< ! DOCTYPE bean公开”——/ / SPRING / / DTD豆2.0 / / EN” “http://www.springframework.org/dtd/spring-beans-2.0.dtd”(
< !实体contextInclude系统"org/springframework/web/context/WEB-INF/contextInclude.xml”> 
]> 
 
< bean >
< !——Spring Bean——>
< bean id =“userDao” 类=“com.edw.dao.UserDao”> 
<属性名=“sessionFactory”ref =“sessionFactory”> < /属性>
< /豆>
     
< bean id =“userManagerBase” 类=“com.edw.service.UserManager”> 
<属性名=“userDao”ref =“userDao”> < /属性>
< /豆>
     
< !——父母为transactionBese,表示支持事务- - >
< bean id =“userManager”父母=“transactionBese”> 
<属性名=“目标”ref =“userManagerBase”> < /属性>
< /豆>
     
< /豆>

hibernate-cfg.xml代码如下:
(java) 视图平原 复制 打印吗?
 
(java) 视图平原 复制 打印吗?
< pre的名字=“代码” 类=“html”> < ! DOCTYPE hibernate配置
        ”——/ / Hibernate / Hibernate配置3.0 DTD / / EN " 
        “http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”> 
 
< hibernate配置>
<会话工厂>
< !——引入需要映射的类- - >
<映射类=“com.edw.entity.User”/ >
< /会话工厂>
< / hibernate配置>



spring-core.xml 代码如下:
[html] 视图平原 复制 打印吗?
< ?xml 版本=“1.0” 编码=“utf - 8”? > 
<豆子 xmlns=“http://www.springframework.org/schema/beans”   
xmlns:上下文=“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”> 
      
    < !——引入其他配置文件,可以为多个- - > 
    <进口 资源=“classpath *:配置/ spring / spring-user.xml”/ > 
     
< /豆子> 

spring-hibernate。 xml代码如下:
[html] 视图平原 复制 打印吗?
< ?xml 版本=“1.0” 编码=“utf - 8”? > 
<豆子 xmlns=“http://www.springframework.org/schema/beans” 
    xmlns:上下文=“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”> 
     
    < !——配置数据源- - > 
    <豆 id=“数据源” 
        类=“org.springframework.jdbc.datasource.DriverManagerDataSource”> 
        <财产 的名字=“driverClassName” 价值=" $ { jdbc.driverClassName } " / > 
        <财产 的名字=“url” 价值=" $ { jdbc.url } " / > 
        <财产 的名字=“用户名” 价值=" $ { jdbc.username } " / > 
        <财产 的名字=“密码” 价值=" $ { jdbc.password } " / > 
    < /豆> 
    < !——导入Src目录下的jdbc.properties文件- - > 
    <背景:property-placeholder 位置=“类路径:jdbc.properties” / > 
    < !——配置hibernate SessionFactory - - > 
    <豆 id=“sessionFactory” 
        类=“org.springframework.orm.hibernate4.LocalSessionFactoryBean”> 
        <财产 的名字=“数据源” 裁判=“数据源” / > 
        <财产 的名字=“hibernateProperties”> 
            <道具> 
                <道具 关键=“hibernate.dialect”>org.hibernate.dialect.MySQLDialect< /道具> 
                <道具 关键=“auto”>更新< /道具> 
                <道具 关键=“hibernate.show_sql”>真正的< /道具> 
                <道具 关键=“hiberante.format_sql”>真正的< /道具> 
            < /道具> 
        < /财产> 
        <财产 的名字=“configLocations”> 
            <列表> 
                <价值> 
classpath *:配置/ hibernate / hibernate.cfg.xml
                < /价值> 
            < /列表> 
        < /财产> 
    < /豆> 
 
    < !——事务管理器- - > 
    <豆 id=“transactionManager” 
        类=“org.springframework.orm.hibernate4.HibernateTransactionManager”> 
        <财产 的名字=“sessionFactory” 裁判=“sessionFactory”>< /财产> 
    < /豆> 
     
    < !——事务代理类- - > 
    <豆 id=“transactionBese” 
        类=“org.springframework.transaction.interceptor.TransactionProxyFactoryBean” 
        lazy-init=“真正的” 文摘=“真正的”> 
        <财产 的名字=“transactionManager” 裁判=“transactionManager”>< /财产> 
        <财产 的名字=“transactionAttributes”> 
            <道具> 
                <道具 关键=“添加*”>PROPAGATION_REQUIRED,异常< /道具> 
                <道具 关键=“更新*”>PROPAGATION_REQUIRED,异常< /道具> 
                <道具 关键=“插入*”>PROPAGATION_REQUIRED,异常< /道具> 
                <道具 关键=“修改*”>PROPAGATION_REQUIRED,异常< /道具> 
                <道具 关键=“删除*”>PROPAGATION_REQUIRED,异常< /道具> 
                <道具 关键=“德尔*”>PROPAGATION_REQUIRED,异常< /道具> 
                <道具 关键=“*”>PROPAGATION_NEVER< /道具> 
            < /道具> 
        < /财产> 
    < /豆> 
 
< /豆子>   

spring-serlvet.xml 代码如下:
[html] 视图平原 复制 打印吗?
< ?xml 版本=“1.0” 编码=“utf - 8”? > 
<豆子 xmlns=“http://www.springframework.org/schema/beans” 
    xmlns:上下文=“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”> 
 
    < !——注解扫描的包- - > 
    <背景:component-scan 基本包=“com.edw.controller” / > 
 
    < !——开启注解方案1 - - > 
    < !——注解方法处理- - > 
< !<豆 类=“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”  
        / >- - -> 
    < !——注解类映射处理- - > 
    < !——< bean类= " org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping " > < /豆> - - > 
 
    < !——开启注解方案2 - - > 
    <mvc:注解驱动的 / > 
 
    < !——静态资源访问,方案1 - - > 
    <mvc:资源 位置=“/ img /” 映射=“/ img / * *” / > 
    <mvc:资源 位置=“/ js /” 映射=“/ js / * *” / > 
 
    < !——静态资源访问,方案2 - - > 
    < !——< mvc:default-servlet-handler / > > 
 
    < !——视图解释类- - > 
    <豆 id=“viewResolver” 
        类=“org.springframework.web.servlet.view.InternalResourceViewResolver”> 
        <财产 的名字=“前缀” 价值=“/ web - inf / jsp /”>< /财产> 
        < !——可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑- - > 
        <财产 的名字=“后缀” 价值=. jsp”>< /财产> 
    < /豆> 
 
    < !——上传文件bean - - > 
< !<豆 id=“multipartResolver” 类=“org.springframework.web.multipart.commons.CommonsMultipartResolver”>  
        <财产 的名字=“defaultEncoding” 价值=“utf - 8” / > <财产 的名字=“maxUploadSize”  
        价值=“10485760000” / > <财产 的名字=“maxInMemorySize” 价值=“40960” / >  
        < /豆>- - -> 
 
< /豆子>   

jdbc.properties(测试是数据库名,根据自己的数据库来改)
(普通) 视图平原 复制 打印吗?
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:/ / localhost:3306 /测试
jdbc.username =根
jdbc.password =根

猜你喜欢

转载自2560240061.iteye.com/blog/2309780