Spring整合Ibatis

[*]所需jar清单
ibatis-2.*.jar *为任意版本,下同,ibatis工作包
spring.jar
spring-ibatis.jar spring集成ibatis插件包
commons-dbcp.jar dbcp,pool为数据库连接池组件包
commons-pool-1.5.2.jar
commons-logging.jar
ojdbc14.jar
[*]目录结构图
[img]http://dl.iteye.com/upload/attachment/552153/62ab31a6-e2c0-3c98-a0da-5e5e1f28879e.jpg[/img]
[*]配置
ibatis配置sql-map-config.xml
<?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>

<settings errorTracingEnabled="true"
useStatementNamespaces="false"
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
/>
<sqlMap resource="com/pb/pojo/Student.xml" />
</sqlMapConfig>

spring配置applicationContext.xml
<?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="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>
<!-- 结束 -->


<!-- 加载属性文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>
config/database.properties
</value>
</list>
</property>
</bean>
<!--结束 -->

<!-- ibatis配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="config/sql-map-config.xml">
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 结束 -->

<!-- DAO配置 -->
<bean id="StudentDao" class="com.pb.dao.StudentDao">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<!-- 结束 -->
</beans>

database.properties配置
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=test
jdbc.password=tiger
dbcp.initialSize=5
dbcp.maxActive =100
dbcp.maxIdle =5
dbcp.maxWait =50
dbcp.poolPreparedStatements =false
dbcp.defaultAutoCommit =false

Student.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 namespace="stu">
<resultMap class="com.pb.pojo.Student" id="Student">
<result property="sid" column="sid" javaType="java.lang.Integer" />
<result property="sname" column="sname" />
<result property="age" column="age" javaType="java.lang.Integer" />
<result property="tel" column="tel" />
<result property="address" column="address" />
<result property="cid" column="cid" javaType="java.lang.Integer" />
</resultMap>
<select id="findAllStudent" resultMap="Student">
select * from student
</select>
</sqlMap>

StudentDao.java
package com.pb.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.pb.pojo.Student;

@SuppressWarnings("unchecked")
public class StudentDao extends SqlMapClientDaoSupport {

public List<Student> findAllStudent() throws SQLException {
List<Student> result = getSqlMapClient().queryForList("findAllStudent");
System.out.println(result);
return result;
}
}

[*]测试
	public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/applicationContext.xml");
StudentDao studentDao = (StudentDao) applicationContext.getBean("StudentDao");
studentDao.findAllStudent();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值