iBatis 简介:

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

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

搭建iBatis 开发环境:

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

2 、编写配置文件:

Jdbc 连接的属性文件

总配置文件, SqlMapConfig.xml

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

Demo 

Student.java:

  1. package com.iflytek.entity;
  2. import java.sql.Date;
  3. /**
  4. * @author xudongwang 2011-12-31
  5. *
  6. * Email:xdwangiflytek@gmail.com
  7. *
  8. */
  9. public class Student {
  10. // 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题
  11. private int id;
  12. private String name;
  13. private Date birth;
  14. private float score;
  15. public int getId() {
  16. return id;
  17. }
  18. public void setId(int id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public Date getBirth() {
  28. return birth;
  29. }
  30. public void setBirth(Date birth) {
  31. this.birth = birth;
  32. }
  33. public float getScore() {
  34. return score;
  35. }
  36. public void setScore(float score) {
  37. this.score = score;
  38. }
  39. @Override
  40. public String toString() {
  41. return "id=" + id + "\tname=" + name + "\tmajor=" + birth + "\tscore="
  42. + score + "\n";
  43. }
  44. }

SqlMap.properties :

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:/ibatis
  3. username=root
  4. password=

Student.xml :

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  3. "http://ibatis.apache.org/dtd/sql-map-2.dtd">
  4. <sqlMap>
  5. <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 -->
  6. <typeAlias alias="Student" type="com.iflytek.entity.Student" />
  7. <!-- 这样以后改了sql,就不需要去改java代码了 -->
  8. <!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
  9. <select id="selectAllStudent" resultClass="Student">
  10. select * from
  11. tbl_student
  12. </select>
  13. <!-- parameterClass表示参数的内容 -->
  14. <!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 -->
  15. <select id="selectStudentById" parameterClass="int" resultClass="Student">
  16. select * from tbl_student where id=#id#
  17. </select>
  18. <!-- 注意这里的resultClass类型,使用Student类型取决于queryForList还是queryForObject -->
  19. <select id="selectStudentByName" parameterClass="String"
  20. resultClass="Student">
  21. select name,birth,score from tbl_student where name like
  22. '%$name$%'
  23. </select>
  24. <insert id="addStudent" parameterClass="Student">
  25. insert into
  26. tbl_student(name,birth,score) values
  27. (#name#,#birth#,#score#);
  28. <selectKey resultClass="int" keyProperty="id">
  29. select @@identity as inserted
  30. <!-- 这里需要说明一下不同的数据库主键的生成,对各自的数据库有不同的方式: -->
  31. <!-- mysql:SELECT LAST_INSERT_ID() AS VALUE -->
  32. <!-- mssql:select @@IDENTITY as value -->
  33. <!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL -->
  34. <!-- 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。
  35. 有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 -->
  36. </selectKey>
  37. </insert>
  38. <delete id="deleteStudentById" parameterClass="int">
  39. <!-- #id#里的id可以随意取,但是上面的insert则会有影响,因为上面的name会从Student里的属性里去查找 -->
  40. <!-- 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 -->
  41. delete from tbl_student where id=#id#
  42. </delete>
  43. <update id="updateStudent" parameterClass="Student">
  44. update tbl_student set
  45. name=#name#,birth=#birth#,score=#score# where id=#id#
  46. </update>
  47. </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 :

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

StudentDao :

  1. package com.iflytek.dao;
  2. import java.util.List;
  3. import com.iflytek.entity.Student;
  4. /**
  5. * @author xudongwang 2011-12-31
  6. *
  7. * Email:xdwangiflytek@gmail.com
  8. *
  9. */
  10. public interface StudentDao {
  11. /**
  12. * 添加学生信息
  13. *
  14. * @param student
  15. * 学生实体
  16. * @return 返回是否添加成功
  17. */
  18. public boolean addStudent(Student student);
  19. /**
  20. * 根据学生id删除学生信息
  21. *
  22. * @param id
  23. * 学生id
  24. * @return 删除是否成功
  25. */
  26. public boolean deleteStudentById(int id);
  27. /**
  28. * 更新学生信息
  29. *
  30. * @param student
  31. * 学生实体
  32. * @return 更新是否成功
  33. */
  34. public boolean updateStudent(Student student);
  35. /**
  36. * 查询全部学生信息
  37. *
  38. * @return 返回学生列表
  39. */
  40. public List<Student> selectAllStudent();
  41. /**
  42. * 根据学生姓名模糊查询学生信息
  43. *
  44. * @param name
  45. * 学生姓名
  46. * @return 学生信息列表
  47. */
  48. public List<Student> selectStudentByName(String name);
  49. /**
  50. * 根据学生id查询学生信息
  51. *
  52. * @param id
  53. * 学生id
  54. * @return 学生对象
  55. */
  56. public Student selectStudentById(int id);
  57. }

StudentDaoImpl :

  1. package com.iflytek.daoimpl;
  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import java.sql.SQLException;
  5. import java.util.List;
  6. import com.ibatis.common.resources.Resources;
  7. import com.ibatis.sqlmap.client.SqlMapClient;
  8. import com.ibatis.sqlmap.client.SqlMapClientBuilder;
  9. import com.iflytek.dao.StudentDao;
  10. import com.iflytek.entity.Student;
  11. /**
  12. * @author xudongwang 2011-12-31
  13. *
  14. * Email:xdwangiflytek@gmail.com
  15. *
  16. */
  17. public class StudentDaoImpl implements StudentDao {
  18. private static SqlMapClient sqlMapClient = null;
  19. // 读取配置文件
  20. static {
  21. try {
  22. Reader reader = Resources
  23. .getResourceAsReader("com/iflytek/entity/SqlMapConfig.xml");
  24. sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
  25. reader.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. public boolean addStudent(Student student) {
  31. Object object = null;
  32. boolean flag = false;
  33. try {
  34. object = sqlMapClient.insert("addStudent", student);
  35. System.out.println("添加学生信息的返回值:" + object);
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. if (object != null) {
  40. flag = true;
  41. }
  42. return flag;
  43. }
  44. public boolean deleteStudentById(int id) {
  45. boolean flag = false;
  46. Object object = null;
  47. try {
  48. object = sqlMapClient.delete("deleteStudentById", id);
  49. System.out.println("删除学生信息的返回值:" + object + ",这里返回的是影响的行数");
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. if (object != null) {
  54. flag = true;
  55. }
  56. return flag;
  57. }
  58. public boolean updateStudent(Student student) {
  59. boolean flag = false;
  60. Object object = false;
  61. try {
  62. object = sqlMapClient.update("updateStudent", student);
  63. System.out.println("更新学生信息的返回值:" + object + ",返回影响的行数");
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. if (object != null) {
  68. flag = true;
  69. }
  70. return flag;
  71. }
  72. public List<Student> selectAllStudent() {
  73. List<Student> students = null;
  74. try {
  75. students = sqlMapClient.queryForList("selectAllStudent");
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. return students;
  80. }
  81. public List<Student> selectStudentByName(String name) {
  82. List<Student> students = null;
  83. try {
  84. students = sqlMapClient.queryForList("selectStudentByName",name);
  85. } catch (SQLException e) {
  86. e.printStackTrace();
  87. }
  88. return students;
  89. }
  90. public Student selectStudentById(int id) {
  91. Student student = null;
  92. try {
  93. student = (Student) sqlMapClient.queryForObject(
  94. "selectStudentById", id);
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. }
  98. return student;
  99. }
  100. }

TestIbatis.java :

  1. package com.iflytek.test;
  2. import java.sql.Date;
  3. import java.util.List;
  4. import com.iflytek.daoimpl.StudentDaoImpl;
  5. import com.iflytek.entity.Student;
  6. /**
  7. * @author xudongwang 2011-12-31
  8. *
  9. * Email:xdwangiflytek@gmail.com
  10. *
  11. */
  12. public class TestIbatis {
  13. public static void main(String[] args) {
  14. StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
  15. System.out.println("测试插入");
  16. Student addStudent = new Student();
  17. addStudent.setName("李四");
  18. addStudent.setBirth(Date.valueOf("2011-09-02"));
  19. addStudent.setScore();
  20. System.out.println(studentDaoImpl.addStudent(addStudent));
  21. System.out.println("测试根据id查询");
  22. System.out.println(studentDaoImpl.selectStudentById());
  23. System.out.println("测试模糊查询");
  24. List<Student> mohuLists = studentDaoImpl.selectStudentByName("李");
  25. for (Student student : mohuLists) {
  26. System.out.println(student);
  27. }
  28. System.out.println("测试查询所有");
  29. List<Student> students = studentDaoImpl.selectAllStudent();
  30. for (Student student : students) {
  31. System.out.println(student);
  32. }
  33. System.out.println("根据id删除学生信息");
  34. System.out.println(studentDaoImpl.deleteStudentById());
  35. System.out.println("测试更新学生信息");
  36. Student updateStudent = new Student();
  37. updateStudent.setId();
  38. updateStudent.setName("李四1");
  39. updateStudent.setBirth(Date.valueOf("2011-08-07"));
  40. updateStudent.setScore();
  41. System.out.println(studentDaoImpl.updateStudent(updateStudent));
  42. }
  43. }

iBatis 的优缺点:

优点:

1、 减少代码量,简单;

2、 性能增强;

3、 Sql 语句与程序代码分离;

4、 增强了移植性;

缺点:

1、 和Hibernate 相比,sql 需要自己写;

2、 参数数量只能有一个,多个参数时不太方便;

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

  1. iBatis简单入门教程

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

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

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

  3. 【转】iBatis简单入门教程

    1. 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. 前端通信:ajax设计方案(三)--- 集成ajax上传技术

    在此之前让我感慨一下现在的前端开发的氛围.我遇到好多人,给我的观念都是,这个东西这个框架有了,那个东西那个框架做了,前端嘛,学几个框架,这个拼凑一下那个拼凑一下就好了.其实我想问,东西都框架做了,那你 ...

  2. mysql和mysql jdbc连接器mysql-connector-java对应关系

    mysql和mysql jdbc连接器mysql-connector-java对应关系,请参考下图:来源于mysql官网

  3. CSS Sprites(CSS精灵) 的优缺点

    CSS Sprites 的优点:     1.减少图片的字节     2.减少了网页的http请求,从而大大的提高了页面的性能     3.解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命 ...

  4. 在PHP中使用MySQL Mysqli操作数据库 ,以及类操作方法

    先来操作函数部分,普遍的MySQL 函数方法,但随着PHP5的发展,有些函数使用的要求加重了,有些则将废弃不用,有些则参数必填... ================================= ...

  5. java 实现 PDF 加水印功能

    使用java代码实现给PDF加水印的功能 首先导入所需要的依赖 <dependency> <groupId>com.itextpdf</groupId> <a ...

  6. C# using用法

    一.using指令 使用using指令在文件顶部引入命名空间,如 using System; using System.IO; 二.using别名 用using为命名空间或类型定义别名,当引入的多个命 ...

  7. C# 开发者审查代码的41条建议

    1. 确保没有任何警告(warnings). 2.如果先执行Code Analysis(启用所有Microsoft Rules)再消除所有警告就更好了. 3. 去掉所有没有用到的usings.编码过程 ...

  8. JPA之@OneToMany、@ManyToOne、@JoinColumn

    顾名思义,@OneToMany.@ManyToOne这两个注解就是处理一对多,多对一的关系 这两个注解是成双成对的,有了@OneToMany,一定会配置一个@ManyToOne OneToMany设置 ...

  9. Jsp&Servlet入门级项目全程实录第8讲

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.添加dao public int studentAdd(Connection con,Student studen ...

  10. Java中Date与String的相互转换

    我们在注册网站的时候,往往需要填写个人信息,如姓名,年龄,出生日期等,在页面上的出生日期的值传递到后台的时候是一个字符串,而我们存入数据库的时候确需要一个日期类型,反过来,在页面上显示的时候,需要从数 ...