下面这篇文章介绍的是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
...
{ /** */ /** * */ private static final long serialVersionUID = 4704328446890394252L ; private Integer employeeId; private Integer age; private String firstName; private String lastName; private Integer departmentId; public Employee() ... { } public Employee(Integer employeeId, String firstName, String lastName, Integer age, Integer departmentId) ... { this .employeeId = employeeId; this .firstName = firstName; this .lastName = lastName; this .age = age; this .departmentId = departmentId; } public Integer getDepartmentId() ... { return departmentId; } public void setDepartmentId(Integer departmentId) ... { this .departmentId = departmentId; } public Integer getEmployeeId() ... { return employeeId; } public void setEmployeeId(Integer employeeId) ... { this .employeeId = employeeId; } public Integer getAge() ... { return age; } public void setAge(Integer age) ... { this .age = age; } public String getFirstName() ... { return firstName; } public void setFirstName(String firstName) ... { this .firstName = firstName; } public String getLastName() ... { return lastName; } public void setLastName(String lastName) ... { this .lastName = lastName; } }
package
css.web.demo.model;
import
java.io.Serializable;
public
class
Department
implements
Serializable
...
{ /** */ /** * */ private static final long serialVersionUID = 9077129830166112946L ; Integer departmentId; String name; public Department() ... { } public Department(Integer departmentId, String name) ... { this .departmentId = departmentId; this .name = name; } public Integer getDepartmentId() ... { return departmentId; } public void setDepartmentId(Integer departmentId) ... { this .departmentId = departmentId; } public String getName() ... { return name; } public void setName(String name) ... { this .name = name; } }
4 DAO层的实现:
package
css.web.demo.persistence;
import
java.util.List;
public
interface
DepartmentDao
...
{ public List getAllDepartments(); }
package
css.web.demo.persistence;
import
org.springframework.orm.ibatis.SqlMapClientTemplate;
import
java.util.List;
public
class
DepartmentIbatisDao
extends
SqlMapClientTemplate
implements
DepartmentDao
...
{ public List getAllDepartments() ... { return queryForList( " Department.getAll " , null ); } }
package
css.web.demo.persistence;
import
java.util.List;
import
css.web.demo.model.Employee;
public
interface
EmployeeDao
...
{ public List getAllEmployees(); public Employee getEmployee(Integer id); public int update(Employee emp); public Integer insert(Employee emp); public int delete(Integer id); }
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
...
{ Log logger = LogFactory.getLog( this .getClass()); public List getAllEmployees() ... { return queryForList( " Employee.getAll " , null ); } public Employee getEmployee(Integer id) ... { return ((Employee)queryForObject( " Employee.getById " , id)); } public int update(Employee emp) ... { return update( " Employee.update " , emp); } public Integer insert(Employee emp) ... { return (Integer)insert( " Employee.insert " , emp); } public int delete(Integer id) ... { return delete( " Employee.delete " , id); } }
5 sqlConfig文件:
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"
>
<
sqlMapConfig
>
<!--
there are plenty of other optional settings, see the ibatis-sql-maps doc
-->
<
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配置文件:
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//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 总结
上面的步骤全是我经验之谈,但我从小语文没学好,所以不知道该怎么表述才能让大家明白,不过有兴趣的朋友,可以到我的资源去下载原代码学习,如果有什么好的建议的话还请告诉我,呵呵,只要按照上面的步骤,在对照源代码就一定能得到你想要的结果。希望我们一起进步!