-【优秀文档】ssh的一点想法_第1页
-【优秀文档】ssh的一点想法_第2页
-【优秀文档】ssh的一点想法_第3页
-【优秀文档】ssh的一点想法_第4页
-【优秀文档】ssh的一点想法_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、是我为新项li专门搭建的基于全注解方式的ssh基础框架,虽然是老掉牙的的东西, 自我感觉很良好,好东西不敢独享,晒晒等拍砖。概述:基于 struts2.23 + spring2.5.6 + hibernate3.6.4 + hibernate-generic-dao1.0 (除t spring,我整合的都是最新的ga包,hibernate-generic-dao是google项目 库屮一个开源的basedao,我灰常喜欢,因为我找不到更好更适合我的) 项目代码是基于eclipse3.6创建的,很简单,大家直接导入则可运行。1 包结构,源码,测试用例,配置文件一 h 了然。每个功能模块都在mod

2、ules包下 做开发,配置文件统一在resource管理(基实也没多少配置文件,都用注解嘛)。針 org厶 chinasb卩 framework ® core厶铮base> 凸 action> 鋅 dao> 务 search0 & db厶凸 modules厶 0 demo> 凸 action> 凸 dao> 凸 model> 凸 service0 凸 util& resources> 凸 content> 凸 hibernatet> & iog4j> & spring> 凸 str

3、uts凸 test厶 d org厶凸 chinasb 0 framework> 凸 core> 凸 modules2无论阅读哪个web项冃代码,我都是先从web.xml开始,项冃有什么东西一淸二 楚。我这里将iog4j监听放在第一,我想他应该能够从系统启动开启就能记录我的所有日 志(求认证)。第二个监听是proxool数据库连接池,听说很高效,所以果断引入(引入步骤搞得复杂吧,我还重写了监听。一切为了稳定,也好扩展我某ii喜欢加 入动态切换数据源做准备。呵呵)。opensessionlnview,我想如果你不喜欢可以 摘掉,反正我很喜欢。struts2指定了自定义的struts.x

4、ml文件位置,指定扫描注解 的action路径。最后是proxool的叮视化图形监控,很棒。<?xml version二"1. 0 encoding二utf-8"?><webapp xmlns:xsi二http:/www. w3. org/2001/xmlschema-instance,z xmlns=/zhttp: /java. sun. com/xml/ns/javaee,zxml ns: web=/zht tp: / java. sun. com/xml/ns/ javaee/web-app_2_5. xsdxsi: schemcilocation二

5、http:/java. sun. com/xml/ns/javaee http:/java. sun. com/xml/ns/javacc/wcb-app_2_5. xsdid二webapp_id" version二2. 5"><display-name>framework</display-name><context-param><param-name>log4jconfi glocati on/param-name><param-value>/web-lnf/classes/resources/lo

6、g4j/log4j. propertie s</param-value></contextparam><co ntext-parcim><param-nanic>propcr tyf i lc</param-namc><param-value>/web-inf/classes/resources/hibernate/ perties</param-value></context-param><context-param><param-name>conte

7、xtconfiglocation</param-name><param-value>classpath:resources/spring/applicationcontext.xm 1</param-value></context-param><!一一 log4j listener 一一>listener<listener-class>org. springfrcimework. web. util. log4jconfigliste ncr</listcncr-class></listener>&

8、lt;!一一 proxool listener 一一><listener><listcncr-class>org. chinasb. framework, core. db. proxoollistener </listener-class></listener><!open session in view filter ><filter><filter-name>opensessioninviewfilter</filter-name>filterclass>org. springfr

9、cimework. orm. hibernate3. support. open scssionlnvicwf订tcr/f订ter-class<init-param><param-name>singlesession</param-name> <param-value>true</param-value></init-param></filter>f订tcr-mapping><filter-name>opensessionlnviewfilter</filter-name> url

