TA的每日心情![](source/plugin/dsu_paulsign/img/emot/shuai.gif) | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
一、前言:. t! `) q+ h: G8 E7 R$ L( `$ n6 D
为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平。你的一个决定会影响团队未来的几年。要考虑方面太多: J: ?. {. k5 I; Y: u
1、简单易用,以提高开发效率。使小部分的精力在框架上,大部分的精力放在业务上。
9 H/ Y/ `& G$ B+ j+ g' W( X2、性能优秀,这是一个最能吸引眼球的话题。$ w, h2 A/ W) R, e# E
3、尽量使用大众的框架(避免使用小众的、私有的框架),新招聘来的开发人员有一些这方面技术积累,减低人员流动再适应的影响。0 @+ Q; ?: V0 g
7 t7 q$ I# L, J, `; y% J A
如果你还在为这件事件发愁,本文最适合你了。选择spring MVC吧。8 c" b+ R* |0 G0 }4 p' l- d
5 @/ u9 i- k; ~& _$ Y0 L
Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对struts 2的超越。现在越来越多的开发团队选择了Spring MVC。
& x/ D& B8 w6 G: B& o1 H% x
- o/ G/ c- d3 [2 r5 IStruts2也是非常优秀的MVC构架,优点非常多比如良好的结构,拦截器的思想,丰富的功能。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降,应避免使用这些功能。而Struts2的多层拦截器、多实例action性能都很好。
7 y2 {9 a' H4 X# G/ C# W, D3 a. H/ P
Spring3 MVC的优点:$ z1 v& T2 h: i7 m
1、Spring3 MVC使用简单,学习成本低。学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。, G( Z1 j, [% `2 c: U# A
2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)
4 J# N& @! {2 ~: z# j n3、Spring3 MVC的灵活是你无法想像的,Spring框架的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。5 z# C: p' @3 N7 m/ y1 ~% D) _
- z& @$ K2 o% L
Struts2的众多优点:
9 U5 u4 l5 r; H& H1、老牌的知名框架,从Struts1起积累了大量用户群体。技术文档丰富。- \! U' J3 r* ], D9 w1 z
2、其它方面略... ! M+ \3 _! e+ B' y0 N
==============================================================
% w1 l, N; }6 K& t7 p什么是mybatis2 A( ?; U) k6 \, J! R& D1 z
MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录.) R6 Z# s! A, D
% t; B _0 P3 L5 V; A" z) Qorm工具的基本思想$ _$ R$ Z0 ]' o9 |: q8 m( Q
无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:4 Q" q/ a, ~& H
1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.
! \+ [/ _* r. d4 x4 Y$ l9 V- ]& ]2. 由sessionfactory 产生 session
0 o+ ^/ _, a! r7 B4 @3. 在session 中完成对数据的增删改查和事务提交等.( e3 ]% I- F: y/ w, v! g) @
4. 在用完之后关闭session 。/ ^3 A; ]4 [1 m- t5 S
5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。' a1 g( X- u; f; `% T
% n% G5 m; s$ {2 ]* V( L项目说明:实现了用户登陆demo。
: r! J' y$ t& f项目配置说明:& C' ~ l7 c0 B# h# I% I1 g: V
web.xml:1 t4 z2 F! X* U1 A9 \
- <?xml version="1.0" encoding="UTF-8"?>
. H, V# I }% b" s; o - <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" * j5 Z! b" ^+ `- z
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 C5 y6 f9 N- K5 o6 l! `4 ?) ~" w3 l2 X
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee - c1 x# B5 e+ a, K" W
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
" |" R/ P y0 W# ^ -
( m' o: T k$ ~ - <!-- 监听spring上下文容器 -->
9 u: ` ?* q8 q3 Z - <listener>
8 Y, C% [/ H5 g/ f, { - <listener-class>
8 S9 J" @% \( [5 t( H - org.springframework.web.context.ContextLoaderListener * ~' ?+ e6 f; P7 b# M9 I* s& p
- </listener-class> # |- x, a" O$ }
- </listener>
) D6 u6 F% P a0 v- T -
9 y A1 t3 f p' B+ S4 u: Q - <!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
7 F0 S7 P& l$ M T3 J8 u - <context-param> 9 ~9 ^7 m" O) d' B$ O5 L
- <param-name>contextConfigLocation</param-name>
5 I# p6 ]4 s# c5 s5 g - <param-value>classpath:*-context.xml</param-value> ; ?& @2 m/ \. D/ n% ~& P# g
- </context-param>
; k. e: M( r+ _ -
5 k) o# q5 A1 N; y4 a - <!-- 配置Spring MVC DispatcherServlet --> & }# x7 E4 t9 q. O$ S- f* |1 F
- <servlet>
' \" d6 L0 j7 e( t, c- p - <servlet-name>MVC</servlet-name>
8 q2 O/ \# u5 x2 f: I M - <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> ; h7 ?4 d! M1 U: U! J+ O
- <!-- 初始化参数 --> % `* [- ? f+ O; V8 v
- <init-param>
) a6 I7 _7 [ o+ w' U - <!-- 加载SpringMVC的xml到 spring的上下文容器中 --> : r' V& [# X2 v3 M" X* O8 q. m
- <param-name>contextConfigLocation</param-name>
" I7 C, [2 `: C: R& ^$ q - <param-value> 6 |5 O$ l) @" { ^
- /WEB-INF/classes/mvc-context.xml 0 O6 B+ G! X2 k7 Q
- </param-value>
8 n/ u H$ A" ~& h1 J+ A6 F - </init-param> & j- z. r5 \6 B5 h% [; Y, I
- <load-on-startup>1</load-on-startup>
$ `( I8 p; T% h: G; c5 J% M; J - </servlet>
r/ g0 M) Z5 C5 ~ -
8 U7 g! D; k$ Z1 D+ D) F; `2 t - <!-- 配置DispatcherServlet所需要拦截的 url --> " |7 }! ?! _' v) j" C/ }) y& ^- K
- <servlet-mapping> 0 e# p) R$ V) I2 y
- <servlet-name>MVC</servlet-name> 5 Y) p1 s6 K3 z9 F: X- e' h
- <url-pattern>*.do</url-pattern>
; i1 \- g% s6 ^; I/ l - </servlet-mapping> ) H! H% w6 I! _
- ; ?4 k; s- a" D
- <welcome-file-list>
. @1 Y4 {; w& e: T" \" O% l7 y- f. ^ - <welcome-file>index.jsp</welcome-file> ( x. L" X6 D! z- d( P+ M) `
- </welcome-file-list> 8 O& M& T. i; M6 O$ W
- + y: }1 a7 E7 r9 Q
- $ a3 U5 F- x6 w: b
- </web-app>
复制代码
- ]* h* W" N+ F gmvc-context.xml:" o2 y# P6 `) ]# e: Y- Z0 o# j
- <?xml version="1.0" encoding="UTF-8"?>
: x+ l! h" k/ P+ X - <beans xmlns="http://www.springframework.org/schema/beans"3 K& N7 |2 L9 y
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
) @9 T$ f+ N9 Q/ N* ^ - xmlns:context="http://www.springframework.org/schema/context"
1 [( k$ D8 q" m( m$ j - xsi:schemaLocation="http://www.springframework.org/schema/beans6 F! Q! I8 ^- }% X
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd: @- w# ^- f1 a& v' Z2 _3 m' w
- http://www.springframework.org/schema/context
! [* \5 }; R2 I+ j3 A - http://www.springframework.org/schema/context/spring-context-3.0.xsd">& G$ E- w0 a# g0 N
- ! z1 ^3 {1 @' A5 X! S$ T+ L# \
- <context:component-scan base-package="com.wg.*" />/ P% B" ?& `4 j* ?( T6 \" G
- h. f( E$ @3 ~0 n- <bean id="viewResolver"3 }0 E! Q j; D- Y
- class="org.springframework.web.servlet.view.UrlBasedViewResolver">
) F/ k: r- X# P2 j; w - <property name="viewClass"
3 r* @. L" B V/ W0 U W) _4 c - value="org.springframework.web.servlet.view.JstlView" /> X$ _$ [; W* r* k7 s2 R
- <property name="prefix" value="/page/" />
0 S* @* r' r- f! o( u - <property name="suffix" value=".jsp" />% h+ L( `0 B; n7 W
- </bean>5 i# A8 l& i( I$ C2 ?. u1 R
- </beans>
3 L0 D& F% X3 m3 k) h4 ~
复制代码 , O, p9 v( G7 Z9 Q4 W! Z5 G
mybatis-config.xml:, v2 q$ P) f; {; Z2 t2 p8 ]
- <?xml version="1.0" encoding="UTF-8"?>
% H7 |# z9 A5 p/ | - <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" : x8 b$ G* p( ~! J$ f( `
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
' H1 _( Q1 E* b* e4 |1 q - <configuration>8 V3 i4 F, O9 _; N: [: }2 B. C
- <typeAliases>
+ o. k8 \/ F2 E1 j% j - <package name="com.wg.bean" />
9 A5 R$ ^0 ~7 `. n% A - </typeAliases>
) `7 R) M2 i7 `3 u: r' f9 a - <mappers>
) g; V" f+ v2 u/ W6 C& K l5 ^ - <mapper resource="com/wg/bean/UserMapper.xml" />
/ ^+ v/ Q; W: k( k7 v# o8 X, I - </mappers>5 w3 w/ }7 z5 ?& P1 T
- </configuration>
复制代码 root-context.xml:
- O) P0 b- Q+ _0 g+ T- <?xml version="1.0" encoding="UTF-8"?>
+ A' y7 q1 i# c, q% x - <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
5 k, W! c3 _: Q' I* q& b4 ?0 u - "http://mybatis.org/dtd/mybatis-3-config.dtd">
. s) P. r! h; a1 v8 @ - <configuration>1 p' t' n" E [
- <typeAliases>1 Y: j, c5 U; {( Z: z% K
- <package name="com.wg.bean" />
' \ S/ L% r4 m; ^* d - </typeAliases>0 ^/ K$ L1 g' b6 ^
- <mappers>
4 S. }' H5 ]$ k8 _ - <mapper resource="com/wg/bean/UserMapper.xml" />" q6 z/ r7 Q) e' E! S1 l
- </mappers>* Z2 z) { w# g
- </configuration>
复制代码
& O: x1 p' v }5 B; [! H( v+ ]user-context.xml:- <?xml version="1.0" encoding="UTF-8"?>
- Z3 ]6 J2 G6 z$ \: |7 H% Z - <beans xmlns="http://www.springframework.org/schema/beans"
' }" N7 q. E% ^, t; [ - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"- Q. R+ D& d2 n, u7 o# e- {$ V$ o
- xmlns:context="http://www.springframework.org/schema/context"" Z# _1 B9 h% c7 i: o4 X- }: E
- xsi:schemaLocation="http://www.springframework.org/schema/beans) i& y9 w& R9 F! h% A
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd. U& \( T) `% q2 X
- http://www.springframework.org/schema/context! B% |% r) Y* h( `
- http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
1 T4 G2 r2 A) a - " f* E1 t6 x9 h8 T
-
& [# [2 R% p& G! k9 |4 V4 o' G4 e - <!-- 用户Dao -->+ i. h% ^- W5 k" ~# G
- <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
, x' z8 k( Y( D+ p5 _! M - <property name="mapperInterface" value="com.wg.dao.UserDao" />
/ k/ Q% w2 _4 U' U - <property name="sqlSessionFactory" ref="sqlSessionFactory" />
) h" k! o( n9 N8 m( y+ r4 Y9 R2 K - </bean>
) e) c5 H# o9 s Q$ A -
& H" `, b; o0 E1 f
$ ~. R0 X; \. W3 `; j( s- </beans>
复制代码 K5 s1 P1 `/ w* z& I% a: D( m6 N# S
% D) n$ h, H* A+ \. B
- r; n7 g" M o
链接:springMvc整合mybatis项目源码) ? N1 ~. I' }& s5 ~
密码:
; P' q' u6 [& m0 _
9 z* R0 p/ U" R: I* r) P6 i+ F9 Y, u& y- i* x2 k4 L# Y( Q
8 j8 D7 t: V6 C% Y1 E7 i
|
|