ibatis(2)ibatis是什么
2016-06-17 14:25
288 查看
【0】README
1)本文部分内容转自 “ibatis in action”,旨在 review “ibatis是什么” 的相关知识;
2)intro to ibatis: ibatis 就是数据映射器,Martin Fowler在《企业应用架构模式》中,对 data mapper模式是这样描述的:
所谓映射器层,是用于在对象和数据库之间搬运数据,同时保证对象,数据库以及映射器本身都相互独立;(干货——数据映射器的描述)
3)与ORM 不同的是:ibatis 不是直接将类映射为数据库 后者 说把类的字段映射为数据库列,而是吧sql 语句的参数与结果(也即输入与输出)映射为类;(干货——ibatis 把 sql 语句的输入输出映射为类)
4)ibatis 在 类和数据库表之间建立了一个额外的间接层: 这为 在类和数据库表之间建立映射关系带来了极大的灵活性。其实这个间接层就是 SQL,这个间接层使得 ibatis 能够更好地隔离数据库设计 和 应用程序中使用的对象模型,这就使得它们之家的相关性降至最低;下图展示了ibatis 如何使用 sql 映射数据;(干货——ibatis
在 类和数据库表之间建立了一个额外的间接层,这个间接层就是sql 语句)
Attention)ibatis 团队通常将 所谓的 “数据映射器” 称为 SQL 映射器;
【1】映射 SQL 语句
Attention)以下的sql 映射是基于 ibatis的,不同于 mybatis;(它们的idea是一样的,只不过xml 元素的属性变了而已)
1)intro:任何一条sql 语句都可以看做是 一组输入和输出;如下图所示:
2)ibatis 使用xml 描述符文件来映射 SQL 语句的输入和输出;(我这里po出的是 mybatis 风格的 sql映射)
【2】ibatis 如何工作
【2.1】ibatis 之于小型、简单系统
1)intro:ibatis非常适合小型系统,reason如下:
reason1)ibatis 本身就很小,它不需要服务器或者其他任何类型的中间件,不需要任何额外的基础设施,没有任何的第三方依赖包;
reason2)ibatis 不会对应用程序或者数据库的现有设计强加任何措施;
reason3)ibatis 同样非常适合大型系统,它甚至可以扩展以满足企业级应用程序的需要;
【2.2】ibatis 之于大型,企业级系统
1)intro: reasons如下:
reason1)ibatis 没有对数据库模型或对象模型的设计做出任何假设;
reason2)ibatis 的某些特征使得它能够非常高效地处理大型数据集;
reason3)ibatis 允许你用多种方式建立从 对象到数据库的映射关系;
【4】何时不使用 ibatis
case1)当你具有完全控制权时,就有充分理由使用一个完全的 对象/关系映射方案,如Hibernate;
case2)当应用程序需要完全动态的 SQL时,那么 选择ibatis就是错误的选择;如果每条语句都是动态生成的话,最好使用原始的 JDBC;
case3)当没有使用 关系数据库时;
case4)当ibatis 不起作用时;
【5】 ibatis的一个荔枝(这里,我po出了 mybatis的荔枝)
step1)mybatis 的 映射文件的自动生成,参见 Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0
整合 中的 准备工作章节;
step2)修改 *Mapper.xml sql 映射文件,如下所示:
step3)建立 model层中的逻辑处理器(service):
step4)利用spring test 作为 client,测试用例如下:
1)本文部分内容转自 “ibatis in action”,旨在 review “ibatis是什么” 的相关知识;
2)intro to ibatis: ibatis 就是数据映射器,Martin Fowler在《企业应用架构模式》中,对 data mapper模式是这样描述的:
所谓映射器层,是用于在对象和数据库之间搬运数据,同时保证对象,数据库以及映射器本身都相互独立;(干货——数据映射器的描述)
3)与ORM 不同的是:ibatis 不是直接将类映射为数据库 后者 说把类的字段映射为数据库列,而是吧sql 语句的参数与结果(也即输入与输出)映射为类;(干货——ibatis 把 sql 语句的输入输出映射为类)
4)ibatis 在 类和数据库表之间建立了一个额外的间接层: 这为 在类和数据库表之间建立映射关系带来了极大的灵活性。其实这个间接层就是 SQL,这个间接层使得 ibatis 能够更好地隔离数据库设计 和 应用程序中使用的对象模型,这就使得它们之家的相关性降至最低;下图展示了ibatis 如何使用 sql 映射数据;(干货——ibatis
在 类和数据库表之间建立了一个额外的间接层,这个间接层就是sql 语句)
Attention)ibatis 团队通常将 所谓的 “数据映射器” 称为 SQL 映射器;
【1】映射 SQL 语句
Attention)以下的sql 映射是基于 ibatis的,不同于 mybatis;(它们的idea是一样的,只不过xml 元素的属性变了而已)
1)intro:任何一条sql 语句都可以看做是 一组输入和输出;如下图所示:
2)ibatis 使用xml 描述符文件来映射 SQL 语句的输入和输出;(我这里po出的是 mybatis 风格的 sql映射)
<insert id="insert" parameterType="com.mybatis.model.Student"> <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into t_student (name) values (#{name,jdbcType=VARCHAR}) </insert>
【2】ibatis 如何工作
【2.1】ibatis 之于小型、简单系统
1)intro:ibatis非常适合小型系统,reason如下:
reason1)ibatis 本身就很小,它不需要服务器或者其他任何类型的中间件,不需要任何额外的基础设施,没有任何的第三方依赖包;
reason2)ibatis 不会对应用程序或者数据库的现有设计强加任何措施;
reason3)ibatis 同样非常适合大型系统,它甚至可以扩展以满足企业级应用程序的需要;
【2.2】ibatis 之于大型,企业级系统
1)intro: reasons如下:
reason1)ibatis 没有对数据库模型或对象模型的设计做出任何假设;
reason2)ibatis 的某些特征使得它能够非常高效地处理大型数据集;
reason3)ibatis 允许你用多种方式建立从 对象到数据库的映射关系;
【4】何时不使用 ibatis
case1)当你具有完全控制权时,就有充分理由使用一个完全的 对象/关系映射方案,如Hibernate;
case2)当应用程序需要完全动态的 SQL时,那么 选择ibatis就是错误的选择;如果每条语句都是动态生成的话,最好使用原始的 JDBC;
case3)当没有使用 关系数据库时;
case4)当ibatis 不起作用时;
【5】 ibatis的一个荔枝(这里,我po出了 mybatis的荔枝)
step1)mybatis 的 映射文件的自动生成,参见 Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0
整合 中的 准备工作章节;
step2)修改 *Mapper.xml sql 映射文件,如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mybatis.dao.StudentMapper">
<resultMap id="BaseResultMap" type="com.mybatis.model.Student">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<insert id="insert" parameterType="com.mybatis.model.Student"> <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into t_student (name) values (#{name,jdbcType=VARCHAR}) </insert>
<sql id="Base_Column_List">
id, name
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap"
parameterType="java.lang.Integer">
select
<include refid="Base_Column_List" />
from t_student
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="int">
delete from t_student
where id = #{id,jdbcType=INTEGER}
</delete>
<update id="updateByPrimaryKey" parameterType="com.mybatis.model.Student">
update t_student
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.mybatis.model.Student">
update t_student
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
step3)建立 model层中的逻辑处理器(service):
package com.mybatis.service; import com.mybatis.model.Student; public interface StudentService { int insertStudent(int id, String username); Student getStudentById(int Id); int deleteStudent(int id); int updateStudent(Student stu); int updateStudentSelective(Student stu); }
package com.mybatis.serviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.mybatis.dao.StudentMapper; import com.mybatis.model.Student; import com.mybatis.service.StudentService; @Service public class StudentServiceImpl implements StudentService{ @Autowired private StudentMapper studentMapper; @Override public int insertStudent(int id, String username) { Student stu = new Student(id, username); int result = studentMapper.insert(stu); return result; } @Override public Student getStudentById(int id) { return studentMapper.selectByPrimaryKey(id); } @Override public int deleteStudent(int id) { return studentMapper.deleteByPrimaryKey(id); } @Override public int updateStudent(Student stu) { int result = studentMapper.updateByPrimaryKey(stu); return result; } @Override public int updateStudentSelective(Student stu) { int result = studentMapper.updateByPrimaryKeySelective(stu); return result; } }
step4)利用spring test 作为 client,测试用例如下:
package com.mybatis.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.mybatis.model.Student; import com.mybatis.service.StudentService; @RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类 @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) public class TestMyBatis { @Resource private StudentService studentService; @Test public void testUpdate2() { studentService.insertStudent(4, "zhugeliang"); Student stu = studentService.getStudentById(4); stu.setName("new_"+stu.getName()); int result = studentService.updateStudentSelective(stu); System.out.println(result); } /*@Test public void testUpdate1() { studentService.insertStudent(3, "zhouyu"); Student stu = studentService.getStudentById(3); stu.setName("new_zhouyu"); int result = studentService.updateStudent(stu); System.out.println(result); }*/ /*@Test public void testDelete() { int result = studentService.deleteStudent(1); System.out.println(result); } */ /*@Test public void test1() { Student stu = studentService.getStudentById(1); System.out.println(" the name is " + stu.getName()); } @Test public void testInsert() { studentService.insertStudent(2, "liubei"); System.out.println("the 2nd name is " + studentService.getStudentById(2).getName()); }*/ }
相关文章推荐
- Android 读写文件整理
- ButterKnife 点击事件没反应的解决方案
- (转)用Eclipse进行C++开发时Bianry not found的问题解决
- python日志操作(loggin模块)
- SpringMVC+MyBatis - 7 Spring自动扫描注解类的冲突问题
- redis win2003 32位下载啊
- 大数据大牛- sunddenly
- jquery map.js
- 链表中有环——怎么判断
- 深入探究Swift数组背后的协议、方法、拓展(转)
- EventBus和litepal数据库的一般使用
- 在ubuntu 12.04上安裝Redmine
- Runtime 获取成员变量列表
- Android中findViewById获取控件返回为空问题怎么解决
- POJ 2406 KMP
- 防痴呆的:啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝多少瓶
- 暴力破解软件的破解选项如何设置
- wpf中ToolTip实现
- MongoDB 安装及问题解决
- Ubuntu 搭建PHP环境