TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
一、前言:& y2 }* S) P- a1 { A5 |7 n
为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平。你的一个决定会影响团队未来的几年。要考虑方面太多:: v/ t* p* \% s. Y( M( [
1、简单易用,以提高开发效率。使小部分的精力在框架上,大部分的精力放在业务上。
9 J9 e4 O/ _4 S \5 ^2 y2、性能优秀,这是一个最能吸引眼球的话题。4 @! X( j; v# S4 g5 V) _7 x. L* L
3、尽量使用大众的框架(避免使用小众的、私有的框架),新招聘来的开发人员有一些这方面技术积累,减低人员流动再适应的影响。
/ K9 w4 W6 V8 o( Y9 _' r S- `- i* f/ |3 V6 @3 F
如果你还在为这件事件发愁,本文最适合你了。选择spring MVC吧。/ a4 J5 J$ f6 {. n( T
4 S E3 e3 e P' P
Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对struts 2的超越。现在越来越多的开发团队选择了Spring MVC。
% D0 ~4 z& Q6 G8 P5 P6 m! u0 [0 `& n( D# |* e, g; i1 t, c3 ^+ G
Struts2也是非常优秀的MVC构架,优点非常多比如良好的结构,拦截器的思想,丰富的功能。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降,应避免使用这些功能。而Struts2的多层拦截器、多实例action性能都很好。' D1 k# c9 d$ `" n! N
, Y, f) @: f; o# \+ h% R
Spring3 MVC的优点:
0 I: h9 M/ q7 E1、Spring3 MVC使用简单,学习成本低。学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。
1 a- J" ~) m) N: j: }! L7 }2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分): r S" i; A0 c) Z+ i% a
3、Spring3 MVC的灵活是你无法想像的,Spring框架的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。
! `9 V$ ?, \: z3 w8 L
/ H% I) l* z2 K; V/ K9 yStruts2的众多优点:
6 V( f$ o) Y* Z8 e4 _5 [# a( b1、老牌的知名框架,从Struts1起积累了大量用户群体。技术文档丰富。
# h4 V4 Y4 n$ `$ S) \2、其它方面略... 4 e9 [" \$ F* A9 H0 A5 \5 A) q5 D0 t
==============================================================1 q% f% k7 e1 M8 T
什么是mybatis
~0 q9 l) ?& }- p, t7 z. tMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录.
# d( X# H+ A: n# Q. {8 g
, J: ]' i# E0 S2 W1 A5 ^1 o8 @: q+ Jorm工具的基本思想. C: X4 y' r1 o
无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点:
% N9 J0 c, O: b# d! w3 B2 `1. 从配置文件(通常是XML配置文件中)得到 sessionfactory., z; O6 ]% N' c
2. 由sessionfactory 产生 session
) B; J6 V6 f: j: s w3. 在session 中完成对数据的增删改查和事务提交等.- n. c; r( {, j4 B4 ^1 v" m
4. 在用完之后关闭session 。
7 t1 b; G F; j5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。
F) f! E# E7 t! v7 _, G; _
4 @" H6 b) G7 x8 J8 |, O1 y; k项目说明:实现了用户登陆demo。1 l; t* q! y. m* D0 w
项目配置说明:
9 j) s6 s- G* L8 k/ d% }web.xml:
& j& u* N5 L% z1 k0 t3 }- <?xml version="1.0" encoding="UTF-8"?> ; f" H1 Q: S7 G3 ? ?
- <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
5 q8 }% L$ r+ D3 T - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 1 r: C- n, N, X, d
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 3 e$ z1 P$ T6 R/ g5 }
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 9 S) s z4 K- b: {# o2 R% j
-
! T x6 H+ u0 }; |+ u/ g) ^7 Q4 Y% F - <!-- 监听spring上下文容器 -->
& i Z) A1 U1 R" R, o' {6 C - <listener>
6 S+ h% t3 C- v - <listener-class> . s: J4 }$ |$ r$ [8 \
- org.springframework.web.context.ContextLoaderListener 0 F" h. |' K1 j j p# Y
- </listener-class>
4 \5 P. [* t1 l+ A - </listener>
" v8 M: b* Y7 a2 i2 s9 ~ - # o% z( g# N' W9 a4 y
- <!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
% K& U& Z# S! `7 [9 d4 W$ i9 Q - <context-param>
1 Y3 Z, W& A5 o5 y. w - <param-name>contextConfigLocation</param-name> 1 r% m7 @9 ~" m+ w, \
- <param-value>classpath:*-context.xml</param-value>
4 x. F) ?5 P4 I6 f" Z - </context-param>
& K% I8 }. t% d$ U - : ~* ?% N8 C, ?; P
- <!-- 配置Spring MVC DispatcherServlet --> . F/ o7 p. k, ?& a- |) z
- <servlet> 4 y" p7 K3 ^) ?; R
- <servlet-name>MVC</servlet-name> : C0 K: c) `: }2 A/ O
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+ F- s' F( f/ u% ?1 [7 U - <!-- 初始化参数 --> + s ?) `9 c5 V! e, A3 F. A
- <init-param> * r2 A& h- g% I' Q" u
- <!-- 加载SpringMVC的xml到 spring的上下文容器中 --> & N1 w& i" f* t; Z' H7 ]8 e
- <param-name>contextConfigLocation</param-name> $ r$ l) B0 T1 K* t5 j' ?: Z# \
- <param-value> # T. `2 z" i5 |3 s
- /WEB-INF/classes/mvc-context.xml " l4 L% X- V4 D1 C, ^7 a& a6 S
- </param-value> z+ g$ u2 {, c7 t; F
- </init-param> 2 B% W* D3 M @; S1 w9 c; T
- <load-on-startup>1</load-on-startup>
( ]6 F N) a! Q* b+ ] - </servlet>
+ s! P3 k k9 b1 }4 G, O - . V% V, W$ \: o. W7 F2 z% h
- <!-- 配置DispatcherServlet所需要拦截的 url --> 0 W2 x* F( U9 e& f
- <servlet-mapping> ! h) e/ M+ S1 @+ p/ K# ]0 m5 q
- <servlet-name>MVC</servlet-name>
* e& L, N; h0 I: w. |) }- Y2 q0 S2 b: b - <url-pattern>*.do</url-pattern> , K4 y+ Y3 @$ A% e
- </servlet-mapping> : |* T! `; O+ n, b7 a& } U
- ! l& f. r! d3 N% k+ U! H, w
- <welcome-file-list>
6 P( B* G1 m7 q: @9 E) } - <welcome-file>index.jsp</welcome-file>
4 D9 W+ f# A4 I# ` - </welcome-file-list>
$ K. B/ |+ S! b: V, m( V3 X' P -
5 o% H: F& r* T, W+ H! K5 _ - 2 o5 m4 P% D' _
- </web-app>
复制代码
/ f3 e3 M- \8 u7 p: L& e+ b7 xmvc-context.xml:
+ j; E7 Q" M* z% W; I- <?xml version="1.0" encoding="UTF-8"?>! b9 f# R, n& f7 h$ d( G
- <beans xmlns="http://www.springframework.org/schema/beans"5 a2 Z* _6 K F. y% n7 O$ y
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"- }" W% l. @& W
- xmlns:context="http://www.springframework.org/schema/context"2 V9 @0 A+ t* Z% I; |3 d0 J
- xsi:schemaLocation="http://www.springframework.org/schema/beans
. h! @2 k) E( @3 `0 k! P6 f - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
# Z7 N' [' o1 G1 u5 ^ - http://www.springframework.org/schema/context
' b9 I. R/ [* p7 Q, {9 g - http://www.springframework.org/schema/context/spring-context-3.0.xsd">
~! {/ U! f$ ?; h
% r( \5 V2 r( p/ E8 K- <context:component-scan base-package="com.wg.*" />, @8 i3 B- D# m) P6 K# |8 N# L
, w" Q3 M$ O* I; U9 R- l. T7 M$ L+ U- <bean id="viewResolver"- G$ W' R8 p) |& O W% g
- class="org.springframework.web.servlet.view.UrlBasedViewResolver">
: q* W/ Z' @; c5 c" p4 C0 A9 a/ b! l - <property name="viewClass"
6 O- k7 @! q7 w# M - value="org.springframework.web.servlet.view.JstlView" />
$ N5 M; J, ?6 Y4 N; H& u E/ ~9 T - <property name="prefix" value="/page/" />
2 C' {/ ]; u: S# P5 @1 [( }+ V - <property name="suffix" value=".jsp" />- b/ ]3 U# [9 q- G4 P
- </bean>6 G P; H( B0 \/ V1 S
- </beans>
, x: U, q. k" R) d
复制代码 U: l; H2 y4 w5 s
mybatis-config.xml:
1 A2 H' S1 e" {$ g n$ [- <?xml version="1.0" encoding="UTF-8"?>9 S) k! A8 U8 v( a7 Y) _* s6 P
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
! t3 K9 y/ W' W - "http://mybatis.org/dtd/mybatis-3-config.dtd">
3 v( K7 N+ _! b6 y1 N& A' Z - <configuration>
( x4 f% z' l) {0 n - <typeAliases>) L6 K, Y7 W7 n5 }( ~
- <package name="com.wg.bean" />- J6 M3 k5 {; n: c6 B$ F
- </typeAliases>
2 X7 g( Y( ~/ G4 A - <mappers># m+ Y* Y3 I: K4 i5 a
- <mapper resource="com/wg/bean/UserMapper.xml" />
2 c4 q- O/ b8 ~5 c1 c6 }) w7 e; g$ T - </mappers>
L( N1 A9 ?# b6 O - </configuration>
复制代码 root-context.xml:
! r* X5 f3 R% {) k% J- <?xml version="1.0" encoding="UTF-8"?>2 K4 [/ K2 |! O( t" T! K5 A
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
7 I# {& @+ D5 \0 C - "http://mybatis.org/dtd/mybatis-3-config.dtd">
) ]0 W( E9 j, k$ G - <configuration>1 o& P$ V& a6 ~! p
- <typeAliases>; v. M* ^0 i# F7 \
- <package name="com.wg.bean" />: v- i& P0 |: e& F" {/ l
- </typeAliases>
1 i5 @* h: w1 i" w! V! k4 O - <mappers>
0 W) W, Q# d% Z- I5 q) _+ Y7 t - <mapper resource="com/wg/bean/UserMapper.xml" />3 N) Y' F J# D) W
- </mappers>! J; V7 w/ X G1 T
- </configuration>
复制代码
% [3 V; w1 d" A/ @ `4 r( L+ L! ]user-context.xml:- <?xml version="1.0" encoding="UTF-8"?>; B9 Q- g! ^5 n9 R& N
- <beans xmlns="http://www.springframework.org/schema/beans"
5 {1 W9 w5 c0 h3 B- R9 h' t - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 |6 g" v; \# C' Y: ^6 m8 ? - xmlns:context="http://www.springframework.org/schema/context"- K6 S. J9 y8 ^$ V/ H. A
- xsi:schemaLocation="http://www.springframework.org/schema/beans
; d3 w! A' G" @/ p( |" s- o - http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
* `9 J7 v* `( {+ m7 X5 h: P+ d - http://www.springframework.org/schema/context
# I0 |2 O6 }0 D0 x - http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
1 L! P! s2 g& U$ E - ( n8 b" M5 W, y1 [; k8 {
- 9 Z, K9 ?. A h3 g7 f- S
- <!-- 用户Dao -->" m3 K, u" o( l: Q3 E
- <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
( T4 T6 l( c; K% v - <property name="mapperInterface" value="com.wg.dao.UserDao" />
# u6 B0 h# f% k2 A$ G) T9 t - <property name="sqlSessionFactory" ref="sqlSessionFactory" />
8 K1 `/ g: o+ z1 J - </bean>, l% N$ ]& a3 `- P/ W' _
- # M: o* V" \$ v2 V: i
- 9 E4 m3 V! o9 R: H/ z/ y# Z" s" G5 S
- </beans>
复制代码
8 \$ a$ Q$ Z/ ?2 S9 O
' _, q; `1 Z- i; Z( ?2 c z+ ]0 F6 D# P8 E, d+ Y
链接:springMvc整合mybatis项目源码
1 ]0 [1 c3 m0 P! r 密码:# j4 E8 D( F9 |
; C3 ^* X, p9 w. w
4 _/ \0 r+ p% i: W% w4 {% {2 U/ d' j2 c
|
|