10、-pattem>/*/url-pattem></filter-mapping><!一一 spring listener -一><listcncr><listener-class>org. springframework. web. context. contextloader listener</1istener-class></listener><!- character encoding f订ter -><filter><filter-name>set character

11、 encoding</filtername><f i11erc1ass>org. spri ngframework. web. f订ter. characterencodingf订ter< /fi1terclass>init-param> <param-name>encoding</param-name><param-value>utf-8</param-value></init-param><ini t-param><param-namc>forccencod ing

12、</param-namc><!-强制进行转码-> <param-value>true</param-value></init-param></filter><filter-mapping><filter-name>set character encoding</fi1tername> <url-pattern>/*</url-pattern></filter-mapping><!-延长action中属性的生命周期,-><filter&

13、gt;<filter-name>struts-cleanup</filter-name><filter-class>org. apache. struts2. dispatcher. actioncontextclea nup</f ilterclass></filter>f订tcr-mapping><filtcr-namc>struts-clcanup</filtcr-namc> <url-pattern>a</url-pattern></filter-mapping>

14、;<!- struts2 f订ter -><filter><filtcr一namc>struts2</filtcr-namc><filter-class>org. apache. struts2. dispatcher, ng. filter. struts? repareandexecutefi1ter</filter-class>ini t-param><param-namc>conf i g</paramnainc><param-value>struts-default. x

15、ml, struts-plugin, xml, resources/s truts/struts, xml</param-va1ue>/ini tparam>init-param><param-name>actionpackages</param-name><param-value>org. chinasb. framework. modules</param-value></init-param></filter>f订tcr-mapping><filter-name>struts

16、2</filter-name> <url-pattern>a</url-pattern></filter-mapping><!一一 proxool monitoring -一><scrvlct><servlet-name>admin</servlet-name><servlet-class>org. logicalcobwebs. proxool. admin. servlet. admin servlet</servlet-class</scrvlct><ser

17、vlet-mapping><servlet-name>admin</servlet-name><url-pattern>/admin. html</url-pattern></servlet-mapping><!一一 welcome list 一-><we1come-filelist><welcome-file>index. html</welcome-file><welcome-file>index. htm</welcome-f ile><welc

18、ome-file>index. jsp</welcome-file></welcome-f订e-list</webapp>3. applicationcontext.xml,我想下面注释得也比较清楚了,如果我写错了或理解错 了希望指止。<?xml version二"1. 0 encoding二"utf-8?><bcans xmlns二http:/www. springframcwork. org/schcma/beanszz xmlns:xsi二http:/www. w3. org/2001/xmlschema-inst

19、ance xmlns:context二http:/www. springframework. org/schema/context" xsi:schemalocation二http:/www. springframcwork. org/schcma/bcans http:/www. springframework, org/schema/beans/spring-beans, xsdhttp:/www. springframework. org/schemei/contexthttp:/www. springframework. org/schema/context/spring-c

20、ontext.xsd><!- 使用 armotation -> <context:armotation-config /><!- 使用 annotation 自动注册 bean,并检查©controller, ©service, ©repository注解已被注入-><conlext:componentscanbase-package二org. chinasb. framework, modules" /><!- hibernate 属性配置-><context:property-p

21、laceholder1 ocation=/,c 1 asspath:resources/hibernate/hibernate. proper!ies,z/><!- hibernate 配置管理-><import resource二appliedtioncontextpersistence. xml /></beans>4. hiberante配置所需的一些属性,指定方言,开始hibernate缓存等,后面还有一 个c3p0的数据连接池属性。你们下载的代码里面,数据源方面我换成了 c3p0,因 为proxool我配置的是随web启动的,而我又不想改成随

22、spring加载启动。所以我 开发中注释掉proxool,以后上线再打开。# hibernatehibernate. dialect二org. hibernate. dialect. mysqldialecthibernatc. show sql=truehibernate. format_sql=falsehibernate. hbm2ddl. auto二update# hibernate cachehibernate. cache, provider_class=org. hibernate, cache. ehcacheproviderhibcrrrntc. cache. usc_quc

23、ry_cache=falschibernate, cache. use_second_level_cache=true # c3p0 configurationhibernate. connection. driver_class=com. mysql. jdbc. driverhibernate.conncction. url二jdbc:mysql:/localhost:3306/chinasb?uscunic ode=true&characterencoding二utf-8hibernate, connection, username二roothibernate, connecti

24、on, password二root5. applicationcontext-persistence.xml,这里就是数据源及事务的一些配置了。事务 我使用了 cglib类级别的代理注解配置方式,如果你喜欢用jdk接口方式动态的代理, 可以去掉proxy-target-class=,true,'顺便也保留了声名式事务。<?xml version二1. 0 encoding二utf-8?><beans xmlns二http:/wvv. springframework. org/schema/beans xmlns:xsi二http:/www. w3. org/2001

25、/xmlschema-instanccz, xmlns:context二http:/www. springframework, org/schema/context" xmlns: aop=/zhttp: /www. springframework. org/schema/aop/z xmlns: tx二http:/www. springframework. org/schema/tx/z xsi: schemcilocation二http:/www. springframework. org/schema/beanshttp:/www. springframework. org/s

26、chema/beans/spring-beans. xsdhttp:/www. springframework. org/schcma/contexthttp:/www. springframework. org/schema/context/spring-context, xsdhttp:/www. springframework. org/schema/aophttp:/www. springframework. org/schema/aop/spring-aop. xsdhttp:/www. springframework. org/schema/txhttp:/www. springf

27、ramework. org/schema/tx/spring-tx. xsd><!- proxool 数据源-><!- <bean id二datasource"class=/zorg. springframework. jdbc. datasource. drivermanagerdatasource/z> <property name=/driverclassnanie/><valuc>org. logicalcobwcbs. proxool. proxooldrivcr</valuc> </proper

28、ty><property name二url><value>proxool. default</value> </property></bcan> > <! c3p0数据源 ><bean id二datasourcecl ass二com. mchan百e. v2. c3p0. combopool eddatasourcez,> <propcrty namc=,zdrivcrclassz,value=,$ hibernate. connection. driver_class,z/><p

29、roperty name二 jdbcurl" value二$ hibernate, connection, url,z/> <property name二properties><props><prop key二user>$hibernate, connection. username</prop><prop kcy=z,passwordz,>$ hibernate, conncction. password </prop> </props></property></bean&g

30、t; <!- scssionfactory -><bean id二sessionfactory"class=/zorg. springframework. orm. hibernate3. annotation. annotationsessio nfactorybean/z><property name二datasource" ref二datasource" /> <propcrty name二packagcstoscan"value=/zorg. chinasb. framework, modules. *

31、model"/<property name=z/hibernateproperties/z><props><propkey二hibernate. dialcctz,>$ hibernate, dialect </prop><propkey二hibernate. show_sql$hibernate. show_sql</prop><propkey二hibernate. format_sql z,>$ hibernate. format_sql </prop><propkey二hiberna

32、te. hbm2ddl. auto,z>$ hibernate. hbm2ddl. auto </prop><propkey二hibernate, cache. provider_classz,>$ hibernate, cache, provider class </prop><propkey二hibernate, cache. usc_qucry_cachc>$hibcrrmte. cache. usc_qucry_cac he</prop><propkey二hibernate, cache. use_second_l

33、evel_cachez/>$ hibernate, cache. use_se cond_level_cache)</prop></props></property>/bean> <!配置事务管理-<bcan id二sactionmanagerclass=/zorg. springframework. orm. hibernate3. hibernatetransactionmanager ><property name二sessionfactory" ref二sessionfactory/z /> <

34、;/bean><!- 配置注解实现管理事务(eglib: proxy-target-class二true") 一一><tx: annotation-driven transaction-manager=/ztransactionmanager/z proxy-target-class二true" /><!-指定使用eglib -><!一一 <aop:aspectj-autoproxy proxytarget-class二true" /> 一一> <!-配置事务的传播特性一一><!t

35、x:advicc id二xadvicc" transaction-manager=,/transac tionmanagerz/><tx:attributes><tx:method/>tx:mcthod/><tx:method propagation二required” /><tx:method/><tx:method propagation二required” />name二add* propagat i on二required namc=/zcdit* propagation二"required&q

36、uot; name= remove*name二save* propagation二required" name二update*"<tx:method name二delete*" propagation二required” /><tx:method name=z/batchupdate propagation二required” />tx:mcthod name二“* read-only二truc /> </tx:attributes></tx:advice>> <!-配置事务的切入点-><

37、;! <aop:config><aop:pointcut id二argetmethod express!on二executi on(*org. chinasb. framework, modules. service. *() /> <aop:advisor advice-ref二txadvice" pointcut-ref二targetmethoct /></aop:config>></bcans>6. struts.xml,你懂的。<?xml version二1. 0 encoding二utf-8?>&

38、lt;!doctype struts public-/apache software foundation/dtd struts configuration 2. o/enz, http:/stmts. apache, org/dtds/struts-2. 0. dtd"><struts><!-开启使用开发模式,详细错误提示-><constant rmme二struts. devmode value二false /><!-将对象交给spring管理-><constant name二struts. objectfactory,

39、z value二spring" /><!-指定资源编码类型-><constant name二struts, i18n. encoding" value二utf-8 /><!-指定每次请求到达,重新加载资源文件-><consteint name二stmts, i 18n. reload" value二false" /><!-指定每次配置文件更改后,自动重新加载-><constant name=,/struts. configuration, xml. reload'7 valu

40、e二false" /> <!-国际化资源文件-><constant name二struts, custom, i18n. resources"value二resources/contcnt/languogc" />!-默认后缀名-><constant name二struts, action, extension" value二do, action, jhtml, /> <!struts annotation ><constant name=,/actionpackagesz,value二o

41、rg. chinasb. framework, modules"/</struts>好了,下面我简单讲一下开发流程。在modules下建立模块,和相应的包(action,dao,model,service,util),比如我上面包 结构的demo模块。demo.java, model类,映射数据库屮的表,每个model 张表,为了适应basedao, 每个model还对应每个dao (不要觉得这是麻烦的)。jpa的注解,你们懂的,不 解释。package org. chinasb. framework, modules, demo, model;import java.

42、io. serializable;import java. util. datc;import javax.persistence. entity;import javax. persistence. generatedvalue; import javax. persistenee. generationtype;import javax. persistence. id;import org.hibernate, annotations. cache;import org. hibernate, armotations. cacheconcurrencystrategy;entity飢ac

43、he(usage = cacheconcurrencystrategy. nonstr1ct read write) public class demo implements serializable private static final long serialversionutd = il;id©generatedvalue (strategy = generationtype. auto) private int id;private string title;private string content;privatc date publishdate;public int

44、 getld() return id;public void setld(int id) this, id 二 id;public string gettitle()return title;public void settitle(string title) this.title 二 title;public string getcontent() :retufn content;public void setcontent (string content) this, content = content;public date gctpublishdatco return publishd

45、ate;public void setpublishdate(date publishdate) this.publishdatc 二 publishdatc;demodao,接口类。我想在多数程序员的项h里做的绝大多数事情都是相关数据库 的curd操作,所以基础框架里整合一个强大的basedao是必须的,而我选择了 开源的genericdao (觉得不适合你的可以替掉)。直接继承genericdao接口,指 定操作的model, -4主键的类型。package org. chinasb. framework, modules. demo, dao;import org. chinasb. f

46、ramework, core. base. dao. hibernate. genericdao; import org. chinasb. framework, modules, demo, model. demo;public interface demodao extends genericdao<demo, tnteger> demodaoimpi,呵,这样写我觉得不麻烦,不多说。repository注解交给spring 管理。package org. chinasb. framework, modules, demo. dao. impl;import org. chin

47、asb. framework, core. base. dao. basedao; import org.chinasb. framework, modules, demo. dao. demodao;import org. chinasb. framework, modules, demo, model. demo; import org. springframework. stereotype. repository;rcpositorypublic class demodaolmpl extends basedao<demo, lnteger> implements demo

48、dao basedao,主要是注入 sessionfactory 给 hibernate-genericdao。 package org.chinasb. framework, core. base, dao;import java. io.serializable;import org. chinasb. framework, core. base. dao. hibernate. genericdaotmpl; import org.hibernate. scssionfactory;import org. springframework. beans, factory, annotati

49、on. autowired;public class basedacxt, id extends serializable> extends genericdaotmpl<t, td> autowired©overridepublic void setsessionfactory(sessionfactory sessionfactory) super, setsessionfactory(sessionfactory);ijdemoservice,接下来就要定义一些相关数据库操作的业务接口给上层使用了,这 里主要列出了 baseda。能操作的部分,像分页,字段过滤

50、查询方面的没冇列出。 package org. chinasb. framework, modules. demo, service;import java.util. list;import org. chinasb. framework, modules, demo, model. demo;public interface demoservice /*增加或更新demo* ©param demo* return*/public boolean save (demo demo);/*批量增加或更新demo* ©param demo* return*/public boo

