下面这篇文章介绍的是struts+spring+ibatis示例,笔者将从他们的配置直到部署都尽可能的介绍清楚:
1 首先创建数据库(以oracle为例),数据库脚本如下:
CREATETABLEEMPLOYEE(EMPLOYEEIDINTEGERNOTNULLPRIMARYKEY,FIRSTNAMEVARCHAR(256),LASTNAMEVARCHAR(256),AGEINTEGER,DEPARTMENTIDINTEGER)
CREATETABLEDEPARTMENT(DEPARTMENTIDINTEGER,NAMEVARCHAR(256))
INSERTINTOEMPLOYEEVALUES(1,'John','Doe',36,100);
INSERTINTOEMPLOYEEVALUES(2,'Bob','Smith',25,300);
INSERTINTOEMPLOYEEVALUES(3,'Carrie','Heffernan',32,200);
INSERTINTOEMPLOYEEVALUES(4,'Doug','Heffernan',34,100);
INSERTINTOEMPLOYEEVALUES(5,'Arthur','Spooner',64,300);
INSERTINTODEPARTMENTVALUES(100,'Accounting');
INSERTINTODEPARTMENTVALUES(200,'R&D');
INSERTINTODEPARTMENTVALUES(300,'Sales');
接下来要为EMPLOYEE表的主键建立自动增长,方法参见前面的一篇文章,再此不在重复了,如果不建自动增长的话会报OAR-00001错误。
2 为项目添加上述三种框架,建议用myeclipse,这样就可以直接加载了,关于ibatis的下载参考下面的联接: http://ibatis.apache.org/ 程序里的jar包如下:
3 建立POJO类:
packagecss.web.demo.model;
importjava.io.Serializable;
publicclassEmployeeimplementsSerializable...{
/***//**
*
*/
privatestaticfinallongserialVersionUID=4704328446890394252L;
privateIntegeremployeeId;
privateIntegerage;
privateStringfirstName;
privateStringlastName;
privateIntegerdepartmentId;
publicEmployee()...{
}
publicEmployee(IntegeremployeeId,StringfirstName,StringlastName,Integerage,IntegerdepartmentId)...{
this.employeeId=employeeId;
this.firstName=firstName;
this.lastName=lastName;
this.age=age;
this.departmentId=departmentId;
}
publicIntegergetDepartmentId()...{
returndepartmentId;
}
publicvoidsetDepartmentId(IntegerdepartmentId)...{
this.departmentId=departmentId;
}
publicIntegergetEmployeeId()...{
returnemployeeId;
}
publicvoidsetEmployeeId(IntegeremployeeId)...{
this.employeeId=employeeId;
}
publicIntegergetAge()...{
returnage;
}
publicvoidsetAge(Integerage)...{
this.age=age;
}
publicStringgetFirstName()...{
returnfirstName;
}
publicvoidsetFirstName(StringfirstName)...{
this.firstName=firstName;
}
publicStringgetLastName()...{
returnlastName;
}
publicvoidsetLastName(StringlastName)...{
this.lastName=lastName;
}
}
packagecss.web.demo.model;
importjava.io.Serializable;
publicclassDepartmentimplementsSerializable...{
/***//**
*
*/
privatestaticfinallongserialVersionUID=9077129830166112946L;
IntegerdepartmentId;
Stringname;
publicDepartment()...{
}
publicDepartment(IntegerdepartmentId,Stringname)...{
this.departmentId=departmentId;
this.name=name;
}
publicIntegergetDepartmentId()...{
returndepartmentId;
}
publicvoidsetDepartmentId(IntegerdepartmentId)...{
this.departmentId=departmentId;
}
publicStringgetName()...{
returnname;
}
publicvoidsetName(Stringname)...{
this.name=name;
}
}
4 DAO层的实现:
packagecss.web.demo.persistence;
importjava.util.List;
publicinterfaceDepartmentDao...{
publicListgetAllDepartments();
}
packagecss.web.demo.persistence;
importorg.springframework.orm.ibatis.SqlMapClientTemplate;
importjava.util.List;
publicclassDepartmentIbatisDaoextendsSqlMapClientTemplateimplementsDepartmentDao...{
publicListgetAllDepartments()...{
returnqueryForList("Department.getAll",null);
}
}
packagecss.web.demo.persistence;
importjava.util.List;
importcss.web.demo.model.Employee;
publicinterfaceEmployeeDao...{
publicListgetAllEmployees();
publicEmployeegetEmployee(Integerid);
publicintupdate(Employeeemp);
publicIntegerinsert(Employeeemp);
publicintdelete(Integerid);
}
packagecss.web.demo.persistence;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.springframework.orm.ibatis.SqlMapClientTemplate;
importcss.web.demo.model.Employee;
importjava.util.List;
publicclassEmployeeIbatisDaoextendsSqlMapClientTemplateimplementsEmployeeDao...{
Loglogger=LogFactory.getLog(this.getClass());
publicListgetAllEmployees()...{
returnqueryForList("Employee.getAll",null);
}
publicEmployeegetEmployee(Integerid)...{
return((Employee)queryForObject("Employee.getById",id));
}
publicintupdate(Employeeemp)...{
returnupdate("Employee.update",emp);
}
publicIntegerinsert(Employeeemp)...{
return(Integer)insert("Employee.insert",emp);
}
publicintdelete(Integerid)...{
returndelete("Employee.delete",id);
}
}
5sqlConfig文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsqlMapConfig
PUBLIC"-//iBATIS.com//DTDSQLMapConfig2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!--thereareplentyofotheroptionalsettings,seetheibatis-sql-mapsdoc-->
<settings
enhancementEnabled="true"
useStatementNamespaces="true"
/>
<sqlMapresource="css/web/demo/persistence/Employee.xml"/>
<sqlMapresource="css/web/demo/persistence/Department.xml"/>
</sqlMapConfig>
6 service层的实现(见源代码):
7 spring配置文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location"value="classpath:spring.properties"/>
</bean>
<beanname="/employeeSetUp"class="css.web.demo.action.EmployeeAction">
<constructor-argindex="0"ref="employeeService"/>
<constructor-argindex="1"ref="departmentService"/>
</bean>
<beanname="/employeeProcess"class="css.web.demo.action.EmployeeAction">
<constructor-argindex="0"ref="employeeService"/>
<constructor-argindex="1"ref="departmentService"/>
</bean>
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
</bean>
<beanid="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<propertyname="configLocation">
<value>classpath:css/web/demo/persistence/SqlMapConfig.xml</value>
</property>
<propertyname="useTransactionAwareDataSource">
<value>true</value>
</property>
<propertyname="dataSource">
<refbean="dataSource"/>
</property>
</bean>
<beanid="sqlMapClientTemplate"
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<propertyname="sqlMapClient">
<refbean="sqlMapClient"/>
</property>
</bean>
<beanid="employeeDao"class="css.web.demo.persistence.EmployeeIbatisDao">
<propertyname="sqlMapClient">
<refbean="sqlMapClient"/>
</property>
</bean>
<beanid="employeeService"class="css.web.demo.service.EmployeeDaoService">
<constructor-argindex="0"ref="employeeDao"/>
</bean>
<beanid="departmentDao"class="css.web.demo.persistence.DepartmentIbatisDao">
<propertyname="sqlMapClient">
<refbean="sqlMapClient"/>
</property>
</bean>
<beanid="departmentService"class="css.web.demo.service.DepartmentDaoService">
<constructor-argindex="0"ref="departmentDao"/>
</bean>
</beans>
8 页面及ACTION的实现(见源代码):
9 总结
上面的步骤全是我经验之谈,但我从小语文没学好,所以不知道该怎么表述才能让大家明白,不过有兴趣的朋友,可以到我的资源去下载原代码学习,如果有什么好的建议的话还请告诉我,呵呵,只要按照上面的步骤,在对照源代码就一定能得到你想要的结果。希望我们一起进步!
分享到:
相关推荐
一个简单的struts+spring+ibatis示例的源码,在源码中包含一个简单的分布功能
struts+spring+ibatis的Demo struts+spring+ibatis的Demo struts+spring+ibatis的Demo
struts2+spring+Ibatis框架包
是将struts2+spring3+iBATIS进行整合。
struts + spring + ibatis 示例
有关Struts2+Spring+Hibernate和Struts2+Spring+Ibatis的整合实例demo 原创,完全基于eclipse开发
struts+spring+ibatis整合示例,包内有sql及jar包,可直接运行。
struts+spring+ibatis框架集成.pdf
struts2+spring+ibatis+mysql AOP日志管理,异常捕获 tomcat6.0+jdk1.6
struts+spring+ibatis框架,自己配的,和大家分享下,数据库是sqlserver2008,下载导入eclipse中可以运行
Struts+Spring+Ibatis整合框架搭建配置文档
开发环境说明 ...本示例完整地结合Struts2+Spring2+iBatis2+MySQL5,演示了一个用户表的增、删、改、查。 想完整学习Struts2+Spring+iBatis的同仁,可以在这个例子中学习或模仿最基本也是最核心的技术要点。
Struts+Spring+Ibatis环境配置(一) - zwjxf的专栏 - 博
里面有spring+struts2+ibatis整合的jar包,
struts+spring+ibatis源代码
struts2 + spring + ibatis 整合例子,仓库:search.maven.org
struts2+spring+ibatis的小demo
简单公文管理 struts+spring+ibatis +ajax 分页
struts+spring+ibatisstruts+spring+ibatisstruts+spring+ibatisstruts+spring+ibatisstruts+spring+ibatisstruts+spring+ibatisstruts+spring+ibatisstruts+spring+ibatisstruts+spring+ibatis
Struts2+Spring+iBatis整合的一个小例子。 包括了对MySQL数据库的增删改查,只适用于新手,使用时请先把ssi.sql文件导入数据库。 压缩包内有源码和所需jar包