TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
一、前言:* r4 V( W+ Y% C7 L" @+ \/ C
为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平。你的一个决定会影响团队未来的几年。要考虑方面太多:" J5 m8 q* N$ O
1、简单易用,以提高开发效率。使小部分的精力在框架上,大部分的精力放在业务上。
( ]8 {6 {5 @: u. Z, Z7 e6 X9 X2、性能优秀,这是一个最能吸引眼球的话题。
9 v. [- m9 k( V3、尽量使用大众的框架(避免使用小众的、私有的框架),新招聘来的开发人员有一些这方面技术积累,减低人员流动再适应的影响。
% F' g8 ?! q9 F0 V1 [ ~7 k" D
2 y: @" K" @- ^! G如果你还在为这件事件发愁,本文最适合你了。选择spring MVC吧。
% r8 H& _, i& ]- `# P. v* a
8 i! U7 {( E5 ISpring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对struts 2的超越。现在越来越多的开发团队选择了Spring MVC。
) ?7 }1 C1 J4 O' }2 ^+ K1 [& r( D: H4 x4 M) U
Struts2也是非常优秀的MVC构架,优点非常多比如良好的结构,拦截器的思想,丰富的功能。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降,应避免使用这些功能。而Struts2的多层拦截器、多实例action性能都很好。/ E% P* R0 o4 S5 c9 P$ G4 Y
6 v, i7 v( G, d7 b; vSpring3 MVC的优点:
+ D4 e P$ S% g, F& M9 R. p1、Spring3 MVC使用简单,学习成本低。学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。: I6 a F' w6 l& R6 O/ ]! n$ E! \
2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)
$ x6 a( h. g: z# S+ }3、Spring3 MVC的灵活是你无法想像的,Spring框架的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。5 {7 W9 G% c) ~/ X3 W- E$ G
# o9 c) Y1 o. X7 n) V# d. uStruts2的众多优点:
# K7 L- ^; [5 L2 B3 z) V1、老牌的知名框架,从Struts1起积累了大量用户群体。技术文档丰富。: I' F% i( `+ U' U% Q6 m2 z0 j
2、其它方面略...
+ f/ G( h/ h2 M6 M5 ]6 D==============================================================3 p& d3 g4 M& l- J% W; D
什么是mybatis
8 D, x a6 L% x+ i* NMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录.; A# j+ v3 g* Z, z x
1 X& S5 B. f9 z# H) n2 d& b3 L- T
orm工具的基本思想
5 c" Y0 c' @5 G2 s7 g- J; `无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:! S/ e) o7 J6 |. q5 q! V' n: m9 W
1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.
$ i: _( b. I; [" l$ y8 O8 P4 x2. 由sessionfactory 产生 session5 S% d3 W0 ]& m) V
3. 在session 中完成对数据的增删改查和事务提交等.
" g" _0 _) d0 Q4. 在用完之后关闭session 。* Z7 m8 }; O7 M( I& H" V W7 s
5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。
- C4 B- A. {5 p
7 C9 T! H1 f6 N H" z g项目说明:实现了用户登陆demo。" {6 _; K, t8 c! I7 [3 `5 q
项目配置说明:7 L; r9 v& \" n7 G. W/ {
web.xml:
) I' _" g; z6 R9 `! L$ I! B- <?xml version="1.0" encoding="UTF-8"?>
( s7 T. T/ q$ P/ ~2 M - <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
/ n2 b) w- H# X# B9 K* W, V1 a9 y - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 c& H# e6 |9 i - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee N) Y) w2 |; L+ K8 @! a+ J
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
, o( p6 J6 p: e8 k; Y - $ ?, `4 [2 w3 |+ p% L* n% G
- <!-- 监听spring上下文容器 -->
! s/ G: G7 Z; z# C& j - <listener>
1 O) x9 d+ ` f/ P( j0 J- ` - <listener-class> 5 b3 n3 x0 A: @2 o/ E7 T7 ~
- org.springframework.web.context.ContextLoaderListener
5 d# Y4 o- C# y$ A. G - </listener-class> 3 h {/ r E' g; Q. l8 o$ h
- </listener> , g; U- J% W+ W6 d# j6 N
-
+ ]/ {# `3 m- d% B! G - <!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
' t5 h1 C' o4 r - <context-param>
1 z) d0 n$ P+ ^ - <param-name>contextConfigLocation</param-name>
. x; U, Z8 m. h# {3 s - <param-value>classpath:*-context.xml</param-value> 9 l5 w! R% z* b! m \8 x. w/ c
- </context-param>
8 |4 h- n" M/ d1 ~3 f! ? -
& O9 |7 R( q5 Z+ } - <!-- 配置Spring MVC DispatcherServlet --> % u3 R' B. S- `5 z" q' t" g' D
- <servlet> . _) w1 a, F% W! o5 Y
- <servlet-name>MVC</servlet-name>
$ k6 h/ ? l! O& L - <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
/ W0 w- x8 Q: W3 L: k - <!-- 初始化参数 --> 6 `6 |0 Y0 m3 Z7 ^! ^ ~: y1 A
- <init-param>
5 \& E# B; `0 N* O: @% c- y, p - <!-- 加载SpringMVC的xml到 spring的上下文容器中 --> - o5 Z1 U! N' D# p3 B% l+ H+ e i
- <param-name>contextConfigLocation</param-name> ' J# ^1 x5 h: \5 P( _, \6 r' m" s
- <param-value>
' e v" \: M5 h7 \& j - /WEB-INF/classes/mvc-context.xml
4 a, L. j! m! x) x1 E9 t, k2 g6 V( L/ { - </param-value>
( w" `% H- @9 g/ w5 w7 l - </init-param> O# m! A. q& ^) X# ~8 `8 @
- <load-on-startup>1</load-on-startup>
3 M* U: B2 a2 `$ n- I' x - </servlet> 7 K5 T) j. D; y5 e
- 8 Z5 I3 m& e8 S( m
- <!-- 配置DispatcherServlet所需要拦截的 url -->
) U) a H* ]& t& Z - <servlet-mapping>
& J7 R8 p# `; h - <servlet-name>MVC</servlet-name>
: `8 J: Y" e& y& Y' X5 R - <url-pattern>*.do</url-pattern> * t# [4 |- ^4 O6 y$ B
- </servlet-mapping>
4 A6 e& g/ A/ b- q. B -
2 S! r% E( t& I8 D& d9 Q - <welcome-file-list> ' E6 Y7 t. ]" f1 J: Z! u
- <welcome-file>index.jsp</welcome-file> 7 ?8 E- u6 {& a( s7 Q1 V7 \
- </welcome-file-list> " c% P A/ |8 V- t# Q7 e( ^' e" V* z
-
# c M& W* S: k8 n0 j4 d - 9 o$ v/ V1 A( w4 l, o* F0 Y5 ]
- </web-app>
复制代码 ( [$ y9 r, Q! J0 p$ F, t; `7 l
mvc-context.xml:
2 v. _9 I& D6 r* M- <?xml version="1.0" encoding="UTF-8"?>
/ m1 X2 F6 g5 Q2 x# V8 T$ i: i - <beans xmlns="http://www.springframework.org/schema/beans" c( d- A3 }1 i6 f7 N' {
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"$ f+ P" Y4 I( P7 C" s1 S( d
- xmlns:context="http://www.springframework.org/schema/context"
' h+ O: _8 k9 i - xsi:schemaLocation="http://www.springframework.org/schema/beans
- a) O+ ]- I( `# }2 p3 k5 f - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd6 h4 [1 p, G" ^( m0 y8 M2 n) M
- http://www.springframework.org/schema/context
5 x* _6 b v) H) k* ~) A - http://www.springframework.org/schema/context/spring-context-3.0.xsd">
: j5 I' U% S3 D1 K9 E# o- j0 M
) b* l" a7 h. t- _8 Q9 m- <context:component-scan base-package="com.wg.*" />
- {- H4 d T( T% j9 [# c - " p% l# @3 e6 ?
- <bean id="viewResolver"+ k7 d- L- z1 l- L6 O! q* J# ~
- class="org.springframework.web.servlet.view.UrlBasedViewResolver"># X' T. K5 e0 D, W' p6 }; K
- <property name="viewClass"
" c& [; M" i2 [+ v7 m3 j - value="org.springframework.web.servlet.view.JstlView" />
- U {$ V* `0 [: J$ y) M - <property name="prefix" value="/page/" />" j% N s( h$ `" N
- <property name="suffix" value=".jsp" />
. l! y; C% q) B- p0 J - </bean>
/ M& W( Z2 g3 V- I2 l/ \7 N! y% A - </beans>6 W& M: k3 `, k% R' D/ l3 }
复制代码
; ]5 f" K X* @* }& ]1 d, h5 Lmybatis-config.xml:
* z& \/ {& n f9 s& `+ \- <?xml version="1.0" encoding="UTF-8"?>
# t2 k; b! L8 U n( v# X8 }* c - <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
( [: g7 m6 X: p$ E' X2 c - "http://mybatis.org/dtd/mybatis-3-config.dtd">* L! B0 O' }" z$ p- f% e8 s
- <configuration>4 F# s$ `7 T' l [8 P9 l
- <typeAliases>4 ^* ^9 c) `; G7 \7 y
- <package name="com.wg.bean" />
, O9 H/ G! Y, I# z% ] - </typeAliases>
/ `$ L W) ~& \ - <mappers>
4 [! I/ v* B s( c( L5 a( a - <mapper resource="com/wg/bean/UserMapper.xml" />
# A4 o8 Z! _1 Z! N1 X2 C+ Y - </mappers>9 F; ? S3 l) Q# C
- </configuration>
复制代码 root-context.xml:' x1 ~4 x; ]6 I0 v P7 U; m
- <?xml version="1.0" encoding="UTF-8"?>
W% l0 |! C0 N) r& U. @ - <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
. k1 w/ l$ E* B8 d - "http://mybatis.org/dtd/mybatis-3-config.dtd">
! I9 }1 K$ a; [5 [& F1 m - <configuration>" K) ]( l& H1 l& E+ N& ]7 P
- <typeAliases>
6 ]' Q: R( ~2 q d6 a - <package name="com.wg.bean" />
9 f' ?/ x$ X/ k$ u9 J - </typeAliases>
[) S! s7 H9 B; a. I - <mappers>
8 ^2 R) a+ M2 o - <mapper resource="com/wg/bean/UserMapper.xml" />
& m6 {# ?, ]3 }9 b5 o' G5 k! ? - </mappers>* h+ e8 X2 G' _- i; r1 F
- </configuration>
复制代码 % k& H, A/ b& H0 F; b/ o
user-context.xml:- <?xml version="1.0" encoding="UTF-8"?>- Q6 v# T, X% H
- <beans xmlns="http://www.springframework.org/schema/beans"
8 }6 C6 A3 }( f6 H" M: P" s - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
( n8 h$ l/ P5 F8 _6 a+ n& s% B, b - xmlns:context="http://www.springframework.org/schema/context"
; [& B) h) ^1 H8 t; d% ? - xsi:schemaLocation="http://www.springframework.org/schema/beans
0 M- M/ M- w: z0 o, `5 T. \" M - http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
/ i3 a, x$ c* I q - http://www.springframework.org/schema/context
f9 U# c) G. P' [# Z i1 U9 ^ - http://www.springframework.org/schema/context/spring-context-3.2.xsd" >4 A2 s3 q v% c3 n+ v! q5 R
- ) }* d K* h, ]* K3 H/ I
- 8 ~0 [7 d v# Q: C; b- Y5 ?" \
- <!-- 用户Dao -->
0 W" d- j7 k+ \. M - <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
' |8 T+ l. ?; V, i6 ?+ f - <property name="mapperInterface" value="com.wg.dao.UserDao" />
4 W1 X) {: u9 Q' V$ g - <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 3 R1 K+ R% c+ y
- </bean>3 d; w4 w; k: x( E) A9 v c/ |
-
0 b: m l4 A- ^6 U9 G. R; M
: P8 e8 ^7 T, V ~- |/ g5 {+ T- </beans>
复制代码
" k, c% O' U: K$ N1 k7 C
+ y$ E, s: x% u; H1 A f5 n) j2 X, O9 A
链接:springMvc整合mybatis项目源码6 m' f }% e5 K( j$ h$ }2 Y$ r1 |
密码:
) u6 o1 N. l& v4 t' ^
6 }; I7 y9 j" J' ?7 c4 x0 D7 Q+ ?' q& l- x# t
# t7 g" F; [: G |
|