基于maven的springmvc +hibernate+ spring框架搭建

转自:http://my.oschina.net/fengshuzi/blog/265110?fromerr=wOMJ3m1s


一、web.xml


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<? 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 >
 
     <!-- Character Filter -->
     < 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 >
     </ filter >
     < filter-mapping >
         < filter-name >encodingFilter</ filter-name >
         < url-pattern >/*</ url-pattern >
     </ filter-mapping >
     
 
     <!-- Spring Listener -->
     < context-param >
         < param-name >contextConfigLocation</ param-name >
         < param-value >classpath*:META-INF/spring/ApplicationContext.xml</ param-value >
     </ context-param >
     
     <!-- SpringMVC -->
     < servlet >
         < servlet-name >springmvc</ servlet-name >
         < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
         < load-on-startup >1</ load-on-startup >
     </ servlet >
     < servlet-mapping >
         < servlet-name >springmvc</ servlet-name >
         < url-pattern >/*</ url-pattern >
     </ servlet-mapping >
     
     < error-page >
         < exception-type >java.lang.Throwable</ exception-type >
         < location >/WEB-INF/pages/error/unchecked/500.html</ location >
     </ error-page >
     < error-page >
         < error-code >500</ error-code >
         < location >/WEB-INF/pages/error/unchecked/500.html</ location >
     </ error-page >
     < error-page >
         < error-code >404</ error-code >
         < location >/WEB-INF/pages/error/unchecked/404.html</ location >
     </ error-page >
     
     < welcome-file-list >
         < welcome-file >/index.jsp</ welcome-file >
     </ welcome-file-list >
 
     < distributable />
</ web-app >



二、springmvc-servlet.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
     xmlns:context = "http://www.springframework.org/schema/context" xmlns:tx = "http://www.springframework.org/schema/tx"
     xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:util = "http://www.springframework.org/schema/util"
     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-3.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
     < import resource = "classpath:META-INF/spring/root-context.xml" />
     < import resource = "classpath:META-INF/spring/mvc-context.xml" />
     
</ beans >



三、root-context.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<? 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:jdbc = "http://www.springframework.org/schema/jdbc" xmlns:jee = "http://www.springframework.org/schema/jee"
     xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:jpa = "http://www.springframework.org/schema/data/jpa"
     xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
     default-lazy-init = "true" >
     < context:annotation-config />
     < context:component-scan base-package = "com.spring.fengshu.springmvc" >
     <!--     <context:include-filter type="regex"
             expression=".*DaoImpl" />
         <context:include-filter type="regex"
             expression=".*ServiceImpl" /> -->
     </ context:component-scan >
     <!-- Root Context: defines shared resources visible to all other web components -->
     < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" >
         < property name = "driverClassName" value = "com.mysql.jdbc.Driver" ></ property >
         < property name = "url" value = "jdbc:mysql://localhost:3306/springmvc" ></ property >
         < property name = "username" value = "root" ></ property >
         < property name = "password" value = "root" ></ property >
         < property name = "maxActive" value = "500" ></ property >
         < property name = "maxIdle" value = "50" ></ property >
         < property name = "maxWait" value = "500" ></ property >
         < property name = "defaultAutoCommit" value = "true" ></ property >
     </ bean >
 
     < bean id = "sessionFactory"
         class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
         < property name = "dataSource" >
             < ref bean = "dataSource" />
         </ property >
         < property name = "annotatedClasses" >
             < list >
               < value >com.spring.fengshu.springmvc.domain.ProjectBase</ value >
             </ list >
         </ property >
         < property name = "hibernateProperties" >
             < props >
                 < prop key = "hibernate.dialect" >
                     org.hibernate.dialect.MySQLDialect
                 </ prop >
                 < prop key = "hibernate.show_sql" >true</ prop >
                 < prop key = "hibernate.hbm2ddl.auto" >update </ prop >
             </ props >
         </ property >
     </ bean >
 
      <!-- 支持  @Transactional 标记 --> 
     < tx:annotation-driven transaction-manager = "transactionManager" /> 
     <!-- 定义事务管理器 -->
     < bean id = "transactionManager"
         class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
         < property name = "sessionFactory" ref = "sessionFactory" />
     </ bean >
 
</ beans >



四、mvc-context.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<? 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:p = "http://www.springframework.org/schema/p"
     xmlns:context = "http://www.springframework.org/schema/context"
     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-3.0.xsd">
 
     <!-- DispatcherPortlet Context: defines this olaf's request-processing infrastructure -->
     
     <!-- Autodetect annotated controllers -->
     < context:component-scan base-package = "com.spring.fengshu.springmvc.controller" />
 
     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
     < bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
         < property name = "prefix" value = "/WEB-INF/pages/" />
         < property name = "suffix" value = ".jsp" />
     </ bean >
     
</ beans >



五、pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<? xml version = "1.0" ?>
 
< 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/maven-v4_0_0.xsd" >
   < modelVersion >4.0.0</ modelVersion >
   < groupId >com.spring.fengshu</ groupId >
   < artifactId >springmvc</ artifactId >
   < version >0.0.1-SNAPSHOT</ version >
   < packaging >war</ packaging >
 
   < name >springmvc</ name >
   < url >http://maven.apache.org</ url >
< prerequisites >
         < maven >3.0.4</ maven >
     </ prerequisites >
     < properties >
         < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
         < java-version >1.6</ java-version >
         < servlet-api.version >2.5</ servlet-api.version >
         < jsp-api.version >2.2</ jsp-api.version >
         < jstl.version >1.2</ jstl.version >
         < org.springframework-version >3.2.5.RELEASE</ org.springframework-version >
         < org.aspectj-version >1.7.4</ org.aspectj-version >
         < org.slf4j-version >1.7.5</ org.slf4j-version >
     </ properties >
 
     < dependencies >
         
     <!-- Spring -->
     < dependency >
         < groupId >org.springframework</ groupId >
         < artifactId >spring-core</ artifactId >
         < version >${org.springframework-version}</ version >
     </ dependency >
     <!-- 用于支持事务 -->
     < dependency >
         < groupId >org.springframework</ groupId >
         < artifactId >spring-orm</ artifactId >
         < version >${org.springframework-version}</ version >
     </ dependency >
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-context</ artifactId >
             < version >${org.springframework-version}</ version >
             < exclusions >
                 <!-- Exclude Commons Logging in favor of SLF4j -->
                 < exclusion >
                     < groupId >commons-logging</ groupId >
                     < artifactId >commons-logging</ artifactId >
                 </ exclusion >
             </ exclusions >
         </ dependency >
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-webmvc-portlet</ artifactId >
             < version >${org.springframework-version}</ version >
         </ dependency >
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-web</ artifactId >
             < version >${org.springframework-version}</ version >
         </ dependency >
         <!-- hibernate begin -->
         < dependency >
             < groupId >org.hibernate</ groupId >
             < artifactId >hibernate-core</ artifactId >
             < version >3.6.3.Final</ version >
         </ dependency >
         < dependency >
             < groupId >org.springframework.data</ groupId >
             < artifactId >spring-data-jpa</ artifactId >
             < version >1.1.2.RELEASE</ version >
         </ dependency >
 
         < dependency >
             < groupId >org.hibernate</ groupId >
             < artifactId >hibernate-entitymanager</ artifactId >
             < version >3.6.3.Final</ version >
         </ dependency >
 
         < dependency >
             < groupId >c3p0</ groupId >
             < artifactId >c3p0</ artifactId >
             < version >0.9.1.2</ version >
         </ dependency >
 
         < dependency >
             < groupId >org.hibernate</ groupId >
             < artifactId >hibernate-validator</ artifactId >
             < version >3.1.0.GA</ version >
         </ dependency >
 
         < dependency >
             < groupId >commons-dbcp</ groupId >
             < artifactId >commons-dbcp</ artifactId >
             < version >1.4</ version >
         </ dependency >
 
         <!-- hibernate end -->
 
         <!-- AspectJ -->
         < dependency >
             < groupId >org.aspectj</ groupId >
             < artifactId >aspectjrt</ artifactId >
             < version >${org.aspectj-version}</ version >
         </ dependency >
 
         <!-- Logging -->
         < dependency >
             < groupId >org.slf4j</ groupId >
             < artifactId >slf4j-api</ artifactId >
             < version >${org.slf4j-version}</ version >
         </ dependency >
         < dependency >
             < groupId >org.slf4j</ groupId >
             < artifactId >jcl-over-slf4j</ artifactId >
             < version >${org.slf4j-version}</ version >
             < scope >runtime</ scope >
         </ dependency >
         < dependency >
             < groupId >org.slf4j</ groupId >
             < artifactId >slf4j-log4j12</ artifactId >
             < version >${org.slf4j-version}</ version >
         </ dependency >
         < dependency >
             < groupId >log4j</ groupId >
             < artifactId >log4j</ artifactId >
             < version >1.2.17</ version >
             < exclusions >
                 < exclusion >
                     < groupId >javax.mail</ groupId >
                     < artifactId >mail</ artifactId >
                 </ exclusion >
                 < exclusion >
                     < groupId >javax.jms</ groupId >
                     < artifactId >jms</ artifactId >
                 </ exclusion >
                 < exclusion >
                     < groupId >com.sun.jdmk</ groupId >
                     < artifactId >jmxtools</ artifactId >
                 </ exclusion >
                 < exclusion >
                     < groupId >com.sun.jmx</ groupId >
                     < artifactId >jmxri</ artifactId >
                 </ exclusion >
             </ exclusions >
         </ dependency >
 
         <!-- @Inject -->
         < dependency >
             < groupId >javax.inject</ groupId >
             < artifactId >javax.inject</ artifactId >
             < version >1</ version >
         </ dependency >
     <!-- mysql -->
         < dependency >
                 < groupId >mysql</ groupId >
                 < artifactId >mysql-connector-java</ artifactId >
                 < version >5.1.22</ version >
             </ dependency >
         < dependency >
             < groupId >org.apache.commons</ groupId >
             < artifactId >commons-lang3</ artifactId >
             < version >3.3</ version >
         </ dependency >
         < dependency >
             < groupId >org.apache.commons</ groupId >
             < artifactId >commons-dbcp2</ artifactId >
             < version >2.0</ version >
         </ dependency >
         < dependency >
             < groupId >commons-beanutils</ groupId >
             < artifactId >commons-beanutils</ artifactId >
             < version >1.9.1</ version >
         </ dependency >
         <!-- Servlet -->
         < dependency >
             < groupId >javax.servlet</ groupId >
             < artifactId >servlet-api</ artifactId >
             < version >${servlet-api.version}</ version >
         </ dependency >
         < dependency >
             < groupId >javax.servlet.jsp</ groupId >
             < artifactId >jsp-api</ artifactId >
             < version >${jsp-api.version}</ version >
         </ dependency >
         < dependency >
             < groupId >javax.servlet</ groupId >
             < artifactId >jstl</ artifactId >
             < version >${jstl.version}</ version >
         </ dependency >
 
         <!-- Test -->
         < dependency >
             < groupId >junit</ groupId >
             < artifactId >junit</ artifactId >
             < version >4.11</ version >
             < scope >test</ scope >
         </ dependency >
     </ dependencies >
 
     < build >
         < plugins >
             <!-- 配置加入jetty服务器,开发时我们一般使用jetty服务器 -->
             < plugin >
                 < groupId >org.mortbay.jetty</ groupId >
                 < artifactId >jetty-maven-plugin</ artifactId >
                 < configuration >
                     <!-- 设置扫描target/classes内部文件变化时间间隔 -->
                     < scanIntervalSeconds >2</ scanIntervalSeconds >
                     < webApp >
                         < contextPath >/springmvc</ contextPath >
                     </ webApp >
                 </ configuration >
             </ plugin >
             <!-- compiler插件, 设定JDK版本 -->
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-compiler-plugin</ artifactId >
                 < version >3.0</ version >
                 < configuration >
                     < source >1.6</ source >
                     < target >1.6</ target >
                     < showWarnings >true</ showWarnings >
                 </ configuration >
             </ plugin >
 
             <!-- war打包插件, 设定war包名称不带版本号 -->
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-war-plugin</ artifactId >
                 < version >2.3</ version >
                 < configuration >
                     < warName >springmvc</ warName >
                 </ configuration >
             </ plugin >
             <!-- eclipse插件, 设定wtp版本 -->
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-eclipse-plugin</ artifactId >
                 < version >2.9</ version >
                 < configuration >
                     < downloadSources >true</ downloadSources >
                     < downloadJavadocs >false</ downloadJavadocs >
                     < wtpversion >2.0</ wtpversion >
             
                     
                 </ configuration >
             </ plugin >
             < plugin >
                 < groupId >org.apache.maven.plugins</ groupId >
                 < artifactId >maven-dependency-plugin</ artifactId >
                 < executions >
                     < execution >
                         < id >install</ id >
                         < phase >install</ phase >
                         < goals >
                             < goal >sources</ goal >
                         </ goals >
                     </ execution >
                 </ executions >
             </ plugin >
         </ plugins >
     </ build >
</ project >



六、controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.spring.fengshu.springmvc.controller;
 
import javax.inject.Inject;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.spring.fengshu.springmvc.dao.IProjectDao;
import com.spring.fengshu.springmvc.domain.ProjectBase;
 
@Controller
@RequestMapping (value = "/" )
public class FirstController {
     @Inject
     private IProjectDao projectDao;
 
     @ResponseBody
     @RequestMapping (value = "hello" , method = RequestMethod.GET)
     public String getComment() {
         ProjectBase projectBase = new ProjectBase();
         projectBase.setProjectName( "fengshu" );
         projectDao.save(projectBase);
         return "hello world!" ;
     }
 
     public IProjectDao getProjectDao() {
         return projectDao;
     }
 
     public void setProjectDao(IProjectDao projectDao) {
         this .projectDao = projectDao;
     }
 
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值