ibatis(2)ibatis是什么

【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映射)
<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_studentwhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="int">delete from t_studentwhere id = #{id,jdbcType=INTEGER}</delete><update id="updateByPrimaryKey" parameterType="com.mybatis.model.Student">update t_studentset 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{@Autowiredprivate StudentMapper studentMapper;@Overridepublic int insertStudent(int id, String username) {Student stu = new Student(id, username);int result = studentMapper.insert(stu);		return result;}@Overridepublic Student getStudentById(int id) {return studentMapper.selectByPrimaryKey(id);}@Overridepublic int deleteStudent(int id) {return studentMapper.deleteByPrimaryKey(id);}@Overridepublic int updateStudent(Student stu) {int result = studentMapper.updateByPrimaryKey(stu);return result;}@Overridepublic 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;  @Testpublic 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);}/*@Testpublic void testUpdate1() {studentService.insertStudent(3, "zhouyu");Student stu = studentService.getStudentById(3);stu.setName("new_zhouyu");int result = studentService.updateStudent(stu);System.out.println(result);}*//*@Testpublic 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());  }  @Testpublic void testInsert() {studentService.insertStudent(2, "liubei");System.out.println("the 2nd name is " + studentService.getStudentById(2).getName());}*/
}  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331102.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

究竟啥才是互联网架构“高可用”

转载自 究竟啥才是互联网架构“高可用”一、什么是高可用 高可用HA&#xff08;High Availability&#xff09;是分布式系统架构设计中必须考虑的因素之一&#xff0c;它通常是指&#xff0c;通过设计减少系统不能提供服务的时间。 假设系统一直能够提供服务&#xff0c;我们说…

maven(3)maven3.3.9使用入门

【0】README1&#xff09;maven 安装step1&#xff09;检查 jdk 是否安装且 环境变量 JAVA_HOME 是否设置&#xff1b;step2&#xff09;download maven&#xff1a; https://maven.apache.org/download.cgi?Preferredftp://mirror.reverse.net/pub/apache/step3&#xff09;…

TCP接入层的负载均衡、高可用、扩展性架构

转载自 TCP接入层的负载均衡、高可用、扩展性架构 一、web-server的负载均衡 互联网架构中&#xff0c;web-server接入一般使用nginx来做反向代理&#xff0c;实施负载均衡。整个架构分三层&#xff1a; 上游调用层&#xff0c;一般是browser或者APP 中间反向代理层&#xff…

使用poi统计工作职责

1 创建一个新的sheet工作页 Sheet job workbook.createSheet("工作职责统计"); 2 查询工作职责问题选项列表&#xff0c;并设置第一行倒出时间 List<Syslistconfig> listconfigs syslistconfigDao.listConfig(29); //工作职责问题选项列表job.createRow(0)…

漫画:什么是字典序算法

转载自 漫画&#xff1a;什么是字典序算法&#xff1f;算法题目&#xff1a; 给定一个正整数&#xff0c;实现一个方法来求出离该整数最近的大于自身的“换位数”。 什么是换位数呢&#xff1f;就是把一个整数各个数位的数字进行全排列&#xff0c;从而得到新的整数。例如53241…

mybatis_user_guide(2)mybatis3.4.0快速入门

【0】README0&#xff09;以下部分内容转自&#xff1a;“mybatis v.3.4.0 User Guide”&#xff1b;1&#xff09;本文旨在梳理 如何 构建 mybatis 环境&#xff0c;与 db 连接&#xff0c;且采用 JUnit 搭建其测试用例&#xff1b;2&#xff09;本文的环境配置都是基于纯 my…

jQuery中的几个案例:隔行变色、复选框全选和全不选

1 表格隔行变色 1 技术分析&#xff1a; 1 &#xff09;基本过滤选择器&#xff1a; odd: even: 2 &#xff09;jq添加和移除样式&#xff1a; addClass(); removeClass(); 2 代码实现 <script src"js/jquery1.11.3/jquery.min.js" type"text/javasc…

从 Linux 源码看 Socket 的阻塞和非阻塞

转载自 从 Linux 源码看 Socket 的阻塞和非阻塞笔者一直觉得如果能知道从应用到框架再到操作系统的每一处代码&#xff0c;是一件Exciting的事情。大部分高性能网络框架采用的是非阻塞模式。笔者这次就从linux源码的角度来阐述socket阻塞(block)和非阻塞(non_block)的区别。 本…

