版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
電力系統(tǒng)總結(jié)筆記
項(xiàng)目第一天(項(xiàng)目框架)
1:項(xiàng)目介紹
參考:《項(xiàng)目分析筆記》和【文檔】中的《國家電力系統(tǒng)技術(shù)點(diǎn)匯總(簡歷使用).doc》
2:項(xiàng)目框架(中創(chuàng)建ElecText.java
publicclassElecTextimplementsjava.io.Serializable{
privateStringtextID;//主鍵工D
privateStringtextName;//測(cè)拭名稱
privateDatetextDate;//測(cè)試日期
privateStringtextRemark;//;則試備,*
publicStringgetTextID(){
returntextID;
}
publicvoidsetTextID(StringtextID){
this.textID=textID;
}
publicStringgetTextName(){
returntextName;
}
publicvoidsetTextName(StringtextName){
this.textName=textName;
}
publicDategetTextDate(){
returntextDate;
}
publicvoidsetTextDate(DatetextDate){
this.textDate=textDate;
}
publicStringgetTextRemark(){
returntextRemark;
}
publicvoidsetTextRemark(StringtextRemark){
this.textRemark=textRemark;
(2)在cn.itcast.elec.domain目錄下,創(chuàng)建ElecText.java對(duì)應(yīng)的映射文件(ElecText.hbm.xml)
<?xmlversion=111.0"encoding=nUTF-8,r?>
<!DOCTYPEhibernate-mappingPUBLIC
itit)
<hibernate-mapping>
<classname="cn.itcast.elec.domain.ElecText"table="Elec_Textn>
<idname=ntextID"type="string"column="textIDn>
(generatorclass=,,uuid,,></generator>
</id>
<propertyname=MtextName'*type="string"column="textNamewx/property>
<propertyname="textDate"type="date"column=,rtextDateffx/property>
<propertyname="textRemark"type="string"
column=ntextFeznark°></property>
</class>
</hibernate-mapping>
(3)在src下創(chuàng)建hibernate.cfg.xml的配置文件
<?xmlversion="1.0"encoding=nUTF-8n?>
<!DOCTYPEhibernate-configurationPUBLIC
??_??
“B>
<hibernate-configuration>
<session-factory>
<1—連接數(shù)據(jù)庫的信息--〉
<property
name="hibernate.connection.driver_class,F>com.mysql.jdbc.Driver</property>
<propertyname="hibernate.connection.urlw>jdbc:mysql,F>root</property>
<property
name="hibernate.connection.pa">org.hibernate.dialect.MySQL5Dialect</properLy
>
<prope.rtyname="hibernate.hbm2ddl.autor,>update</property>
〈propertyname="hibernate.show_sqlrf>true</property>
<!—加載映射文件-->
〈mappingresource="cn/itcan/>
</session-factory>
</hibernate-configuration>
(4)使用junit在test包中進(jìn)行測(cè)試(并且導(dǎo)入log4j的配置文件)
publicclassTe;//加載類路徑hibernate.cfg.xml和映射文件
SessionFactorysf=configuration.buildSessionFactory();
Sessions=sf.openSession();
Transactiontr=s.beginTransaction();
//測(cè)試操作對(duì)象的過程,就是操作數(shù)據(jù)庫表
ElecTextelecText=newElecText();
elecText.setTextName("測(cè)試Hibernate名稱");
elecText.setTextDate(newDate());
elecText.setTextRemark("測(cè)試Hibernate備注”);
s?save(elecText);
mit();
s.close();
}
第四步:DAO層
(1)在cn.itcast.elec.dao中創(chuàng)建2個(gè)接口(公用接口和業(yè)務(wù)接口)
?公用接口:
publicinterfaceICommonDao<T>{
voidsave(Tentity);
)
?業(yè)務(wù)接口(需要繼承公共接口,并且指定泛型T所對(duì)應(yīng)的對(duì)象:
pxiblicinterfacelElecTextDaoextendsICommonDao<ElecText>{
publicstaticfinalStringSERVICE_NAME=
ncn,itcast.elec.dao.impl.ElecTextDaoImplH;
)
(2)在cn.itcast.elec.dao.impl中創(chuàng)建2個(gè)接口的實(shí)現(xiàn)類
?公用類(需要繼承HibemaleDaoSupport,這樣可以方便使用HibernateTemplate對(duì)象):
publicclassCommonDaoImpl<T>extendsHibernateDaoSupportimplements
ICommonDao<T>{
/**使用?Resource注入SessionFactory*/
^Resource(name=HsessionFactory,1)
publicfinalvoidsetSessionFactoryDi(SessionFactorysessionFactory){
this.setSessionFactory(sessionFactory);
)
/**保存*/
publicvoidsave(Tentity){
this.getHibernateTemplate().save(entity);
)
?業(yè)務(wù)類(需要繼承公用類,這樣可以使用公用類中的定義的方法)
@Repository(lElecTextDao.SERVICE_NAME)
publicclassElecTextDaoImplextendsCommonDaoImpl<ElecText>implements
lElecTextDao{
}
(3)在src創(chuàng)建spring的配置文件(beans.xml)
<?xmlversion=.0"encoding="UTF-8n?>
<beansxmlns=""
xmlns:context=""
xmlns:aop="”
xmln”
xmlns:xsi="”
xsi:schemaLocation="
<!一1:開啟對(duì)注解的支持,組件的自動(dòng)掃描,掃描在類上定義的?Controller?Service
?Repositiry注解,范圍是cn.itcast.elec的包一〉
<context:component-scanbase-package="cn.itcast.elec”/>
<!-2:?先不寫,大家想想項(xiàng)目中應(yīng)該添加什么?
<!--3:倉ij建SessionFactory,這是spring整合hibernat一的核心-->
<beanid=r,sessionFactory"
class=norg.springframework.orm.hibernate3.LocalSessionFactoryBeann>
<!一力【1載類路徑下的hibernate.cfg.xml—>
<propertyname="configLocation”>
<va1ue>cla</value>
</property>
</bean>
<!—4:聲明事務(wù)管理器(切面)-->
<beanid="trManager"
class="org.springframework..orm.hibernate3.HibernateTransactionManager">
<propertyname=nsessionFactory"ref=nsessionFactory"></proper\..y>
</bean>
<!--5:注解,在Service的類或者是Service的方法上,添加@Transcational()注解,用來控
制事務(wù)--〉
<tx:annotation-driventransaction-manager="trManagern/>
<!-也可以使用spring的配置文件的方式管理事務(wù)
<tx:adviceid=naantransaction-manager=ntrManager',>
<tx:attributes〉
<tx:methodname=Hsave*11isolation=,*DEFAULTMpropagation="REQUIRED*'
read-only=nfalse"/>
<tx:methodname=uupdate***isolation=nDEFAULTM
propagation^REQUIRED”read-only=nfalseH/>
<tx:methodname=ndelete*”isolation=MDEFAULT"
propagation=nREQUIREDHread-only=nfalsen/>
<tx:methodname=n*"read-only=Mtruen/>
</tx:attributes〉
</tx:advice>
<aop:config>
<aonid=Hbbn/>
<aop:advisoradvice-ref=naanpointcut-ref=nbbn/>
</aop:config>——>
</beans>
(4)使用junit完成測(cè)試
publicclassTestDao{
/**測(cè)試保存*/
QTest
publicvoidsave(){
〃加載類路徑下的spring容器
Applicationcontextac=newCla");
//調(diào)用接口
lElecTextDaoelecTextDao=(lElecTextDao)
ac,getBean(lElecTextDao.SERVICE_NAME);
//操作對(duì)象
ElecTextelecText=newElecText();
elecText.setTextName("測(cè)試Dao名稱”);
elecText.setTextDate(newDate());
elecText.setTextRemark("測(cè)試Da。備注”);
elecTextDao.save(elecText);
}
)
注意:如果數(shù)據(jù)沒有保存,需要設(shè)置事務(wù)自動(dòng)提交:在hibernate.cfg.xml中添加:
<!--使用hibernate的方式提交事務(wù)(自動(dòng)提交)-->
〈propu二一了name="hibernate.connection.autocommit,r>true</pro:j'&rty>
第五步:Service層
(1)在cn.itcast.elec.service中創(chuàng)建接口:
publicinterfacelElecTextService{
publicstaticfinalStringSERVICE_NAME=
ncn.itcast.elec.service.impl.ElecTextServicelmpl”;
voidsaveElecText(ElecTextelecText);
)
(2)Scn.itcast.elec.service.impl中創(chuàng)建接口的實(shí)現(xiàn)類:
eService(lElecTextService.SERVICE_NAME)
^Transactional(readOnly=true)
publicclassElecTextServicelmplimplementsIElecTextService{
gResource(name=IElecTextDao.SERVICE_NAME)
privatelElecTextDaoelecTextDao;
^Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIR
ED,readOnly=false)
publicvoidsaveElecText(ElecTextelecText){
elecTextDao.save(elecText);
)
)
(3)使用junit測(cè)試
publicclassTen);
IElecTextServiceelecTextService=(IElecTextService)
ac.getBean(lElecTextService.SERVICE_NAME);
//操作對(duì)象
ElecTextelecText=newElecText();
elecText.setTextName("測(cè)試Service名稱");
elecText.setTextDate(newDate());
elecText.setTextRemark("測(cè)試Service備注”);
elecTextService.saveElecText(elecText);
}
)
由于spring提供的聲明式事務(wù)處理,進(jìn)行事務(wù)的控制,在業(yè)務(wù)層的類和方法上定義
@Transactional(),同時(shí)去掉hibernate.cfg.xml中的配置,由spring統(tǒng)一控制。去掉的代
碼是:
<!--使用hibernate的方式提交事務(wù)(自動(dòng)提交)-->
<propertyname="hibernate.connection.autocommitr,>true</property>
第六步:控制層(MVC)
(1)在cn.itcast.elec.web.action中創(chuàng)建Action(業(yè)務(wù)Action)類和BaseAction(公用Action)
?Action類:(注意:這里要設(shè)置成多例,即@5(:(中?(value=prototype),因?yàn)閟truts2的
Action是多實(shí)例,多線程)
^Controller("elecTextAction'?)
@Scope(value="prototype")
publicclassElecTextActionextendsBaseAction<ElecText>{
ElecTextelecText=this.getModel();
^Resource(name=IElecTextService.SERVICE_NAME)
privateIElecTextServiceelecTextService;
/**執(zhí)行保存*/
publicStringsave(){
〃保存
elecTextService.saveElecText(elecText);
return"save”;
}
)
BaseAction類(封裝模型驅(qū)動(dòng)對(duì)象,;
try(
entity=(T)entityclass.ne—Tns七;
}catch(Exceptione){
e.prin=req;
}
publicvoidsetServletResponse(=res;
}
}
(2)在cn.itcast.elec.util包下創(chuàng)建公用類(泛型轉(zhuǎn)換)。
泛型轉(zhuǎn)換的目的子類傳遞真實(shí)對(duì)象類型,在父類中使用泛型轉(zhuǎn)換成真實(shí)對(duì)象類型。
以后util包下封裝的就是公用類。
publicclassTUtils{
/**泛型轉(zhuǎn)換成真實(shí)類型(范類轉(zhuǎn)換)*/
publicstaticClassgetTClass(Classentity){
Parameter!zedTypeparameter!zedType=(ParameterizedType)
entity.getGenericSuperclass();
Classentityclass=(Class)
parameterizedType.getActualTypeArguments()[0];
returnentityclass;
(3)在src下創(chuàng)建struts2的配置文件struts.xml
<?xmlversion=nl.0"encoding="UTF-8,r?>
<!DOCTYPEstrutsPUBLIC
”-//ApacheSon
???〉
<struts>
<!—設(shè)置開發(fā)模式
<constantname="struts.devMode"value=Mtruewx/constant>
<!—設(shè)置U工標(biāo)簽的主題—>
<constantname="struts.ui.theme"value=nsin?p2enx/constant>
<!一配置struts的URL訪問的后綴,改成do(此處可以不修改,此時(shí)默認(rèn)是.action)―>
<constantname="struts.action.extension“value="do,rx/constant>
<!—系統(tǒng)管理
〈packagename="system"namespace="/system"extends="struts-default">
<actionname="elecTextAction_^nclass="elecTextAction"method="{1}n>
<resu.lname=,rsave,r>/system/textAdd.jsp</resul
</action>
〈/package〉
〈中添加配置:
<web-appversion="2.5"
xmlns=n"
xmlns:xsi=""
xsi:schemaLocation="
n>
<display-name></display-name>
<!一配置struts2的過濾器,這是struts2運(yùn)行的核心一>
<filter>
<:i1:.er-name>struts2</fi].r.ername〉
<rilter-:zlass>org.apache.struts2.dispatcher.ng.filter.二巳〉
</context-param>
<listener>
<1is7.i-?r.er-cH>org.springframework.web.context.ContextLoaderListener</2i
stener-class>
</listener>
<welcome-file-list>
<welcomefi'e>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(5)導(dǎo)入css,script,jsp,images進(jìn)行測(cè)試
使用system/textAdd.jsp進(jìn)行測(cè)試:頁面如圖
測(cè)試專用jsp
測(cè)試名稱:
測(cè)試日期:
測(cè)試備注:
[保存]
點(diǎn)擊【保存】完成。測(cè)試保存ElecText對(duì)象。
r
/**使用?Resource注入SessionFactory*/
@Resource(name=,,sessionFactoryn)
publicfinalvoidsetSessionFactoryDi(SessionFactorysessionFactory){
this.setSessionFactory(sessionFactory);
)
/★★保存*/
publicvoidsave(Tentity){
this.getHibernateTemplate().save(entity);
}
/**更新*/
publicvoidupdate(Tentity){
this.getHibernateTemplate().update(entity);
}
/*★使用主鍵工D,查詢對(duì)象★/
publicTfindObjectBylD(Serializableid){
return(T)-his.getHibernateTemplate().get(entityclass,id);
)
/**使用主鍵ID,刪除對(duì)象(刪除一個(gè)或者多個(gè)對(duì)象)*/
publicvoiddeleteObjectBylDs(Serializable...ids){
if(ids!=null&&ids.length>0){
for(Serializableid:ids){
Objectentity=this.findObjectBylD(id);
this.getHibernateTemplate().delete(entity);
)
)
}
/**使用封裝對(duì)象的集合,批量刪除對(duì)象*/
publicvoiddeleteObjectByCollection(List<T>list){
this.getHibernateTemplate().deleteAll(list);
}
/**測(cè)試指定查詢條件,查詢結(jié)果集(不分頁)*/
/**
這里工=1的目的是方便在Service層拼裝sql或者h(yuǎn)ql語句,連接統(tǒng)一使用and
*SELECToFROMElecTextoWHEREl=xxx#Dao層填寫
ANDo.textNameLIKE'%張%'#Service拼裝
ANDo.textRemarkLIKE1?#Service拼裝
ORDERBYo.textDateASC,o.textNamedesc#Service拼裝
*/
publicList<T>findCollectionByConditionNoPage(Stringcondition,
finalObject[]params,Map<StringzString>orderby){
Stringhql="SELECToFROMH+entityClass.getSimpleName()+noWHERE1=1n;
StringorderByHql=this.initOrderByHql(orderby);
finalStringfinalHql=hql+condition+orderByHql;
//執(zhí)行hq工語句
/**方式一:直接使用HibernateTemplate的find()方法,find方法支持執(zhí)彳了hql語句*/
//List<T>list=this.getHibernateTemplate().find(finalHql,params);
/**方式二:獲取SessionFactory,在獲取Session*/
//SessionFactorysf=this.getHibernateTemplate().getSessionFactory();
//Sessions=sf.getCurrentSession();
//Queryquery=s.createQuery(finalHql);
//query.setParameter(0,params[0]);
//query.setParameter(lzparams[1]);
//List<T>list=query.list();
/**方式三:使用hibernateTemplate調(diào)用回調(diào)函數(shù)★/
List<T>list=this.getHibernateTemplate().execute(new
HibernateCallback(){
publicObjectdoInHibernate(Sessionsession)
throwsHibernateException,SQLException{
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 植物的生長和繁殖
- 礦區(qū)燒變巖含水層保護(hù)技術(shù)規(guī)-編制說明
- 新入員工安全培訓(xùn)試題及答案(各地真題)
- 廠里安全培訓(xùn)試題及參考答案(研優(yōu)卷)
- 公司、項(xiàng)目部、各個(gè)班組安全培訓(xùn)試題(打?。?/a>
- 班組安全培訓(xùn)試題含答案【典型題】
- 公司主要負(fù)責(zé)人安全培訓(xùn)試題加下載答案
- 公司廠級(jí)員工安全培訓(xùn)試題含解析答案
- 期中測(cè)試(試題)-2024-2025學(xué)年四年級(jí)上冊(cè)數(shù)學(xué)人教版
- (期中復(fù)習(xí))五年級(jí)上冊(cè)字音高頻考點(diǎn)專項(xiàng)訓(xùn)練-2024-2025學(xué)年 語文統(tǒng)編版
- 2024巴黎奧運(yùn)會(huì)秋季開學(xué)第一課主題班會(huì)
- 黃腐酸知識(shí)完整版本
- 外研版(三起)(2024)三年級(jí)上冊(cè)英語Unit 1 Let's be friends 教學(xué)設(shè)計(jì)
- 2023年山東省煙臺(tái)市萊山區(qū)初家街道社區(qū)工作者招聘筆試題庫及答案解析
- 溢流堰面施工專項(xiàng)方案培訓(xùn)資料
- 創(chuàng)業(yè)修煉智慧樹知到期末考試答案章節(jié)答案2024年同濟(jì)大學(xué)
- GB/T 44133-2024智能電化學(xué)儲(chǔ)能電站技術(shù)導(dǎo)則
- 上海2024年上海市金融穩(wěn)定發(fā)展研究中心招聘筆試歷年典型考題及考點(diǎn)附答案解析
- 外墻保溫及裝飾工程投標(biāo)書模板
- 園林技術(shù)專業(yè)自評(píng)總結(jié)報(bào)告
- 行政執(zhí)法考試試卷及參考答案
評(píng)論
0/150
提交評(píng)論