spring+springmvc+hibernate 框架搭建

1、新建web项目,将所需jar包放到 lib 目录下

2、配置web.xml 配置文件

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  3   <display-name>SchoolResourceRepository</display-name>
  4       <filter>
  5         <filter-name>Set Character Encoding</filter-name>
  6         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  7         <init-param>
  8             <param-name>encoding</param-name>
  9             <param-value>utf-8</param-value>
 10         </init-param>
 11     </filter>
 12     <filter-mapping>
 13         <filter-name>Set Character Encoding</filter-name>
 14         <url-pattern>/*</url-pattern>
 15     </filter-mapping>
 16     <context-param>
 17         <param-name>CharEncode</param-name>
 18         <param-value>utf-8</param-value>
 19     </context-param>
 20     <!-- <filter>
 21         <filter-name>charEncode</filter-name>
 22         <filter-class>net.wwwyibu.filter.CharEncodeFilter</filter-class>
 23         <async-supported>true</async-supported>
 24     </filter>
 25     <filter-mapping>
 26         <filter-name>charEncode</filter-name>
 27         <url-pattern>*.do</url-pattern>
 28     </filter-mapping>
 29     <filter-mapping>
 30         <filter-name>charEncode</filter-name>
 31         <url-pattern>*.jsp</url-pattern>
 32     </filter-mapping>
 33     <filter-mapping>
 34         <filter-name>charEncode</filter-name>
 35         <url-pattern>*.action</url-pattern>
 36     </filter-mapping>
 37     <filter-mapping>
 38         <filter-name>charEncode</filter-name>
 39         <url-pattern>/*</url-pattern>
 40     </filter-mapping>-->
 41
 42     <!-- <filter>
 43         <filter-name>loginFilter</filter-name>
 44         <filter-class>net.wwwyibu.filter.StuZHMassgeLoginFilter</filter-class>
 45     </filter>
 46     <filter-mapping>
 47         <filter-name>loginFilter</filter-name>
 48         <url-pattern>/stuZHMessage/*</url-pattern>
 49     </filter-mapping> -->
 50
 51
 52     <!-- <filter>
 53         <filter-name>loginFilter</filter-name>
 54         <filter-class>net.wwwyibu.filter.LoginOutFilter</filter-class>
 55     </filter>
 56     <filter-mapping>
 57         <filter-name>loginFilter</filter-name>
 58         <url-pattern>/*</url-pattern>
 59     </filter-mapping> -->
 60
 61     <context-param>
 62         <param-name>contextConfigLocation</param-name>
 63         <param-value>
 64             WEB-INF\classes\applicationContext.xml</param-value>
 65     </context-param>
 66     <listener>
 67         <listener-class>
 68             org.springframework.web.context.ContextLoaderListener
 69         </listener-class>
 70     </listener>
 71     <!-- 监听初始化学校代码 -->
 72     <!-- <listener>
 73       <listener-class>net.wwwyibu.filter.ServletListener</listener-class>
 74     </listener> -->
 75
 76
 77     <servlet-mapping>
 78         <servlet-name>default</servlet-name>
 79         <url-pattern>/static/*</url-pattern>
 80     </servlet-mapping>
 81     <servlet>
 82         <servlet-name>springmvc</servlet-name>
 83         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 84         <init-param>
 85             <description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description>
 86             <param-name>contextConfigLocation</param-name>
 87             <param-value>WEB-INF\classes\applicationContext.xml</param-value>
 88         </init-param>
 89         <load-on-startup>1</load-on-startup>
 90     </servlet>
 91     <servlet-mapping>
 92         <servlet-name>springmvc</servlet-name>
 93         <url-pattern>/</url-pattern>
 94     </servlet-mapping>
 95     <filter>
 96         <filter-name>HiddenHttpMethodFilter</filter-name>
 97         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 98     </filter>
 99     <filter-mapping>
100         <filter-name>HiddenHttpMethodFilter</filter-name>
101         <servlet-name>springmvc</servlet-name>
102     </filter-mapping>
103   <welcome-file-list>
104     <welcome-file>index.html</welcome-file>
105     <welcome-file>index.htm</welcome-file>
106     <welcome-file>index.jsp</welcome-file>
107     <welcome-file>default.html</welcome-file>
108     <welcome-file>default.htm</welcome-file>
109     <welcome-file>default.jsp</welcome-file>
110   </welcome-file-list>
111 </web-app>

3、SpringContext.xml配置文件 和 Spring-mvc.xml配置文件,这两个可以写到一个xml文件中,applicationContext.xml中。

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans"
  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  5     xmlns:mvc="http://www.springframework.org/schema/mvc"
  6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7 http://www.springframework.org/schema/util spring-util-3.2.xsd
  8 http://www.springframework.org/schema/tx spring-tx-3.2.xsd
  9 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
 10 http://www.springframework.org/schema/aop spring-aop-3.2.xsd
 11 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 12     <!-- 让Spring通过自动扫描来查询和管理Bean -->
 13     <context:component-scan base-package="net.wwwjpz" />
 14
 15     <context:annotation-config />
 16
 17     <mvc:annotation-driven />
 18     <!-- 加載 properties 配置文件 -->
 19     <bean id="propertyConfig"
 20         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 21         <property name="location">
 22             <value>classpath:jdbc.properties</value>
 23             <!-- <value>file:D:/YiBuProperties/SchoolResourceRepository/jdbc.properties</value> -->
 24         </property>
 25         <property name="ignoreUnresolvablePlaceholders" value="true" />
 26     </bean>
 27
 28 <!--     <bean id="ExcelDatabase" class="net.wwwyibu.service.ExcelDatabase"></bean>
 29     <bean id="MedicalService" class="net.wwwyibu.service.MedicalService"></bean>
 30     <bean id="StudentService" class="net.wwwyibu.service.StudentService"></bean>
 31     <bean id="UploadService" class="net.wwwyibu.service.UploadService"></bean>
 32     <bean id="SaveHealthService" class="net.wwwyibu.service.SaveHealthService"></bean>
 33     <bean id="EvaluationService" class="net.wwwyibu.service.EvaluationService"></bean>
 34     <bean id="ZhsjService" class="net.wwwyibu.service.ZhsjService"></bean>
 35     <bean id="ExamService" class="net.wwwyibu.service.ExamService"></bean> -->
 36     <!-- <bean id="ExcelCheckService" class="net.wwwyibu.service.ExcelCheckService"></bean> -->
 37
 38
 39
 40
 41     <!--
 42     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 43         <property name="defaultEncoding" value="UTF-8"/>
 44         <property name="maxUploadSize" value="20971520"/>
 45         <property name="resolveLazily" value="true"/>
 46     </bean> -->
 47
 48     <bean id="dateSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
 49         <property name="driver">
 50             <value>${jdbc.driver}</value>
 51         </property>
 52         <property name="driverUrl">
 53             <value>${jdbc.url}</value>
 54         </property>
 55         <property name="user">
 56             <value>${jdbc.username}</value>
 57         </property>
 58         <property name="alias">
 59             <value>${jdbc.username}</value>
 60         </property>
 61         <property name="password">
 62             <value>${jdbc.password}</value>
 63         </property>
 64
 65         <property name="maximumActiveTime">
 66             <value>1200000</value>
 67         </property>
 68         <property name="prototypeCount">
 69             <value>100</value>
 70         </property>
 71         <property name="maximumConnectionCount">
 72             <value>100</value>
 73         </property>
 74         <property name="minimumConnectionCount">
 75             <value>10</value>
 76         </property>
 77         <property name="simultaneousBuildThrottle">
 78             <value>500</value>
 79         </property>
 80         <property name="trace">
 81             <value>true</value>
 82         </property>
 83         <property name="verbose">
 84             <value>true</value>
 85         </property>
 86         <property name="testBeforeUse">
 87             <value>true</value>
 88         </property>
 89         <property name="houseKeepingTestSql">
 90             <value>select 1 from dual</value>
 91         </property>
 92
 93     </bean>
 94
 95
 96     <bean id="transactionManager"
 97         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 98         <property name="dataSource">
 99             <ref bean="dateSource" />
100         </property>
101     </bean>
102
103     <bean id="transactionManagerHib"
104         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
105         <property name="sessionFactory">
106             <ref local="sessionFactory" />
107         </property>
108     </bean>
109
110
111     <bean id="transactionInterceptor"
112         class="org.springframework.transaction.interceptor.TransactionInterceptor">
113         <property name="transactionManager" ref="transactionManagerHib" />
114
115         <property name="transactionAttributes">
116             <props>
117                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
118                 <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
119                 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
120                 <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
121                 <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop>
122                 <prop key="insert*">PROPAGATION_REQUIRED</prop>
123                 <prop key="update*">PROPAGATION_REQUIRED</prop>
124                 <prop key="save*">PROPAGATION_REQUIRED</prop>
125                 <prop key="delete*">PROPAGATION_REQUIRED,-BussException</prop>
126                 <prop key="*">PROPAGATION_REQUIRED</prop>
127             </props>
128         </property>
129     </bean>
130
131
132
133
134     <bean id="autoProxyCreator"
135         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
136         <property name="interceptorNames">
137             <list>
138                 <idref bean="transactionInterceptor" />
139             </list>
140         </property>
141         <property name="beanNames">
142             <list>
143                 <value>/yeepayCallBack,Add_Re,*DAO</value>
144             </list>
145         </property>
146     </bean>
147
148     <bean id="hibernateDaoTemplate" abstract="true">
149         <property name="sessionFactory">
150             <ref bean="sessionFactory" />
151         </property>
152     </bean>
153
154
155     <bean id="sessionFactory"
156         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
157         <property name="dataSource">
158             <ref bean="dateSource" />
159         </property>
160         <property name="hibernateProperties">
161             <props>
162                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
163                 <!-- <prop key="hibernate.dialect">net.wwwyibu.util.MySQL5DialectRegexp</prop> -->
164                 <prop key="hibernate.show_sql">true</prop>
165                 <prop key="hibernate.hbm2ddl.auto">none</prop>
166
167                 <!--<prop key="hibernate.current_session_context_class">thread</prop> -->
168                 <prop key="hibernate.jdbc.batch_size">50</prop>
169                 <prop key="hibernate.jdbc.fetch_size">50</prop>
170                 <!--代表使用Hibernate的二级缓存 -->
171                 <prop key="hibernate.cache.use_second_level_cache">true</prop>
172                 <!--代表是否使用查询缓存,这里不使用,因为一般而言查询缓存的命中率并不是很高,所以我们没有 必要为每一个用户的查询缓存它的数据,所以这里设为false -->
173                 <prop key="hibernate.cache.use_query_cache">false</prop>
174                 <!--用于指定使用缓存产品的驱动类 -->
175                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
176             </props>
177         </property>
178         <property name="mappingResources">
179             <list>
180                 <!-- <value>net/wwwyibu/orm/Student.hbm.xml</value>
181                 <value>net/wwwyibu/orm/Studentclass.hbm.xml</value> -->
182             </list>
183         </property>
184     </bean>
185     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
186         <property name="sessionFactory">
187             <ref bean="sessionFactory" />
188         </property>
189     </bean>
190         <bean id="multipartResolver"
191             class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
192             <!-- 上传文件大小上限,单位为字节(10MB) -->
193             <property name="maxUploadSize">
194                 <value>10485760</value>
195             </property>
196             <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
197             <property name="defaultEncoding">
198                 <value>UTF-8</value>
199             </property>
200         </bean>
201
202     <!-- mvc -->
203
204     <bean
205         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
206     <bean
207         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
208
209     <!-- Default ViewResolver -->
210     <bean id="viewResolver"
211         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
212         <property name="viewClass"
213             value="org.springframework.web.servlet.view.JstlView" />
214         <property name="prefix" value="/" />
215         <property name="suffix" value=".jsp"></property>
216     </bean>
217     <mvc:default-servlet-handler />
218     <!-- mvc -->
219
220
221 </beans>

原文地址:https://www.cnblogs.com/erlongxizhu-03/p/10399411.html

时间: 2024-11-09 02:00:24

spring+springmvc+hibernate 框架搭建的相关文章

Spring+Springmvc+Hibernate环境搭建与配置

这里给出JavaWeb项目Spring+Springmvc+Hibernate框架环境的搭建与配置,方便以后快速创建项目.下面是以本人的一个项目为例给出的配置,不同项目具体的结构都是可以按一下结构完成的,至于配置内容则需要按照具体的项目需求更改部分内容.需要注意的是,本项目是以使用注解为前提完成的项目. 一.项目目录 首先说明一下,本项目是maven项目,采用Spring+Springmvc+Hibernate的框架,前端模板引擎采用thymeleaf,html代码存放在下面结构的templat

Spring+Spring MVC+Hibernate框架搭建实例

前言:这里只是说明整个搭建流程,并不进行原理性的讲解 一 下面所需要用到的数据库配置: 数据库方面,使用mysql创建一个users表,具体代码如下: DROP TABLE IF EXISTS `users`; CREATE TABLE `users` (   `UserID` int(4) NOT NULL AUTO_INCREMENT,   `UserName` varchar(16) NOT NULL,   `Password` varchar(16) NOT NULL,   `Telep

Maven+Spring+SpringMVC+MyBatis框架搭建

看了一段时间Android,学了学C++,搭个SSM的框架复习复习老本行. 原来的SSH——Struts,spring,hibernate,逐渐被现在的SSM取代,当然了,还有各有优缺点的. 搭的这个框架中的SpringMVC并不是返回页面,而是返回json数据,在前端的js中处理页面的展现,我是为了让Android客户端能够访问SpringMVC的controller,并给Android客户端返回json数据考虑的. 一.还是一样的,要先看maven中都引入什么包: Java代码 复制代码 收

spring+springMvc+hibernate框架简单实例

配置文件: 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" xsi:schemaLocation="http://java.sun.com/xml

简单Spring+Struts2+Hibernate框架搭建

使用Maven+Spring+Struts2+Hibernate整合 pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd

maven+Spring+SpringMVC+Hibernate快速搭建

目录结构: spring-servlet.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:mvc="http://www.sp

Spring+SpringMVC+Mybatis框架搭建

一.项目结构及所需jar包 1.1.项目结构 1.2依赖jar包(含json-lib及 log4j) 二.配置文件 2.1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/ja

spring ,springMVC,shiro 框架搭建。

1.  导入相关jar包. 2.完成spring的相关配置 1) 在web.xml中完成spring的声明 contextLoaderListener的配置,如果是location,那么配置文件必须放到web项目的根目录中. 如果是放到类路径下,需要改为classpath:applicationContext.xml 2)新建spring的配置文件 这个名称必须与web.xml中,配置的一致.如applicationContext.xml 3.完成springMVC的配置 1)在web.xml中

搭建基于全注解的Spring+Spring MVC+Hibernate框架

以实例讲解Spring+Spring MVC+Hibernate框架搭建步骤: 一.配置web.xml Xml代码   <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XML