struts+spring+ibatis示例(附:源代码)

本文提供了一个Struts+Spring+Ibatis整合的详细示例,包括数据库搭建、项目配置、POJO定义、DAO层实现、Service层实现、Spring配置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面这篇文章介绍的是struts+spring+ibatis示例,笔者将从他们的配置直到部署都尽可能的介绍清楚:

1 首先创建数据库(以oracle为例),数据库脚本如下:

CREATE TABLE EMPLOYEE(EMPLOYEEID INTEGER NOT NULL PRIMARY KEY ,FIRSTNAME VARCHAR ( 256 ),LASTNAME VARCHAR ( 256 ),AGE INTEGER ,DEPARTMENTID INTEGER )
CREATE TABLE DEPARTMENT(DEPARTMENTID INTEGER ,NAME VARCHAR ( 256 ))
INSERT INTO EMPLOYEE VALUES ( 1 , ' John ' , ' Doe ' , 36 , 100 );
INSERT INTO EMPLOYEE VALUES ( 2 , ' Bob ' , ' Smith ' , 25 , 300 );
INSERT INTO EMPLOYEE VALUES ( 3 , ' Carrie ' , ' Heffernan ' , 32 , 200 );
INSERT INTO EMPLOYEE VALUES ( 4 , ' Doug ' , ' Heffernan ' , 34 , 100 );
INSERT INTO EMPLOYEE VALUES ( 5 , ' Arthur ' , ' Spooner ' , 64 , 300 );
INSERT INTO DEPARTMENT VALUES ( 100 , ' Accounting ' );
INSERT INTO DEPARTMENT VALUES ( 200 , ' R&D ' );
INSERT INTO DEPARTMENT VALUES ( 300 , ' Sales ' );

接下来要为EMPLOYEE表的主键建立自动增长,方法参见前面的一篇文章,再此不在重复了,如果不建自动增长的话会报OAR-00001错误。

2 为项目添加上述三种框架,建议用myeclipse,这样就可以直接加载了,关于ibatis的下载参考下面的联接: http://ibatis.apache.org/ 程序里的jar包如下:

3 建立POJO类:

package css.web.demo.model;

import java.io.Serializable;

public class Employee implements Serializable ... {
/***//**
*
*/

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;
}


}

package css.web.demo.model;

import java.io.Serializable;

public class Department implements Serializable ... {
/***//**
*
*/

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层的实现:

package css.web.demo.persistence;

import java.util.List;

public interface DepartmentDao ... {
publicListgetAllDepartments();
}

package css.web.demo.persistence;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import java.util.List;

public class DepartmentIbatisDao extends SqlMapClientTemplate implements DepartmentDao ... {
publicListgetAllDepartments()...{
returnqueryForList("Department.getAll",null);
}

}

package css.web.demo.persistence;

import java.util.List;

import css.web.demo.model.Employee;

public interface EmployeeDao ... {

publicListgetAllEmployees();

publicEmployeegetEmployee(Integerid);

publicintupdate(Employeeemp);

publicIntegerinsert(Employeeemp);

publicintdelete(Integerid);
}

package css.web.demo.persistence;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import css.web.demo.model.Employee;

import java.util.List;

public class EmployeeIbatisDao extends SqlMapClientTemplate implements EmployeeDao ... {
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"
/>
< sqlMap resource ="css/web/demo/persistence/Employee.xml" />
< sqlMap resource ="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 >

< bean id ="propertyConfigurer" class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name ="location" value ="classpath:spring.properties" />
</ bean >

< bean name ="/employeeSetUp" class ="css.web.demo.action.EmployeeAction" >
< constructor-arg index ="0" ref ="employeeService" />
< constructor-arg index ="1" ref ="departmentService" />
</ bean >

< bean name ="/employeeProcess" class ="css.web.demo.action.EmployeeAction" >
< constructor-arg index ="0" ref ="employeeService" />
< constructor-arg index ="1" ref ="departmentService" />
</ bean >

< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" >
< property name ="driverClassName" value ="${jdbc.driverClassName}" />
< property name ="url" value ="${jdbc.url}" />
< property name ="username" value ="${jdbc.username}" />
< property name ="password" value ="${jdbc.password}" />
</ bean >

< bean id ="sqlMapClient"
class
="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
< property name ="configLocation" >
< value > classpath:css/web/demo/persistence/SqlMapConfig.xml </ value >
</ property >
< property name ="useTransactionAwareDataSource" >
< value > true </ value >
</ property >
< property name ="dataSource" >
< ref bean ="dataSource" />
</ property >
</ bean >

< bean id ="sqlMapClientTemplate"
class
="org.springframework.orm.ibatis.SqlMapClientTemplate" >
< property name ="sqlMapClient" >
< ref bean ="sqlMapClient" />
</ property >
</ bean >

< bean id ="employeeDao" class ="css.web.demo.persistence.EmployeeIbatisDao" >
< property name ="sqlMapClient" >
< ref bean ="sqlMapClient" />
</ property >
</ bean >

< bean id ="employeeService" class ="css.web.demo.service.EmployeeDaoService" >
< constructor-arg index ="0" ref ="employeeDao" />
</ bean >

< bean id ="departmentDao" class ="css.web.demo.persistence.DepartmentIbatisDao" >
< property name ="sqlMapClient" >
< ref bean ="sqlMapClient" />
</ property >
</ bean >

< bean id ="departmentService" class ="css.web.demo.service.DepartmentDaoService" >
< constructor-arg index ="0" ref ="departmentDao" />
</ bean >



</ beans >

8 页面及ACTION的实现(见源代码):

9 总结

上面的步骤全是我经验之谈,但我从小语文没学好,所以不知道该怎么表述才能让大家明白,不过有兴趣的朋友,可以到我的资源去下载原代码学习,如果有什么好的建议的话还请告诉我,呵呵,只要按照上面的步骤,在对照源代码就一定能得到你想要的结果。希望我们一起进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值