1. iBatis 简介:

iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。

官网为:http://www.mybatis.org/

2. 搭建iBatis 开发环境:

1 、导入相关的jar 包,ibatis-2.3.0.677.jar 、mysql-connector-java-5.1.6-bin.jar

2 、编写配置文件:

Jdbc 连接的属性文件

总配置文件, SqlMapConfig.xml

关于每个实体的映射文件(Map 文件)

3. Demo

Student.java:

    package com.iflytek.entity;
import java.sql.Date;
/**
* @author xudongwang 2011-12-31
*
* Email:xdwangiflytek@gmail.com
*
*/
public class Student {
// 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题
  private int id;
  private String name;
  private Date birth;
  private float score;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Date getBirth() {
    return birth;
  }
  public void setBirth(Date birth) {
    this.birth = birth;
  }
  public float getScore() {
    return score;
  }
  public void setScore(float score) {
    this.score = score;
  }
  @Override
  public String toString() {
    return "id=" + id + "\tname=" + name + "\tmajor=" + birth + "\tscore="
    + score + "\n";
  }
}

SqlMap.properties :

    driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ibatis
username=root
password=123

Student.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>
<!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 -->
<typeAlias alias="Student" type="com.iflytek.entity.Student" />
<!-- 这样以后改了sql,就不需要去改java代码了 -->
<!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
<select id="selectAllStudent" resultClass="Student">
select * from tbl_student
</select>
<!-- parameterClass表示参数的内容 -->
<!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 -->
<select id="selectStudentById" parameterClass="int" resultClass="Student">
select * from tbl_student where id=#id#
</select>
<!-- 注意这里的resultClass类型,使用Student类型取决于queryForList还是queryForObject -->
<select id="selectStudentByName" parameterClass="String" resultClass="Student">
select name,birth,score from tbl_student where name like '%$name$%'
</select>
<insert id="addStudent" parameterClass="Student">
insert into tbl_student(name,birth,score) values (#name#,#birth#,#score#);
<selectKey resultClass="int" keyProperty="id">
select @@identity as inserted
<!-- 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: -->
<!-- mysql:SELECT LAST_INSERT_ID() AS VALUE -->
<!-- mssql:select @@IDENTITY as value -->
<!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL -->
<!-- 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。
有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 -->
</selectKey>
</insert>
<delete id="deleteStudentById" parameterClass="int">
<!-- #id#里的id可以随意取,但是上面的insert则会有影响,因为上面的name会从Student里的属性里去查找 -->
<!-- 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 -->
delete from tbl_student where id=#id#
</delete>
<update id="updateStudent" parameterClass="Student">
update tbl_student set name=#name#,birth=#birth#,score=#score# where id=#id#
</update>
</sqlMap>

说明:

如果xml 中没有ibatis 的提示,则window --> Preference--> XML-->XML Catalog---> 点击add

选择uri URI: 请选择本地文件系统上

iBatisDemo1/WebContent/WEB-INF/lib/sql-map-config-2.dtd 文件;

Key Type: 选择Schema Location;

Key: 需要联网的,不建议使用;

SqlMapConfig.xml :

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- 引用JDBC属性的配置文件 -->
<properties resource="com/iflytek/entity/SqlMap.properties" />
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC">
<!-- 数据源 -->
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}" />
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${username}" />
<property name="JDBC.Password" value="${password}" />
</dataSource>
</transactionManager>
<!-- 这里可以写多个实体的映射文件 -->
<sqlMap resource="com/iflytek/entity/Student.xml" />
</sqlMapConfig>

StudentDao :

package com.iflytek.dao;
import java.util.List;
import com.iflytek.entity.Student;
/**
* @author xudongwang 2011-12-31
*
* Email:xdwangiflytek@gmail.com
*
*/
public interface StudentDao {
/**
* 添加学生信息
*
* @param student
* 学生实体
* @return 返回是否添加成功
*/
public boolean addStudent(Student student);
/**
* 根据学生id删除学生信息
*
* @param id
* 学生id
* @return 删除是否成功
*/
public boolean deleteStudentById(int id);
/**
* 更新学生信息
*
* @param student
* 学生实体
* @return 更新是否成功
*/
public boolean updateStudent(Student student);
/**
* 查询全部学生信息
*
* @return 返回学生列表
*/
public List<Student> selectAllStudent();
/**
* 根据学生姓名模糊查询学生信息
*
* @param name
* 学生姓名
* @return 学生信息列表
*/
public List<Student> selectStudentByName(String name);
/**
* 根据学生id查询学生信息
*
* @param id
* 学生id
* @return 学生对象
*/
public Student selectStudentById(int id);
}

TestIbatis.java :

