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)二>,做一个完整的示例完成一个简 ...
随机推荐
- setuid函数解析
在讨论这个setuid函数之前,我们首先要了解的一个东西就是内核为每个进程维护的三个UID值.这三个UID分别是实际用户ID(real uid).有效用户ID(effective uid).保存的设置 ...
- jquery.fullCalendar官方文档翻译(一款小巧好用的日程管理日历, 可集成Google Calendar)
1. 使用方式, 引入相关js, css后, $(‘#div_name’).fullCalendar({//options}); 接受的是一个option对象 2. 普通属性 2.1. year, ...
- delphi下,不错的多语言翻译组件
http://yktoo.com/en/software/dklangTraned http://sourceforge.net/projects/dklang/
- 重绘panel控件,实现panel的阴影效果
最近想在项目中添加一个要有阴影的panel控件,找了好多资料,最后通过采用图片的方式实现了panel的阴影效果,效果图如下: 重绘代码如下: using System; using System.Co ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- Windows Server 2008 R2 64bit兼容Chrome浏览器
近日更换系统Windows Server 2008 R2 64bit系统,发现谷歌浏览器插件无法正常运行,终于找到如下解决方案: 打开桌面谷歌浏览器属性,将target目标 C:\Users\Admi ...
- OSGI容器与插件
插件必须符合osgi规范才能插到osgi容器中,osgi容器查看插件jar中MANIFEST.MF中osgi容器. 所谓插件----就是打包好的jar文件, 内部都封装好了一些功能
- ActiveMQ之selector的用法
前面的例子中创建一个消息消费者使用的是: sesssion.createConsumer(destination) 另外,还提供了另一种方式: sesssion.createConsumer(dest ...
- Oracle体系结构知识点的运用
体系结构方面的优化问题: 设数据库很大,访问量非常高,共享池很小:这样共享池里面就无法存储很多解析过得sql语句,导致很多硬解析,这样数据库就非常缓慢.这个时候要加大共享池.如果是自动管理,就加大SG ...
- ubuntu 12.04版本出现界面终端打开broken pipe,但是tty1这些可以。
sudo apt-get remove xserver-xorg sudo apt-get install xserver-xorg