注意事项:
以下内容基于“
”中最后形成的 sampleApp 项目。Spring 2.5.6 之前版本的整合方式与本篇基本相同,不再重复。
配置数据源
是时候改用 DB 存储 sampleApp 中的雇员(Employee)信息了。我们采用 Mysql,并假定你已安装它(截稿时最新版 MySQL Community Server 5.1.47)。
- 准备数据库
- 创建数据库 sample
- 创建表 employees
CREATE TABLE IF NOT EXISTS `employees` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`age` int(2) NOT NULL DEFAULT '0',
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
- 插入一些模拟数据
- 准备所需组件
- 使用单独文件存储 DB 驱动等信息
- 创建 resources 包
- 在包下创建 jdbc.properties 文件,输入你的 DB 信息
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://域名或IP:端口/sample
jdbc.username=用户名
jdbc.password=密码
- 修改 web-application-config.xml 文件
- 增加命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- 引入 jdbc.properties 文件
<context:property-placeholder location="classpath:resources/jdbc.properties"/>
- 配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<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>
- 向 EmployeeService 中注入 dataSource
<bean id="employeeServiceDest">
<flex:remoting-destination />
<property name="dataSource" ref="dataSource"/>
</bean>
- 修改 EmployeeService
- 运行 sampleApp
重构 server 端
你是否觉得目前的 server 端比较混乱?是的,我们需要重构它。
- 按以下原则重构 server 端
重构后结构图(代码请参照之后的附件):
- 重新配置 web-application-config.xml
将以下内容
<bean id="employeeServiceDest" class="com.sample.EmployeeService">
<flex:remoting-destination />
<property name="dataSource" ref="dataSource"/>
</bean>
更改为
<bean id="employeeServiceDest" class="com.sample.service.EmployeeServiceImpl">
<flex:remoting-destination />
<property name="employeeDao" ref="employeeDao"/>
</bean>
<bean id="employeeDao" class="com.sample.dao.EmployeeDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
- 运行 sampleApp
附件:
sampleApp-8-1.7z
整合 iBATIS 2.3
spring-framework 3.0 目前只支持 iBATIS 2.x,期望它尽快支持 iBATIS 的高版本 – MyBatis
- 准备所需组件
- 下载解压 iBATIS 2.3.4,将 ibatis-2.3.4.726.jar 拷贝到 lib 下
- 将以下 spring framework 的 jar 文件拷贝到 lib 下
org.springframework.jdbc-3.0.2.RELEASE.jar
org.springframework.orm-3.0.2.RELEASE.jar
org.springframework.transaction-3.0.2.RELEASE.jar
- 通过 Spring 管理 iBATIS
- 追加以下代码
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/sqlmap-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
- 修改
<bean id="employeeDao" class="com.sample.dao.EmployeeDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
为
<bean id="employeeDao" class="com.sample.dao.EmployeeDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
- 创建 iBATIS 映射文件
- 创建包 com.sample.dao.ibatis
- 创建映射文件 employees.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<select id="getEmployees" resultClass="com.sample.entity.Employee">
SELECT * FROM employees
</select>
</sqlMap>
- 创建 iBATIS 配置文件
在 WEB-INF 下创建 sqlmap-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="com/sample/dao/ibatis/employees.xml"/>
</sqlMapConfig>
- 修改 Dao 的实现类 EmployeeDaoImpl
package com.sample.dao;
import java.util.ArrayList;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.sample.entity.Employee;
public class EmployeeDaoImpl extends SqlMapClientDaoSupport
implements EmployeeDao {
public ArrayList<Employee> getList() {
return (ArrayList<Employee>)getSqlMapClientTemplate().
queryForList("getEmployees");
}
}
- 重构 Dao 和 Service 中 getList 方法的返回值类型
由 ArrayList<Employee> 改为 List<Employee>
- 重新运行 sampleApp
没问题吧
怎么样?现在整个架构感觉舒服多了吧 但是否感觉缺点什么?对,事务!我们下一篇搞定它。
分享到:
相关推荐
ibatis-2.3.4.726.jar,ibatis-2.3.0.677.jar,ibatis-2.3.3.720.jar下载,都已放入一个压缩包下。
ibatis-2.3.ibatis-2.3.ibatis-2.3
ibatis-2.3.3.720.jar
ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677
ibatis-2.3.4.726最新API chm格式 非常好用
ibatis-2.3.4.726-src 源代码,很难找的代码。用来调试和学习很方便。
ibatis-2.3.3.7.jar
ibatis-2.3.2.715源代码
ibatis-2.3.4.726官方最新开发包 和官方资料
ibatis-2.3.4.732.jar
对ibatis-2.3.4.726的改进 1. 物理分页功能,支持cache 2. 读写分离 3. 添加数据库方言,透明支持多种数据库
Source of ibatis-sqlmap version 2.3.4.726
ibatis2.3 源码
提供两个版本的ibatis jar包下载。
ibatis-2.3.0.677-sources.jar 值得学习的源码资源,不容错过。
ibatis-2.3.2.715.jar包
ibatis的jar包,用于和数据库交互
ibatis-2.3.4.726_api_doc.CHM ibatis-2.3.4.726_api_doc.CHM