package com.iflytek.test;
import java.sql.Date;
import java.util.List;
import com.iflytek.daoimpl.StudentDaoImpl;
import com.iflytek.entity.Student;
/**
* @author xudongwang 2011-12-31
*
* Email:xdwangiflytek@gmail.com
*
*/
public class TestIbatis {
public static void main(String[] args) {
  StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
  System.out.println("测试插入");
  
     Student addStudent = new Student();
  addStudent.setName("李四");
  addStudent.setBirth(Date.valueOf("2011-09-02"));
  addStudent.setScore(88);
  System.out.println(studentDaoImpl.addStudent(addStudent));
  System.out.println("测试根据id查询");
  System.out.println(studentDaoImpl.selectStudentById(1));
  System.out.println("测试模糊查询");
       List<Student> mohuLists = studentDaoImpl.selectStudentByName("李");
  for (Student student : mohuLists) {
    System.out.println(student);
  }
  System.out.println("测试查询所有");
  List<Student> students = studentDaoImpl.selectAllStudent();
  for (Student student : students) {
    System.out.println(student);
  }
  System.out.println("根据id删除学生信息");
  System.out.println(studentDaoImpl.deleteStudentById(1));
  System.out.println("测试更新学生信息");
  Student updateStudent = new Student();
  updateStudent.setId(1);
  updateStudent.setName("李四1");
  updateStudent.setBirth(Date.valueOf("2011-08-07"));
  updateStudent.setScore(21);
  System.out.println(studentDaoImpl.updateStudent(updateStudent));
}
}

4. iBatis 的优缺点:

  • 优点:
  1. 减少代码量,简单;
  2. 性能增强;
  3. Sql 语句与程序代码分离;
  4. 增强了移植性;
  • 缺点:
  1. 和Hibernate 相比,sql 需要自己写;
  2. 参数数量只能有一个,多个参数时不太方便;

转自:http://www.cnblogs.com/ycxyyzw/archive/2012/10/13/2722567.html

【转】iBatis简单入门教程的更多相关文章

  1. iBatis简单入门教程

    iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足 ...

  2. 转:iBatis简单入门教程

    iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足 ...

  3. [转]iBatis简单入门教程

    iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足 ...

  4. 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注 ...

  5. GitHub这么火,程序员你不学学吗? 超简单入门教程 【转载】

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...

  6. Flyway 简单入门教程

    原文地址:Flyway 简单入门教程 博客地址:http://www.extlight.com 一.前言 Flyway 是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式.Flyway 可以 ...

  7. NumPy简单入门教程

    # NumPy简单入门教程 NumPy是Python中的一个运算速度非常快的一个数学库,它非常重视数组.它允许你在Python中进行向量和矩阵计算,并且由于许多底层函数实际上是用C编写的,因此你可以体 ...

  8. lodop简单入门教程

    lodop简单入门 1 安装(这个不介绍,下载安装即可) 声明只能装windows,linux不能装,所以linux 服务器要使用直接使用http://localhost:8000/CLodopfun ...

  9. GitHub这么火,程序员你不学学吗? 超简单入门教程 干货

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...

随机推荐

  1. gdbserver静态编译

    redhat9 编译gdb server(静态编译)下载gdb-6.2a.tar:http://download.chinaunix.net/download.php?id=6680&Reso ...

  2. AndroidManifest.xml文件详解(application)

    http://blog.csdn.net/think_soft/article/details/7557101 语法(SYNATX): <application android:allowTas ...

  3. JSP2 自定义标签

    实现步骤 实现自定义标签的处理类继承javax.servlet.jsp.tagext.SimpleTagSupport,并重写doTag方法 建立标签库配置文件 在jsp中使用自定义标签 一个简单的标 ...

  4. 《深入浅出Nodejs》笔记——模块机制(2)

    前言 书上还有很大一部分讲了C/C++模块的编译过程.核心模块编写和C/C++扩展模块的内容,不过我对C++一窍不通因此没有仔细看,如果以后需要再自习看吧. 包与NPM 第三方模块中,模块和模块之间是 ...

  5. 洛谷——P1748 H数

    P1748 H数 题目背景 无 题目描述 所谓H数,是指只含有2,3,5,7这些质因数的数,如630是H数,而22不是.现在要求输出第n个H数,为了方便起见将H[1]定为1.已知n不超过10000,最 ...

  6. Linux命令之chattr

    chattr [-RVf] [-v version] [mode] files… chattr修改文件在Linux第二扩展文件系统(E2fs)上的特有属性.符号模式(mode)有+-=[aAcCdDe ...

  7. Xamarin 中Visual Studio创建项目提示错误

    Xamarin 中Visual Studio创建项目提示错误 错误信息:Object reference not set to an instance of an object 出现这种情况,是由于没 ...

  8. UVA 12338:Anti-Rhyme Pairs(后缀数组+ST表)

    [题目链接] click [题目大意] 给出一些字符串,询问查询任意两个字符串的最长公共前缀 [题解] 将字符串拼接,对拼接的字符串做后缀数组,对于查询的两个字符串, 只要在height数组上查询区间 ...

  9. [AGC027F]Grafting

    题意:给定两棵树,一次操作形如:选一个未被选过的叶子,改变它的一条出边,问能否在第一棵树上做一些操作使得它变成第二棵树,如果能则询问最小操作次数 如果答案不是$n$,那么存在一个点$r$没有被选中过 ...

  10. 【DFS】Gym - 100781A - Adjoin the Networks

    给你一个森林,让你把它连接成一颗树,使得直径最小. 就求出每颗树的重心以后,全都往直径最大的那个的重心上连,一般情况是最大/2+次大/2+1,次大/2+第三大/2+2 中取较大者. 还有些特殊情况要特 ...