Spring整合Ibatis

javaibatisspring

  • 所需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的更多相关文章

  1. Spring整合Ibatis之SqlMapClientDaoSupport

    前言 HibernateDaoSupport   SqlMapClientDaoSupport . 其实就作用而言两者是一样的,都是为提供DAO支持,为访问数据库提供支持. 只不过HibernateD ...

  2. Spring 整合 ibatis

    是的,真的是那个不好用的ibatis,不是好用的mybatis. 由于工作需要用到ibatis需要自己搭建环境,遇到了不少的坑,做一下记录. 一.环境配置 Maven JDK1.6 (非常重要,使用S ...

  3. Spring2.5整合Ibatis入门级开发实例

      分类: SPRING IBATIS2010-11-22 20:19 1170人阅读 评论(0) 收藏 举报 ibatisspringstringpropertiesclassuser 最近一直在看 ...

  4. spring+struts2+ibatis 框架整合以及解析

    一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...

  5. spring+springmvc+ibatis整合注解方式实例【转】

    源自-----> http://shaohan126448.iteye.com/blog/2033563 (1)web.xml文件(Tomcat使用) 服务器根据配置内容初始化spring框架, ...

  6. spring+springmvc+ibatis整合注解方式实例

    需求说明 实现用户通过数据库验证登录需求.採用 Myeclipse+Tomcat 6.0+Mysql 5.0+JDK 1.6 2.数据库表 开发所用是Mysql数据库,仅仅建立单张用户表T_USER, ...

  7. Intellij IDEA +MAVEN+Jetty实现Spring整合Mybatis

    1 pom.xml(这里出现transaction错误,是版本的问题) <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  8. Spring整合MyBatis

    前言:MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的XML或注解用 ...

  9. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

随机推荐

  1. windows phone 8 开发系列(一)环境搭建

    一:前奏说明 本人一名普通的neter,对新玩意有点小兴趣,之前wp7出来的时候,折腾学习过点wp7开发,后来也没怎么用到(主要对微软抛弃wp7的行为比较不爽),现在wp8已经出来一段时间了,市场上也 ...

  2. wordpress使用video.js与七牛云存储实现无广告视频分享应用

    video.js是一款极受欢迎的基于HTML5的开源WEB视频播放器,其充分利用了HTML5的视频支持特性,可以实现全平台的无视频插件播放功能,对于现在流行的手机.PAD等移动智能终端有极佳的应用体验 ...

  3. c++11: bind用法

    原型: template< class R, class F, class... Args > bind( F&& f, Args&&... args ); ...

  4. Silverlight浮动窗体 floatablewindow 非模态对话框

    1.http://www.cnblogs.com/yinxiangpei/articles/2613913.html 说明:Silverlight的ChildWindow组件给我们的开发带来了便利,比 ...

  5. Win7下MongoDB安装

    一.下载MongoDB 下载地址:http://www.mongodb.org/downloads 注意:1.从2.2开始,MongoDB不再支持windows xp. 2.32位MongoDB最大支 ...

  6. C#中gridView常用属性和技巧介绍

    .隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; .得到当前选定记录某字段的值 sValue=Table.Rows[gridV ...

  7. 表达式语言之java对正则表达式的处理

    正则表达式用于字符串匹配,字符串查找,字符串替换等.例如注册email格式的验证等.java中处理正则表达式相关的类主要有java.lang.String,java.util.regex.Patter ...

  8. Arrays.asList的使用及异常问题

    将数组转成List问题,通常我们习惯这样写成:List<String> list = Arrays.asList("1","2"); 于是我们这样就 ...

  9. telnet 时代的 bbs

    人类曾经用telnet 来访问 bbs,后来有了www,web 浏览器取代了telnet Telnet协议是TCP/IP协议族中的一员 arp和ping的区别 ping也属于一个通信协议,是TCP/I ...

  10. springboot pom 引用集合

    <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</arti ...