经典的整合struts2+spring+ibitis实例文档格式.docx
- 文档编号:19299462
- 上传时间:2023-01-05
- 格式:DOCX
- 页数:9
- 大小:68.93KB
经典的整合struts2+spring+ibitis实例文档格式.docx
《经典的整合struts2+spring+ibitis实例文档格式.docx》由会员分享,可在线阅读,更多相关《经典的整合struts2+spring+ibitis实例文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
welcome-file>
index.jsp<
/welcome-file>
/welcome-file-list>
!
--加载spring文件-->
context-param>
param-name>
contextConfigLocation<
/param-name>
param-value>
/WEB-INF/applicationContext.xml<
/param-value>
/context-param>
listener>
listener-class>
org.springframework.web.context.ContextLoaderListener
/listener-class>
/listener>
--加载struts文件-->
filter>
filter-name>
struts2<
/filter-name>
filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
/filter-class>
/filter>
filter-mapping>
url-pattern>
/*<
/url-pattern>
/filter-mapping>
/web-app>
现在从数据持久化层开始做:
PO层(以Dep为例)
Dep.java
packageorg.po;
publicclassDep{
privateIntegerdid;
privateStringdname;
publicIntegergetDid(){
returndid;
}
publicvoidsetDid(Integerdid){
this.did=did;
publicStringgetDname(){
returndname;
publicvoidsetDname(Stringdname){
this.dname=dname;
dep.xml:
DOCTYPEsqlMap
PUBLIC"
-//ibatis.apache.org//DTDSQLMap2.0//EN"
"
//ibatis.apache.org/dtd/sql-map-2.dtd"
sqlMapnamespace="
dep"
typeAliasalias="
Dep"
type="
org.po.Dep"
/>
resultMapclass="
id="
resultDep"
resultproperty="
did"
column="
dname"
/resultMap>
selectid="
findAllDep"
resultMap="
[CDATA[SELECT*FROMdep]]>
/select>
findByIdDep"
parameterClass="
Integer"
resultClass="
[CDATA[SELECT*FROMdepWHEREdid=#did#]]>
insertid="
insertDep"
[CDATA[INSERTINTOdepVALUES(#did#,#dname#)]]>
/insert>
updateid="
updateDep"
[CDATA[UPDATEdepSETdname=#dname#WHEREdid=#did#]]>
/update>
deleteid="
deleteDep"
[CDATA[DELETEFROMdepWHEREdid=#did#]]>
/delete>
/sqlMap>
这个是ibatis的起始位置,所有映射文件都可以从这里找到。
config.xml:
DOCTYPEsqlMapConfig
-//ibatis.apache.org//DTDSQLMapConfig2.0//EN"
//ibatis.apache.org/dtd/sql-map-config-2.dtd"
sqlMapConfig>
sqlMapresource="
org/po/dep.xml"
org/po/emp.xml"
org/po/loves.xml"
org/po/emploves.xml"
/sqlMapConfig>
下来就写dao文件了:
(以DepDao.java为例)
packageorg.dao;
importjava.sql.SQLException;
importjava.util.List;
importcom.ibatis.sqlmap.client.SqlMapClient;
publicclassDepDaoimplementsIDao{
privateSqlMapClientclient;
publicbooleandelete(Integerid){
try{
client.delete("
id);
returntrue;
}catch(SQLExceptione){
e.printStackTrace();
returnfalse;
@SuppressWarnings("
unchecked"
)
publicList<
Object>
findAll(){
List<
list=null;
list=client.queryForList("
);
returnlist;
publicObjectfindById(Integerid){
DepDaodep=null;
dep=(DepDao)client.queryForObject("
returndep;
publicbooleansave(Objectobj){
DepDaodep=(DepDao)obj;
client.insert("
dep);
publicbooleanupdate(Objectobj){
client.update("
publicSqlMapClientgetClient(){
returnclient;
publicvoidsetClient(SqlMapClientclient){
this.client=client;
下来是要写appliactionContext.xml:
beansxmlns="
//www.springframework.org/schema/beans"
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-2.0.xsd"
--spring代理的数据源,与数据库连接从这里开始-->
beanid="
DataSource"
class="
org.springframework.jdbc.datasource.DriverManagerDataSource"
propertyname="
driverClassName"
value>
oracle.jdbc.driver.OracleDriver<
/value>
/property>
url"
jdbc:
oracle:
thin:
@192.1.1.40:
1521:
orcl<
username"
system<
password"
/bean>
--spring代理ibatis的工厂,创建sqlMapClient,在ibatis中很重要的对象,它是调用数据库操作方法的入口-->
sqlClient"
org.springframework.orm.ibatis.SqlMapClientFactoryBean"
dataSource"
reflocal="
configLocation"
classpath:
config.xml<
--Dao类-->
DepDao"
org.dao.DepDao"
client"
refbean="
/>
LovesDao"
org.dao.LovesDao"
EmpDao"
org.dao.EmpDao"
EmpLoves"
org.dao.EmpLoves"
--Face类-->
Face"
org.face.Face"
depDao"
lovesDao"
/beans>
用法和以前的差不多·
都是注入到spring容器中,不同的是struts的action不容在注入了。
下来是struts的action:
packageorg.action;
importorg.face.Face;
importorg.po.Dep;
importcom.opensymphony.xwork2.ActionSupport;
publicclassDepActionextendsActionSupport{
privateListdepList;
privateFaceface;
publicStringexecute(){
returnnull;
publicStringfindAll(){
depList=face.getDepDao().findAll();
return"
error"
;
publicFacegetFace(){
returnface;
publicvoidsetFace(Faceface){
this.face=face;
publicListgetDepList(){
returndepList;
publicvoidsetDepList(ListdepList){
this.depList=depList;
struts2.0没有了actionForm,将所有需要的值都放在了action中,只要赋值后,界面就可以直接用,方便。
。
但是看着挺乱的。
struts.properties:
struts.locale=zh_CN
struts.i18n.encoding=UTF-8
struts.objectFactory=spring
struts.xml
DOCTYPEstrutsPUBLIC
-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
//struts.apache.org/dtds/struts-2.0.dtd"
struts>
constantname="
struts.objectFactory.spring.autoWire"
value="
type"
<
struts.objectFactory"
spring"
includefile="
struts-default.xml"
packagename="
struts2"
extends="
struts-default"
default-interceptor-refname="
paramsPrepareParamsStack"
actionname="
save"
org.action.EmpAction"
method="
resultname="
ok"
redirect-action"
findAll<
/result>
/error.jsp<
/action>
delete"
edit"
/edit.jsp<
update"
findAll"
list"
/list.jsp<
/package>
/struts>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 经典 整合 struts2 spring ibitis 实例