pojo和javabean的区别

【0】README 1&#xff09;本文转自&#xff1a; http://wenku.baidu.com/view/eba89bbcf121dd36a32d828a.html 【1】正文如下&#xff1a; POJO 和JavaBean是我们常见的两个关键字&#xff0c;一般容易混淆&#xff0c;POJO全称是Plain Ordinary Java Object / Pure Old Jav…

使用poi调整字体格式、添加单元格注释、自动调整列宽

1 创建新的工作铺 import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apa…

MySQL 的索引是什么?怎么优化?

转载自 MySQL 的索引是什么&#xff1f;怎么优化&#xff1f; 摘要: 索引对大数据的查询速度的提升是非常大的&#xff0c;Explain可以帮你分析SQL语句是否用到相关索引。 索引类似大学图书馆建书目索引&#xff0c;可以提高数据检索的效率&#xff0c;降低数据库的IO成本。My…

maven(5)坐标和依赖

【0】README1&#xff09;本文部分文字转自 “maven实战”&#xff0c;旨在 review “maven(5)坐标和依赖” 的相关知识&#xff1b;【2】坐标详解 1&#xff09;intro&#xff1a;坐标用于定位 类库&#xff0c;而一组maven 坐标通过一些元素来进行定义的&#xff1a;groupId…

poi中文api文档

POI中文API文档 一、 POI简介 Apache POI是Apache软件基金会的开放源码函式库&#xff0c;POI提供API给Java程序对Microsoft Office格式档案读和写的功能。二、 HSSF概况 HSSF 是Horrible SpreadSheet Format的缩写&#xff0c;通过HSSF&#xff0c;你可以用纯Java代码来读取、…

聊聊MyBatis缓存机制

转载自 聊聊MyBatis缓存机制前言MyBatis是常见的Java数据库访问层框架。在日常工作中&#xff0c;开发人员多数情况下是使用MyBatis的默认缓存配置&#xff0c;但是MyBatis缓存机制有一些不足之处&#xff0c;在使用中容易引起脏数据&#xff0c;形成一些潜在的隐患。个人在业务…

maven(6)仓库

【0】README1&#xff09;本文部分文字转自 “maven实战”&#xff0c;旨在 review “maven(6)仓库” 的相关知识&#xff1b; 【1】何为 Maven仓库1&#xff09;intro to 构件&#xff1a;在maven中&#xff0c;任何一个依赖&#xff0c;插件或者项目构建的输出&#xff0c;都…

防止用户重复提交表单数据,session方式,js方式

1. 使用session的方式创建Token令牌解决 创建一个生成令牌的工具类&#xff0c;在该类中有返回类的对象&#xff0c;生成token的方法public class TokenUtil {/**单例设计模式&#xff08;保证类的对象在内存中只有一个&#xff09;*1、把类的构造函数私有*2、自己创建一个类的…

mybatis_user_guide(3)XML配置

【-1】README1&#xff09;本文全文总结于 http://www.mybatis.org/mybatis-3/zh/configuration.html#environments【0】MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置&#xff08;settings&#xff09;和属性&#xff08;properties&#xff09;信息。文档的顶层结构如…

easyUI 运用窗口和form表单制作导出功能

这里运用到easyUI的窗口模式和form表单的提交制作一个有条件的导出excel数据统计的功能&#xff0c;主要是知道了怎么运用easyUI的窗口和表单 jsp中&#xff1a;<!-- 导出数据来源条件窗口 --><div id"exportSign" ><form id"condition" me…

分布式一致性算法:可能比你想象得更复杂

转载自 分布式一致性算法&#xff1a;可能比你想象得更复杂分布式系统的难题张大胖遇到了一个难题。他们公司的有个服务器&#xff0c;上面保存着宝贵的数据&#xff0c;领导Bill 为了防止它挂掉&#xff0c; 要求张大胖想想办法把数据做备份。张大胖发挥了抽象的能力&#xff…

mybatis_user_guide(4) Mapper XML 文件

【-1】README 1&#xff09;本文全文总结于 http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html【0】SQL 映射文件有很少的几个顶级元素&#xff08;按照它们应该被定义的顺序&#xff09;&#xff1a; cache – 给定命名空间的缓存配置。cache-ref – 其他命名空间缓存配置…