TA的每日心情![](source/plugin/dsu_paulsign/img/emot/shuai.gif) | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
一、前言:
/ I, n$ ]/ S5 o% A5 X5 B为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平。你的一个决定会影响团队未来的几年。要考虑方面太多:6 r j! I% k; ~0 L" |1 k
1、简单易用,以提高开发效率。使小部分的精力在框架上,大部分的精力放在业务上。% j9 Z+ M; F) n( Y* f, l
2、性能优秀,这是一个最能吸引眼球的话题。8 ]7 C" j, j5 {
3、尽量使用大众的框架(避免使用小众的、私有的框架),新招聘来的开发人员有一些这方面技术积累,减低人员流动再适应的影响。
/ L$ @) r: {2 e+ G3 x
2 y% H* M; V8 L4 f如果你还在为这件事件发愁,本文最适合你了。选择spring MVC吧。
% C6 M8 Y- E4 d" B( c3 Z3 `9 @$ I8 l% c- e8 Z# H
Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对struts 2的超越。现在越来越多的开发团队选择了Spring MVC。( B+ s( W+ ~$ @
) X. H! i. }( OStruts2也是非常优秀的MVC构架,优点非常多比如良好的结构,拦截器的思想,丰富的功能。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降,应避免使用这些功能。而Struts2的多层拦截器、多实例action性能都很好。
/ k# A. _7 ~. _* s4 z7 `3 d ]4 A
4 [+ }- L7 u7 @/ b- hSpring3 MVC的优点:+ x$ Q& A. t: X: I* V
1、Spring3 MVC使用简单,学习成本低。学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。
+ p$ z0 F0 y& C5 x2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)7 s0 U" u- a4 m/ b
3、Spring3 MVC的灵活是你无法想像的,Spring框架的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。% [& q# z! ?/ O9 ^2 d
; l+ ^$ J5 C7 e. K1 h" g/ ]3 pStruts2的众多优点:
7 w: K3 \0 c) I7 A; F1 k1、老牌的知名框架,从Struts1起积累了大量用户群体。技术文档丰富。/ W+ V" J- z/ R% S+ v: B) ~
2、其它方面略... 1 D* D. q# D9 _& u" y4 E
==============================================================
A" a/ X7 R, T# S [. d# J1 b2 m) S什么是mybatis
" v( c( ]7 I! ?2 OMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录.
3 C$ F7 }4 K1 r& ^. n3 V$ I
: g5 m0 c) I0 t& R) S8 l* _" horm工具的基本思想5 T* ~. ] Q p( L& E
无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:
) D' x* c$ X b6 u1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.
1 W1 @. S1 O& A) o" V& \2. 由sessionfactory 产生 session+ f2 G7 s, E1 k0 H, n
3. 在session 中完成对数据的增删改查和事务提交等.' Q& x" P: g! |: N( J/ w9 H
4. 在用完之后关闭session 。5 m7 F0 I: w* L3 l( w
5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。
4 p( @. J( B9 T! a
" {5 y" }; D8 h8 n% B项目说明:实现了用户登陆demo。
J0 \/ ?3 c- m' o- V项目配置说明:1 [; a; {; g6 ~, s
web.xml:
7 F% S4 K# Z+ H& y9 e2 I- <?xml version="1.0" encoding="UTF-8"?>
1 x( U5 \! V. V8 n7 \7 g - <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
6 V$ L& o' e" _( y2 c) g' l - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
% d; {5 l o) C6 D - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 7 H* s4 D/ G3 r1 e8 G+ r
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
7 w1 f5 g& Q! w- I) F - 4 L0 X4 I0 G* w# Q
- <!-- 监听spring上下文容器 -->
+ w8 t7 P' v0 N - <listener> % U/ I8 e* R; \# Q' r. D9 e- ?
- <listener-class>
0 e& D& T2 ?) R% n( `5 a( x2 `$ N - org.springframework.web.context.ContextLoaderListener - ?; Y. t" x7 I
- </listener-class> / ~$ B4 N- j9 G/ n9 O
- </listener> 7 V2 e4 C; q, A7 J, T) |, @$ M
- : D5 Z' C$ w0 j
- <!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
3 ~# N: F, h: B. w) S/ l% M2 U Z4 e - <context-param> 1 @& R0 Y5 X. ~1 w! ^' i
- <param-name>contextConfigLocation</param-name>
& s4 S4 L, v6 h* K! ~% W h - <param-value>classpath:*-context.xml</param-value> " C$ A2 |* Y& C- x1 {! d# r/ `
- </context-param> # H: _' M6 i; j& D c
-
% G1 }& u% S1 M- E9 U5 W* s' H3 N) z7 a - <!-- 配置Spring MVC DispatcherServlet -->
" }. h6 w0 U$ h: p9 |5 k - <servlet> % c! f& I/ {% D- A3 h
- <servlet-name>MVC</servlet-name>
9 q1 G/ F% m f# C+ }/ _2 t! [ - <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> , I0 } L2 x/ c9 E& Z( K
- <!-- 初始化参数 -->
" R' k3 R+ A1 z$ M" D - <init-param> # I; p! A1 y- c: q- a+ |
- <!-- 加载SpringMVC的xml到 spring的上下文容器中 --> ! L& t( a8 l1 H: Y3 F
- <param-name>contextConfigLocation</param-name> " u! N/ }, x& L- d7 H
- <param-value>
4 v% z! [: H6 f - /WEB-INF/classes/mvc-context.xml ( c Y& @ i' m0 |$ @7 u
- </param-value>
6 g+ z/ _% t, a# _4 ]. u - </init-param>
" p2 Q& Q) T" y$ x% |1 y6 ` - <load-on-startup>1</load-on-startup>
3 H. W. j6 C4 o - </servlet> & E% W* Q8 m+ _% ]3 i
- 4 T: E+ ~+ C) G$ n
- <!-- 配置DispatcherServlet所需要拦截的 url -->
9 f, X/ Y% b7 Z+ X4 `$ s3 V - <servlet-mapping> & g2 S; d) m! l3 x
- <servlet-name>MVC</servlet-name>
8 E k9 E, ^) j! \0 [5 E) Y - <url-pattern>*.do</url-pattern>
+ D2 [( L* x* @9 a' m. d3 R* i* | - </servlet-mapping> / H& d) g2 @& Y
-
Q1 P; M# V4 N- p% a- ], b - <welcome-file-list> 0 Q9 M& k! R% K! d2 T# U/ ^4 I
- <welcome-file>index.jsp</welcome-file>
( R- @ f% R8 y6 x |8 V - </welcome-file-list> & C, c- F- i* l+ B
- 3 D( F5 F1 R: f" H m3 i
- 8 _0 @8 T5 U: D% { R V* g/ P
- </web-app>
复制代码 5 { b- }4 l- k9 U/ n& G
mvc-context.xml:+ k, r, F+ `) a% k. m1 g2 G$ R, g/ r
- <?xml version="1.0" encoding="UTF-8"?>
! ^& {9 i" F9 g7 Z+ M/ F# ] - <beans xmlns="http://www.springframework.org/schema/beans"5 C. W3 S9 }0 n5 L; U1 P' o7 b
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
, P, [* G( x9 s, Q9 I, ~9 E - xmlns:context="http://www.springframework.org/schema/context"9 h/ U4 ~9 U. `) X
- xsi:schemaLocation="http://www.springframework.org/schema/beans) E3 O2 I) x- [ X @
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd5 ~, P3 v0 B7 H3 ]: H( X$ _
- http://www.springframework.org/schema/context( [7 @6 Q8 h/ D. t
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
1 `: k% V* C2 R0 u
# l/ i7 Y8 Z/ \* g: G, v7 h/ D- <context:component-scan base-package="com.wg.*" />
: j, \) A/ ~9 K* B7 E2 ], B - 1 ?0 e- d3 h A2 Q8 ~6 P- K: a
- <bean id="viewResolver"( t' u/ O: J& O( l2 @7 h; g
- class="org.springframework.web.servlet.view.UrlBasedViewResolver">+ P. _3 t$ y' D+ B. {
- <property name="viewClass"
]8 ^' T) H2 S v1 O - value="org.springframework.web.servlet.view.JstlView" />: @; ^3 P4 |+ Q# a9 I
- <property name="prefix" value="/page/" />
9 b3 H5 z3 n2 v - <property name="suffix" value=".jsp" />0 k9 v2 U8 `: Q2 z- s& U
- </bean>
7 `' v ~4 O1 @: A& ?$ O7 U - </beans>- g+ H g' l( e
复制代码
" N" U) P5 H- |* ~$ H! zmybatis-config.xml:/ q9 m# R/ ?$ ~# K% L. q5 ]& X
- <?xml version="1.0" encoding="UTF-8"?>1 B, Q' b/ O- M' k3 M0 K: M7 _
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+ y+ T: e L, z* C3 X1 ] - "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 U' o7 L. X0 p4 b& ?; ?* | - <configuration>) p; q* l% }, \; U6 l9 H7 {$ B2 C
- <typeAliases>9 K9 k, x* U+ w' ]
- <package name="com.wg.bean" />
9 P* E# r; ^0 T- M% G1 M- L - </typeAliases>- e, S/ I9 o3 D* [
- <mappers>
8 w6 E1 `8 W7 f - <mapper resource="com/wg/bean/UserMapper.xml" />3 G ` q1 `2 H: V/ X- e
- </mappers>& W$ x1 G( [& e# G; ]
- </configuration>
复制代码 root-context.xml:0 ^( W3 s0 f# J
- <?xml version="1.0" encoding="UTF-8"?>
( q o( A& a2 N+ i! d$ y, x - <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ( z9 f3 t" q/ ~$ o: g( I
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
. q9 T7 w) v9 ? - <configuration>. {2 a" ~! d9 W% o& H. e
- <typeAliases>
1 B, M0 ^) d7 s3 c" J! {2 Q - <package name="com.wg.bean" />4 z; r/ R. l" r* w4 {3 f- M
- </typeAliases>
0 O* V) \* `1 b/ H o! q - <mappers>
, U! X7 ?# F: e( b/ H& H - <mapper resource="com/wg/bean/UserMapper.xml" />
) n5 W9 G' S' ]! f+ y: x - </mappers>7 A2 N2 w6 [. D6 Y
- </configuration>
复制代码
8 n+ a) J) _( c& c* p% P8 x/ _. Auser-context.xml:- <?xml version="1.0" encoding="UTF-8"?>
9 u! q" N; H5 O# F6 z# M - <beans xmlns="http://www.springframework.org/schema/beans"4 z1 L0 [5 O. n8 b9 K
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"! L% X9 y+ o2 [) [2 X
- xmlns:context="http://www.springframework.org/schema/context"
& G5 ^( ]& S5 q) U+ | - xsi:schemaLocation="http://www.springframework.org/schema/beans8 I5 H$ K& B) e: M+ O
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd M0 W2 v: _4 N7 k/ K; L. `* B
- http://www.springframework.org/schema/context' w1 `8 U) e9 e$ M$ i
- http://www.springframework.org/schema/context/spring-context-3.2.xsd" >7 p8 o- g5 i8 U! R6 J- \
-
' l- G! b. R! H2 q9 ]( d -
2 n1 ^4 h. t5 n" y6 k' M1 T - <!-- 用户Dao -->. F# K+ I# G% p. }
- <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 9 K2 R6 h6 F6 A4 T! i
- <property name="mapperInterface" value="com.wg.dao.UserDao" /> 6 u0 W( W m- g( E8 }1 O7 v
- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 3 Y3 T7 J4 d; q5 |* Y+ L: T
- </bean>
6 [' H) d: h4 k* e -
) O2 y& M5 z( y+ T; B% P K4 { - 5 A1 O& i$ w. H8 l o0 i6 [
- </beans>
复制代码
" A& i" G) q. q3 j! M# F2 ~2 v2 I
" r4 k# H! p% i( @' S+ v8 Y1 g
" Y5 ^) c; i3 S$ Y# o( h链接:springMvc整合mybatis项目源码7 u6 V& n" W: b( k4 T( b# _
密码:
8 p9 A0 S5 U6 L g% Y! {
+ D6 c+ r" e5 u; o% A/ q; g3 |( M F, ?9 A |/ g
( B+ ^: T. p6 `: x* v- t3 x. u
|
|