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
- 配置
ibatis配置sql-map-config.xml
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代码
<?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配置
Java代码
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代码
<?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
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;
}
}
- 测试
Java代码
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/applicationContext.xml");
StudentDao studentDao = (StudentDao) applicationContext.getBean("StudentDao");
studentDao.findAllStudent();
}
Spring整合Ibatis的更多相关文章
- Spring整合Ibatis之SqlMapClientDaoSupport
前言 HibernateDaoSupport SqlMapClientDaoSupport . 其实就作用而言两者是一样的,都是为提供DAO支持,为访问数据库提供支持. 只不过HibernateD ...
- Spring 整合 ibatis
是的,真的是那个不好用的ibatis,不是好用的mybatis. 由于工作需要用到ibatis需要自己搭建环境,遇到了不少的坑,做一下记录. 一.环境配置 Maven JDK1.6 (非常重要,使用S ...
- Spring2.5整合Ibatis入门级开发实例
分类: SPRING IBATIS2010-11-22 20:19 1170人阅读 评论(0) 收藏 举报 ibatisspringstringpropertiesclassuser 最近一直在看 ...
- spring+struts2+ibatis 框架整合以及解析
一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...
- spring+springmvc+ibatis整合注解方式实例【转】
源自-----> http://shaohan126448.iteye.com/blog/2033563 (1)web.xml文件(Tomcat使用) 服务器根据配置内容初始化spring框架, ...
- spring+springmvc+ibatis整合注解方式实例
需求说明 实现用户通过数据库验证登录需求.採用 Myeclipse+Tomcat 6.0+Mysql 5.0+JDK 1.6 2.数据库表 开发所用是Mysql数据库,仅仅建立单张用户表T_USER, ...
- Intellij IDEA +MAVEN+Jetty实现Spring整合Mybatis
1 pom.xml(这里出现transaction错误,是版本的问题) <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- Spring整合MyBatis
前言:MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的XML或注解用 ...
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
随机推荐
- eclipse 最全快捷键(网络收集)
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...
- 配置drbd高可用集群
前期准备: 同步时间 (两个节点) 节点一(172.16.21.6) [root@stu21 heartbeat2]# ntpdate 172.16.0.1 31 Dec 20:59:25 ntpda ...
- AJAX项目中的一些总结
最近在做AJAX数据处理交互,遇到一些问题,备份以免以后忘记 1.参数地址问题 确保参数地址的正确性 ; 阅读后台源码,参数和返回值要确定: 一般的大点公司应该有规范: 2.关于ajax事件触发请求多 ...
- WinForm TreeView节点重绘,失去焦点的高亮显示
当用户焦点离开TreeView时,TreeView选中节点仍然高亮,但是颜色符合主题. 设置TreeView.HideSelection = False;可让选中节点保持高亮. 添加重绘事件 Tree ...
- java.lang.NoClassDefFoundError Adding a jar to an RCP application
给RCP中加入jar包与一般的java工程是有些个区别的,否则会出现"java.lang.NoClassDefFoundError" Open plug-in.xmlGo to R ...
- JDK 动态代理实现原理
一.引言 Java动态代理机制的出现,使得Java开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象便能动态生成代理类.代理类会负责将所有方法的调用分派到委托对象上反射执行,在分派执行的过 ...
- 11g RAC R2 之Linux DNS 配置
在动手配置前,最好先了解下DNS的理论,以免犯不必要的错误.这都是被坑后的觉悟 -_-!!! Oracle 11g RAC 集群中引入了SCAN(SingleClientAccessName)的概念, ...
- WPF 系统托盘 图标闪烁
WPF消息通知 系统托盘,图标闪烁 using System.Windows.Forms; using System.Windows.Threading; public partial class W ...
- flex 监听网络连接情况
NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChange); private ...
- atomic_read
static inline int atomic_read(const atomic_t *v) { return (*(volatile int *)&(v)->counter); } ...