51、lean save(demo demos);/*删除demo* ©param demo* ©return*/public boolean remove (demo demo);/*批量删除demo* param demos*/public void remove(demo demos);/*根据主键删除demo* ©param id* ©return*/public boolean removebytd(tnteger id);/*批量根据主键删除demo* ©param ids*/public void rcmovcbylds(intcgcr

52、 ids);/*查询demo数据记录集* rcturn*/public list<demo> findall();/*根据主键查询demo* param id* rcturn*/public demo findbyld(integer id);/*批量根据主键杳询demo记录集* ©param ids* ©return*/public dcmo findbylds(integer ids);/*持久化session数据到数据库*/public void flush();demoservicelmpi,接口实现。这里全用注解控制了事务,及演示如何使用basedao

53、 当然强人的basedao并不仅仅这些方法,还有分页等强大的杳询数据功能。人家可 以在 http:/code.qooqle.eom/p/hibernateqenericdao/进行查看。package org. chinasb. framework, modules. demo, service, impl;import java.util. list;import javax. annotation. resource;import org. chinasb. framework, modules, demo. dao. demodao;import org. chinasb. framew

54、ork, modules, demo, model. demo;import org. chinasb. framework, modules, demo, service. demoservice;import org springframework. stereotype. service;import org.springframework. transact!on. emnotation. propagation; import org. springframework. transaction. armotation. transeictiorial;©service

