- 浏览: 13307 次
- 性别:
- 来自: 上海
文章分类
最新评论
集成Spring IBatis 的整个过程如下:
- 引入所有需要的*.jar文件。
- 配置web.xml。
- 配置log4j。
- 配置C3P0数据库连接池。
- 配置Spring以及与IBatis集成。
- 建立数据库表结构及其domain类和配置
- 编写dao类。
- 编写单元测试类。
一、所需类库 | Required jar list
c3p0-0.9.1.2.jar // 连接池实现类库
commons-logging-1.0.4.jar
ibatis-2.3.4.726.jar
log4j-1.2.15.jar
ojdbc14-10g.jar // Oracle JDBC驱动类库
spring-2.5.6.jar
spring-test.jar // 单元测试需要用到的类库
junit-4.4.jar // 单元测试需要用到的类库
二、配置web.xml | How to configure web.xml
1、在web.xml中增加如下内容:
<!-- 载入Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:ApplicationContext.xml
</param-value>
</context-param>
<!-- Spring 监听器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
三、配置log4j | How to configure log4j
1、在src/main/resouces 目录下建立 log4j.xml,其内容如下:
<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="sis_log_file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="log/sis.log" />
<param name="DatePattern" value=".yyyyMMddHH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %p %t %C{3} %m%n" />
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="sis_log_file" />
</root>
</log4j:configuration>
四、配置C3P0数据库连接池 | How to configure c3p0 jdbc connection pool
1、在src/main/resouces 目录下建立 jdbc.properties,其内容如下:
#c3p0 数据源配置;
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.0.8:1521:orcl10
jdbc.username=aofeng
jdbc.password=aofeng
# 连接池完成初始化后建立的连接数量
initialPoolSize=0
# 连接池的最小连接数量
minPoolSize=2
# 连接池的最大连接数量
maxPoolSize=10
# 连接的最大空闲时间(单位:秒),当连接空闲超时此时间后,连接将被回收
maxIdleTime=1000
idleConnectionTestPeriod=1000
2、然后在Spring的配置ApplicationContext.xml中添加如下配置:
<!-- 载入资源文件,下面的dataSource就引用了资源文件中的配置项 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxIdleTime" value="${maxIdleTime}"/>
<property name="maxPoolSize" value="${maxPoolSize}"/>
<property name="minPoolSize" value="${minPoolSize}"/>
<property name="initialPoolSize" value="${initialPoolSize}"/>
<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>
</bean>
五、配置Spring与IBatis集成 | How to configure spring integrates with ibatis
1、在src/main/resouces 目录下建立IBatis的SQL映射配置文件SqlMapConfig.xml,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" useStatementNamespaces="true"/>
<!-- 此处开始添加SqlMap配置文件 -->
</sqlMapConfig>
2、在Spring的配置ApplicationContext.xml中添加如下配置:
<!-- IBatis ORM 操作类 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>
<!-- Spring的IBatis模板 -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<!-- 事务管理配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
六、建立数据表结构及Domain类 | Create table and its corresponding domain class
1、测试Spring与IBatis集成的表结构如下:
create table USER
(
USER_ID NUMBER(10) not null,
USER_TYPE NUMBER(2),
USER_STATUS NUMBER(1) not null,
USER_NAME VARCHAR2(11) not null,
USER_PASSWD VARCHAR2(20) not null,
CREATE_TIME DATE not null,
UPDATE_TIME DATE not null,
LAST_LOGIN_TIME DATE
);
alter table USER
add constraint PK_USER primary key (USER_ID)
using index;
2、建立User表对应的domain类:User.java,其内容如下:
/**
* 建立时间:2011-3-19
*/
package cn.aofeng.sis.domain;
import java.util.Date;
/**
* 表USER对应的持久层POJO.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
public class User {
/**
* This field corresponds to the database column USER.USER_ID
*/
private Long userId;
/**
* This field corresponds to the database column USER.USER_TYPE
*/
private Short userType;
/**
* This field corresponds to the database column USER.USER_STATUS
*/
private Short userStatus;
/**
* This field corresponds to the database column USER.USER_NAME
*/
private String userName;
/**
* This field corresponds to the database column USER.USER_PASSWD
*/
private String userPasswd;
/**
* This field corresponds to the database column USER.CREATE_TIME
*/
private Date createTime;
/**
* This field corresponds to the database column USER.UPDATE_TIME
*/
private Date updateTime;
/**
* This field corresponds to the database column USER.LAST_LOGIN_TIME
*/
private Date lastLoginTime;
/**
* This method returns the value of the database column USER.USER_ID
*
* @return the value of USER.USER_ID
*/
public Long getUserId() {
return userId;
}
/**
* This method sets the value of the database column USER.USER_ID
*
* @param userId the value for USER.USER_ID
*/
public void setUserId(Long userId) {
this.userId = userId;
}
/**
* This method returns the value of the database column USER.USER_TYPE
*
* @return the value of USER.USER_TYPE
*/
public Short getUserType() {
return userType;
}
/**
* This method sets the value of the database column USER.USER_TYPE
*
* @param userType the value for USER.USER_TYPE
*/
public void setUserType(Short userType) {
this.userType = userType;
}
/**
* This method returns the value of the database column USER.USER_STATUS
*
* @return the value of USER.USER_STATUS
*/
public Short getUserStatus() {
return userStatus;
}
/**
* This method sets the value of the database column USER.USER_STATUS
*
* @param userStatus the value for USER.USER_STATUS
*/
public void setUserStatus(Short userStatus) {
this.userStatus = userStatus;
}
/**
* This method returns the value of the database column USER.USER_NAME
*
* @return the value of USER.USER_NAME
*/
public String getUserName() {
return userName;
}
/**
* This method sets the value of the database column USER.USER_NAME
*
* @param userName the value for USER.USER_NAME
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* This method returns the value of the database column USER.USER_PASSWD
*
* @return the value of USER.USER_PASSWD
*/
public String getUserPasswd() {
return userPasswd;
}
/**
* This method sets the value of the database column USER.USER_PASSWD
*
* @param userPasswd the value for USER.USER_PASSWD
*/
public void setUserPasswd(String userPasswd) {
this.userPasswd = userPasswd;
}
/**
* This method returns the value of the database column USER.CREATE_TIME
*
* @return the value of USER.CREATE_TIME
*/
public Date getCreateTime() {
return createTime;
}
/**
* This method sets the value of the database column USER.CREATE_TIME
*
* @param createTime the value for USER.CREATE_TIME
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
/**
* This method returns the value of the database column USER.UPDATE_TIME
*
* @return the value of USER.UPDATE_TIME
*/
public Date getUpdateTime() {
return updateTime;
}
/**
* This method sets the value of the database column USER.UPDATE_TIME
*
* @param updateTime the value for USER.UPDATE_TIME
*/
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
/**
* This method returns the value of the database column USER.LAST_LOGIN_TIME
*
* @return the value of USER.LAST_LOGIN_TIME
*/
public Date getLastLoginTime() {
return lastLoginTime;
}
/**
* This method sets the value of the database column USER.LAST_LOGIN_TIME
*
* @param lastLoginTime the value for USER.LAST_LOGIN_TIME
*/
public void setLastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
}
3、在与User.java的同一个package下建立User表的SqlMap映射配置文件:USER_SqlMap.xml。其内容如下:
4、在IBatis的配置文件SqlMapConfig.xml中加入USER_SqlMap.xml:
七、编写DAO类 | Write dao class
1、编写DAO接口类:UserDAO.java,其内容如下:
2、编写DAO实现类:UserDAOImpl.java,其内容如下:
八、编写JUnit单元测试 | Write junit testcase class
见下文Spring IBatis Struts2 集成之二
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="USER" >
<resultMap id="BaseResultMap" class="cn.aofeng.sis.domain.User" >
<result column="USER_ID" property="userId" jdbcType="DECIMAL" />
<result column="USER_TYPE" property="userType" jdbcType="DECIMAL" />
<result column="USER_STATUS" property="userStatus" jdbcType="DECIMAL" />
<result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
<result column="USER_PASSWD" property="userPasswd" jdbcType="VARCHAR" />
<result column="CREATE_TIME" property="createTime" jdbcType="DATE" />
<result column="UPDATE_TIME" property="updateTime" jdbcType="DATE" />
<result column="LAST_LOGIN_TIME" property="lastLoginTime" jdbcType="DATE" />
</resultMap>
<select id="selectByUserIdOrUserName" resultMap="BaseResultMap" parameterClass="cn.aofeng.sis.domain.User" >
select USER_ID, USER_TYPE, USER_STATUS, USER_NAME, USER_PASSWD, CREATE_TIME, UPDATE_TIME,
LAST_LOGIN_TIME
from TEST_PTL_USER
<dynamic prepend=" where" >
<isNotNull prepend="or" property="userId" >
USER_ID = #userId:DECIMAL#
</isNotNull>
<isNotNull prepend="or" property="userName" >
USER_NAME= #userName:VARCHAR#
</isNotNull>
</dynamic>
</select>
<delete id="deleteByUserId" parameterClass="Long" >
delete from TEST_PTL_USER
where USER_ID = #value#
</delete>
</sqlMap>
4、在IBatis的配置文件SqlMapConfig.xml中加入USER_SqlMap.xml:
<sqlMap resource ="cn/aofeng/sis/domain/USER_SqlMap.xml" />
七、编写DAO类 | Write dao class
1、编写DAO接口类:UserDAO.java,其内容如下:
/**
* 建立时间:2011-3-19
*/
package cn.aofeng.sis.dao;
import cn.aofeng.sis.domain.User;
/**
* 账号表User DAO接口定义.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
public interface UserDAO {
/**
* 根据账号ID删除账号.
*
* @param userId 账号ID.
* @return 返回1表示成功,返回0表示失败.
*/
int deleteByUserId(Long userId);
/**
* 根据账号ID查询账号信息.
*
* @param userId 账号ID.
* @return 如果查询成功返回一个@{link com.ailk.dm.odomain.testportal.domain.TestPtlUser}实例;查找不到返回null.
*/
User selectByUserId(Long userId);
/**
* 根据账号名称询账号信息.
*
* @param userName 账号名称.
* @return 如果查询成功返回一个@{link com.ailk.dm.odomain.testportal.domain.TestPtlUser}实例;查找不到返回null.
*/
User selectByUserName(String userName);
}
2、编写DAO实现类:UserDAOImpl.java,其内容如下:
/**
* 建立时间:2011-3-19
*/
package cn.aofeng.sis.dao;
import cn.aofeng.sis.domain.User;
import javax.annotation.Resource;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* 账号表User DAO IBatis实现.
*
* @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a>
*/
@Repository("userDAO")
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class UserDAOImpl implements UserDAO {
@Resource(name="sqlMapClientTemplate")
private SqlMapClientTemplate _sqlMapClientTemplate;
protected SqlMapClientTemplate getSqlMapClientTemplate() {
return _sqlMapClientTemplate;
}
protected String getNamespace() {
return "USER";
}
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public int deleteByUserId(Long userId) {
int result = getSqlMapClientTemplate().delete(getNamespace() + ".deleteByUserId", userId);
return result;
}
public User selectByUserId(Long userId) {
User param = new User();
param.setUserId(userId);
return (User) getSqlMapClientTemplate().queryForObject(getNamespace() + ".selectByUserIdOrUserName", param);
}
public User selectByUserName(String userName) {
User param = new User();
param.setUserName(userName);
return (User) getSqlMapClientTemplate().queryForObject(getNamespace() + ".selectByUserIdOrUserName", param);
}
}
相关推荐
Spring2.5.5 struts2.1.6 ibatis2.3.4 Mysql5.1 利用IntelliJ IDEA(9) 集成 所有JAR包已经包含
struts1.2 spring2 ibatis 集成项目实战源码
加快了开发速度,但是也有一些不足之处,比如由于三种框架的配置文件较多,也给我们带来了一些不便,特别是对于较小的应用来说更是如此,本文主要是对Strtus2、Spring、iBatis三个开源框架进行一个集成
ibatis 完美例子 一对多 批处理 事务 和 spring struts2集成 ,一朵多 插入1万条数据,不到2秒,备注不包含类库
SSI(Struts2 + Spring + IBatis)框架集成。
struts+spring+ibatis框架集成.pdf
此项目继承了Spring Struts2 Ibatis CXF,项目里面含有数据执行文件!具体请参见项目的readme.txt文件,里面详细描述需要对应框架支持需要的jar包。请注意,如果选取的版本不一致,所需要的jar可能不一样。
spring iBATIS struts1集成例子,详细源码
Struts1+Spring+iBatis集成-jar包,Struts1+Spring+iBatis集成
struts2 ibatis spring 集成开发
struts 2+spring +ibatis集成 ,
struts1+spring+hibernate+ibatis集成
使用Spring集成struts2、ibatis、poi实现的增删改查功能,包括采用jquery实现的无刷新查询机分页、dwr实现的两级联动、以及采用poi动态将数据库数据导出成excel,本demo采用mysql数据库,附有建表sql,项目导入...
前言:本讲是在【第四讲:WEB项目-集成Flex3+BlazeDS3.2+Spring2.5.6+iBatis2.3.4】的基础上添加iBatis,此次集成其实质是Spring与Struts2的整合而已 目标:WEB项目-集成Flex3+BlazeDS3.2+Spring2.5.6+iBatis2.3.4+...
下载即用 下载就能跑 简单的test 供初学者学习
*架构struts2_spring2.5_ibatis2.3 *mysql5.0 *jdk 1.6 *带有所有jar包,可直接运行 本实例实现了用户登陆,用户信息CRUD相关操作。让你感受到了ibatis做o/r mapping的方便快捷。 下次集成dwr进来 create ...
spring.struts,ibatis集成包,还有很多常用的包,这是我项目中用到的包,在这里备份一个,还有一部分在framework_lib2中
工具Myeclispe8.0 环境myelcipse tomcat6 所需jar包在.classpath文件中查看
struts2+ibatis+spring集成的简单例子,eclipse3.3直接导入既可,那些包太大,所以我没放进来..