上图是大致目录结构和所需jar包,下面贴代码
实体类:SampleBean
package fdf.bean;
public class SampleBean {
private long id;
private String name;
private byte[] blobSample;//接收blob类型字段
private String clobSample;//接收clob类型字段
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getBlobSample() {
return blobSample;
}
public void setBlobSample(byte[] blobSample) {
this.blobSample = blobSample;
}
public String getClobSample() {
return clobSample;
}
public void setClobSample(String clobSample) {
this.clobSample = clobSample;
}
}
ibatis配置文件
1、实体类映射sql的配置文件Sample.xml
<?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>
<typeAlias alias="Sample" type="fdf.bean.SampleBean"/>
<resultMap class="Sample" id="resultSample">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="blobSample" column="blobSample" jdbcType="BLOB" typeHandler="com.ibatis.sqlmap.engine.type.BlobTypeHandlerCallback" />
<result property="clobSample" column="clobSample" jdbcType="CLOB" javaType = "java.lang.String"/>
</resultMap>
<select id="findSampleList" parameterClass="Sample" resultClass="Sample" resultMap="resultSample">
select * from ibatis_spring_util
</select>
</sqlMap>
2、加载映射配置的文件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>
<sqlMap resource="fdf/config/ibatis/Sample.xml"/>
</sqlMapConfig>
连接数据库配置文件(以本机oracle为例)jdbc.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
username=****
password=*****
Spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="fdf"></context:component-scan>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:fdf/config/oracle/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!--initialSize: 初始化连接-->
<property name="initialSize" value="2"/>
<!--maxIdle: 最大空闲连接-->
<property name="maxIdle" value="2"/>
<!--minIdle: 最小空闲连接-->
<property name="minIdle" value="2"/>
<!--maxActive: 最大连接数量-->
<property name="maxActive" value="5"/>
<!--removeAbandoned: 是否自动回收超时连接-->
<property name="removeAbandoned" value="true"/>
<!--removeAbandonedTimeout: 超时时间(以秒数为单位)-->
<property name="removeAbandonedTimeout" value="180"/>
<!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒-->
<property name="maxWait" value="3000"/>
<property name="poolPreparedStatements" value="false"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!-- 配置数据源 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:fdf/config/ibatis/SqlMapConfig.xml</value>
</property>
</bean>
</beans>
操作接口:SampleDao
package fdf.dao;
import java.util.List;
import fdf.bean.SampleBean;
public interface SampleDao {
public List<SampleBean> findSampleList(SampleBean sampleBean);
}
ibatis基础操作接口BaseDao
package fdf.dao;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import com.ibatis.sqlmap.client.SqlMapClient;
public class BaseDao<T> extends SqlMapClientTemplate{
@Resource(name = "sqlMapClient")
private SqlMapClient sqlMapClient;
@PostConstruct
public void initSqlMapClient(){
super.setSqlMapClient(sqlMapClient);
}
@SuppressWarnings("unchecked")
public List<Object> queryListNoPage(String sqlId,Object obj){
List<Object> list = null;
try {
list = (List<Object>) super.getSqlMapClient().queryForList(sqlId, obj);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
接口实现类SampleDaoImpl
package fdf.dao.impl;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import fdf.bean.SampleBean;
import fdf.dao.BaseDao;
import fdf.dao.SampleDao;
@Transactional
@Service @Scope("prototype")
public class SampleDaoImpl extends BaseDao<SampleBean> implements SampleDao{
@SuppressWarnings("unchecked")
@Override
public List<SampleBean> findSampleList(SampleBean sampleBean) {
// TODO Auto-generated method stub
return (List)queryListNoPage("findSampleList",sampleBean);
}
}
main方法入口SampleStart
package fdf.init;
import java.util.List;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import fdf.bean.SampleBean;
import fdf.dao.SampleDao;
public class SampleStart {
public static void sampleExcute(SampleBean sampleBean){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("fdf/config/spring/applicationContext.xml");
SampleDao sampleDao=(SampleDao)ctx.getBean("sampleDaoImpl");
List<SampleBean> list=sampleDao.findSampleList(sampleBean);
for(int i=0;i<list.size();i++){
System.out.println("id======="+list.get(i).getId());
System.out.println("ClobSample======="+list.get(i).getClobSample());
}
}
public static void main(String[] args) {
//初始化log4j
PropertyConfigurator.configure("log4j.properties");
SampleBean sampleBean=new SampleBean();
SampleStart.sampleExcute(sampleBean);
}
}
本文是自己的慢慢积累,希望与大家共同进步,本文地址 http://blog.csdn.net/wind_fdf/article/details/12889875,转载请说明,谢谢!!!