TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
一、前言:
6 F, E& _! F. w3 E; J为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平。你的一个决定会影响团队未来的几年。要考虑方面太多:) i: [' y( o% s Y- H8 R
1、简单易用,以提高开发效率。使小部分的精力在框架上,大部分的精力放在业务上。 ~9 W, ~0 `+ \' a
2、性能优秀,这是一个最能吸引眼球的话题。0 D. L8 L" e5 h9 v7 h9 Y
3、尽量使用大众的框架(避免使用小众的、私有的框架),新招聘来的开发人员有一些这方面技术积累,减低人员流动再适应的影响。! {/ K# E5 c4 l9 Q, |
( e: H$ f* f& b, ^4 X* p如果你还在为这件事件发愁,本文最适合你了。选择spring MVC吧。' |$ _6 z! \' ^
1 q" Z& J) r3 {7 b3 w$ sSpring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对struts 2的超越。现在越来越多的开发团队选择了Spring MVC。0 ~ I# Y. [8 ?& t3 ?
+ I* [1 Z3 D. _' h1 u3 P
Struts2也是非常优秀的MVC构架,优点非常多比如良好的结构,拦截器的思想,丰富的功能。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降,应避免使用这些功能。而Struts2的多层拦截器、多实例action性能都很好。9 \( A. {6 U! \! @# b9 @7 I, [
. P, l" w: F) E- DSpring3 MVC的优点:
2 {# N3 ^# K$ Q$ C) [: L& J: K0 O1、Spring3 MVC使用简单,学习成本低。学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。: ]" t% c$ Q$ z4 v! t
2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)
, f# M% F3 J) ?6 f& p8 T3、Spring3 MVC的灵活是你无法想像的,Spring框架的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。
; M; U) V& i/ ^! f7 f7 I5 M& u2 t5 {' W0 e! u/ N
Struts2的众多优点:. x$ D: _( \! G* K0 A' I x) E
1、老牌的知名框架,从Struts1起积累了大量用户群体。技术文档丰富。
1 X" Z/ q' i/ K# E3 s( ^2、其它方面略...
! S! N0 n8 S# v$ Z$ c9 r! c============================================================== h$ @$ q! p# [
什么是mybatis
% S# e9 R% D& v& b: PMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录.
9 G4 H' c( ]9 [% u8 e! [: q
* Y* Y7 B1 T1 T( Z# ?( |orm工具的基本思想# f/ J8 C' Z5 b% `& h
无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:8 T( ~% v! n3 k0 h% ^/ Y0 e+ v0 \/ n
1. 从配置文件(通常是XML配置文件中)得到 sessionfactory.& b- i& j5 E7 Y* Z1 d6 E
2. 由sessionfactory 产生 session L7 |0 v6 U' i2 S' ^
3. 在session 中完成对数据的增删改查和事务提交等.
% x2 d. \8 E' I6 n. e% i8 m$ T4. 在用完之后关闭session 。& c5 ` {# o9 ~9 H/ Q2 q5 c4 Y
5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。
( W' _: B+ j3 X1 o2 a e
' B! w# O& z! |" D( a项目说明:实现了用户登陆demo。! M; r, t W" e( w0 |) g
项目配置说明:0 ~- j: J& K) x$ B, j, B' v
web.xml:
2 n- n( s$ a/ U' D% S- <?xml version="1.0" encoding="UTF-8"?>
5 B5 c1 ]" E: W" o$ }7 D% A# B% e. W6 \ - <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 0 T( C% ?$ |( Z
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - E& g8 k @& U7 m& X
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 4 v L9 ^* ~6 l P1 L! M
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> : X4 Z! V6 K, s# P7 k
-
% x5 A+ V1 K* }1 D" S- p" L- u - <!-- 监听spring上下文容器 --> , ]7 F) q5 A( j C" e7 D
- <listener>
3 O1 x; H5 K/ G* N# L - <listener-class>
( k- ~- X6 h) n( I - org.springframework.web.context.ContextLoaderListener
, h( m4 J/ S+ Z* ^2 k - </listener-class>
, x: `6 C/ }6 p8 G# e - </listener>
6 P) t0 Q& M8 } -
2 x! C. f. u- Y6 Z2 F6 G0 K - <!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
" C6 |% W/ {# Q+ r# t+ ^ - <context-param>
* y" u0 L3 M+ M - <param-name>contextConfigLocation</param-name> + b& R" ^1 s+ _* l: ]
- <param-value>classpath:*-context.xml</param-value> & d7 D$ O- f8 V3 v9 i- _
- </context-param>
" x9 @# {7 l( t6 A6 Q& t/ |- k6 b - 2 ?) V6 V1 H% A; L# W, x7 D5 M6 e
- <!-- 配置Spring MVC DispatcherServlet --> : P: {, j( G& v2 ~- Y
- <servlet> ) [# o3 |/ G5 {) B' i% S w
- <servlet-name>MVC</servlet-name> ! W0 h6 l, Q8 [: R' _& k
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
! y8 h3 f1 O: V. P6 K6 K - <!-- 初始化参数 --> R6 K# d1 y$ w- Q, u e
- <init-param> A3 z+ n! w+ r: N5 l
- <!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
& m9 T4 d% G" p - <param-name>contextConfigLocation</param-name>
7 u4 @- z5 `+ s# n* d - <param-value> 6 B4 I$ [ }9 M+ L- ]
- /WEB-INF/classes/mvc-context.xml - Z) R) N* h/ {: O1 {
- </param-value> " X8 P. i% a7 y2 \8 ]% ?
- </init-param>
- R5 b. V6 w+ U7 s5 P7 _: w/ n1 y - <load-on-startup>1</load-on-startup>
+ }1 y+ I& D( x" R1 i - </servlet>
, {1 S' m7 u6 `! @; w -
" q7 _2 m' a9 D% P - <!-- 配置DispatcherServlet所需要拦截的 url --> ' @4 T& M# Q6 l- `
- <servlet-mapping> R5 B, [# J f: y% E
- <servlet-name>MVC</servlet-name>
0 j% s5 k, U7 R% p) H - <url-pattern>*.do</url-pattern> - Q; h7 w; K5 H# o5 X, O0 d
- </servlet-mapping>
. \8 L; e) @" d" o1 t -
7 O; [0 \3 A3 U& S) ~/ y6 @) X - <welcome-file-list> $ k" N2 R. k% l2 x N: p
- <welcome-file>index.jsp</welcome-file> 1 T( F& [8 h) c8 X; U
- </welcome-file-list>
+ y+ k* W2 }8 R1 j -
& O" M5 z* Y( u -
5 U/ A0 G- F, h9 S& J - </web-app>
复制代码 7 b l7 j8 C! E1 D/ M6 N1 t$ X5 Q
mvc-context.xml:
1 P# J% N* k+ t- <?xml version="1.0" encoding="UTF-8"?>
# O$ ~: p4 u# m6 E G; ~1 w6 S - <beans xmlns="http://www.springframework.org/schema/beans"
7 L4 E" Q5 t' N* o2 E: t - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"+ I! {* d; B9 R+ B: q, r
- xmlns:context="http://www.springframework.org/schema/context"
R2 P; @8 N* U8 j% f - xsi:schemaLocation="http://www.springframework.org/schema/beans+ D$ C4 c) t4 x1 W/ \/ e
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd) M) ~; {9 M; ]+ Y+ B3 J l
- http://www.springframework.org/schema/context
. x' x& c9 Y2 d( k - http://www.springframework.org/schema/context/spring-context-3.0.xsd">
8 I' m* O4 ]5 a! b* ^; q9 x - + h- o8 w! ?8 ]9 v. o! O
- <context:component-scan base-package="com.wg.*" />
) ~/ ?5 E& f' K5 w" r - ) {$ d2 A, i% p* ]% ?
- <bean id="viewResolver"
# Q9 F6 y) u. ~ ~ - class="org.springframework.web.servlet.view.UrlBasedViewResolver">
! w( F, Q6 a0 h* o8 K, y1 J - <property name="viewClass". I" {* X+ T5 R8 ?2 l
- value="org.springframework.web.servlet.view.JstlView" />
, A. s4 [) x2 z! s+ ?1 Z% k - <property name="prefix" value="/page/" />
( g) p! L( A0 |8 J; [ - <property name="suffix" value=".jsp" />
2 K, z7 w5 y' S) c' v M - </bean>4 V4 K6 X4 Z4 y& a' Y
- </beans>
9 w# n6 z8 V0 {" J P; O! S
复制代码 1 F: U( Q& p" N0 G( [$ O3 J1 }
mybatis-config.xml:
# e S) {) V4 q# }4 }- <?xml version="1.0" encoding="UTF-8"?>
3 U$ ~4 n: U* Z( d1 I4 U - <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 Y7 n, g/ u# l R% S) b0 z* M
- "http://mybatis.org/dtd/mybatis-3-config.dtd">4 z4 I7 P, K! _. J @
- <configuration>7 ]# j* R0 E. ~" H
- <typeAliases>
+ r4 f ^' Y* V. G: Z0 ^ - <package name="com.wg.bean" />
; V; k/ N) V4 Z3 B - </typeAliases># i4 I/ G) x( H$ C5 ^! A( Q
- <mappers># K1 C3 J9 A( L* P
- <mapper resource="com/wg/bean/UserMapper.xml" />; s" _* n+ I! y J8 ?. L1 c p
- </mappers>5 p2 A% E6 p' r: }7 s
- </configuration>
复制代码 root-context.xml:
* w% b' p! u- j. Z4 U. l7 V- <?xml version="1.0" encoding="UTF-8"?>9 b- {% J, y0 o' i; G
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
' }3 n2 J6 i8 K* A( e9 _2 P9 y - "http://mybatis.org/dtd/mybatis-3-config.dtd">
, A/ W F3 m* W" y9 j - <configuration>
9 x6 H/ Q0 i7 `; X Y5 ]8 I - <typeAliases>$ A$ C! V% m. D5 W0 o1 E( X4 ?
- <package name="com.wg.bean" />; `0 g* a6 |4 ~; V: T5 ?8 h
- </typeAliases>
/ Z: X0 f, O2 h! \7 |8 A - <mappers>- D) m% K% a$ p& @7 ^6 c2 R
- <mapper resource="com/wg/bean/UserMapper.xml" />% F& ~* d8 \1 q5 J
- </mappers>- ?# T# Z* e! U9 q( F% d) Z2 s5 N
- </configuration>
复制代码
& Y" N( j" E1 R6 tuser-context.xml:- <?xml version="1.0" encoding="UTF-8"?>) Z. @, z6 @3 q4 w* M6 s
- <beans xmlns="http://www.springframework.org/schema/beans"
, R+ M! F' ^+ [9 B - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"- z7 S+ ?; v1 B' g5 l$ ]
- xmlns:context="http://www.springframework.org/schema/context"
" C* ]% i8 _1 x1 D - xsi:schemaLocation="http://www.springframework.org/schema/beans+ u9 T k) _. c$ _
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd: O! f( \, s" E6 [
- http://www.springframework.org/schema/context
* K# y: h2 h& {* F+ ? - http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
0 k1 R! r) Y7 m' V7 K+ L -
6 |' r, T8 q" @3 l$ K -
/ l- \2 Q( T6 M. G7 T9 a2 b - <!-- 用户Dao -->$ L6 e7 @/ V. z3 e) N/ J
- <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> * A+ n8 t# y( E$ `$ ]
- <property name="mapperInterface" value="com.wg.dao.UserDao" />
4 {3 B, D% o- U0 W: ` i5 L/ g - <property name="sqlSessionFactory" ref="sqlSessionFactory" /> / ]. r3 x$ o& K x6 K6 F+ L; q
- </bean>0 l% y! `6 @8 C0 ?3 E
- 2 l; b; Z0 C3 L O3 P% F8 c
8 [7 T2 @, _$ a) b3 |- </beans>
复制代码
$ l5 V' H) n' ~% ^" f, U
; z$ ?& T Z/ d1 \/ j5 l- B3 g" z; I3 Z
链接:springMvc整合mybatis项目源码 @2 u ?6 K0 |# T$ A
密码:. @0 m$ E6 J+ B: `5 {
2 E3 k9 [5 Z% o( J- ~6 W
5 x" K' k0 _/ ^
) h' z# q$ g" V1 Y
|
|