55、69;transactional (readonly 二 true, propagation 二 propagation .supports) public class demoservicetmpl implements demoservice ©resourceprivate demodao demodao;©overridetransactional (readonly = false, propagation 二 propagation. required)public boolean save (demo demo) return demodao. save (d

56、emo);©override©transactional (readonly 二 false, propagation 二 propagation. required)public boolcan save (demo demos) return demodao. save(demos);©overridetransactional(rcadonly 二 false, propagation 二 propagation. required)public boolean remove(demo demo) return demodao. remove(demo);&

57、#169;override©transactional(readonly 二 false, propagation 二 propagation. required)public void remove(demo demos) demodao. remove (demos);ij©override©transactional(readonly 二 false, propagation 二 propagation. required)public boolean rcmovcbyid(integer id) return demodao. removebyld (id

58、);©overridetransactional (rcadonly 二 false,propagation 二 propagation. required)public void removebylds(integer ids) demodao. removebylds (ids);©overridepublic list<demo> findall () return demodao. findall ();©overridepublic demo findbytd(tnteger id) return demodao- find (id);

59、69;overridepublic demo findbytds(tnteger ids) return demodao- find (ids);©overridepub11c void flush() demodao. flush ();demoaction,这里演示了查看,增,删功能。也是基于注解的方式,继承了 baseaction。 package org. chinasb. framework, modules, demo, action;import java. util. date;import java.util. list;import javax. annotati

60、on. resource;import org. apache. struts2. convcntion. armotation. action;import org. apache. struts2. convention, annotation. result;import org. chinasb. framework, core. base, action. baseaction;import org chinasb. framework, modules, demo, model. demo;import org. chinasb. framework, modules. demo,

61、 service. demoservice;import org. springframework. stereotype. controllcr;©controllerpublic class demoaction extends baseaction private static final long scrialvcrsionuid 二 il;©resourceprivate demoservice demoservice;action(value 二 ”/dcmoactiof: results 二 ©rcsult(name 二 success, location 二"/manager/niodules/denio/index. jsp)©overridepublic string execute() list<demo> demolist = demoservice. findall (); httpservletrequest. se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论