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 连接的属性文件  (eg:SqlMap.properties)

总配置文件 (SqlMapConfig.xml)

关于每一个实体的映射文件(Map 文件) (eg:Student.xml)

------------------------------------------------------------------------------------------

本文以 对学生Student的 增删改查为例

Demo:

project文件夹:

数据库结构截图:

--------------------------------------------------------------------------------------------------------------------------

1. Student.java

package com.ibatis.entity;

import java.sql.Date;

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 "Student [id=" + id + ", name=" + name + ", birth=" + birth
+ ", score=" + score + "]";
} }


2. SqlMap.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
username=root
password=123456

3. 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.ibatis.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>

4. 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/ibatis/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/ibatis/Student.xml" />
</sqlMapConfig>

5. StudentDao.java

package com.ibatis.dao;

import java.util.List;

import com.ibatis.entity.Student;
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); }

6. StudentDaoImpl.java

package com.ibatis.daoimpl;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.dao.StudentDao;
import com.ibatis.entity.Student; import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class StudentDaoImpl implements StudentDao { private static SqlMapClient sqlMapClient = null; // 读取配置文件
static {
try {
Reader reader = Resources
.getResourceAsReader("com/ibatis/SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} public boolean addStudent(Student student) {
Object object = null;
boolean flag = false;
try {
object = sqlMapClient.insert("addStudent", student);
System.out.println("加入学生信息的返回值:" + object);
} catch (SQLException e) {
e.printStackTrace();
}
if (object != null) {
flag = true;
}
return flag;
} public boolean deleteStudentById(int id) {
boolean flag = false;
Object object = null;
try {
object = sqlMapClient.delete("deleteStudentById", id);
System.out.println("删除学生信息的返回值:" + object + "。这里返回的是影响的行数");
} catch (SQLException e) {
e.printStackTrace();
}
if (object != null) {
flag = true; }
return flag; } public boolean updateStudent(Student student) {
boolean flag = false;
Object object = false;
try {
object = sqlMapClient.update("updateStudent", student);
System.out.println("更新学生信息的返回值:" + object + ",返回影响的行数");
} catch (SQLException e) {
e.printStackTrace();
}
if (object != null) {
flag = true;
}
return flag;
} public List<Student> selectAllStudent() {
List<Student> students = null;
try {
students = sqlMapClient.queryForList("selectAllStudent");
} catch (SQLException e) {
e.printStackTrace();
}
return students;
} public List<Student> selectStudentByName(String name) {
List<Student> students = null;
try {
students = sqlMapClient.queryForList("selectStudentByName",name);
} catch (SQLException e) {
e.printStackTrace();
}
return students;
} public Student selectStudentById(int id) {
Student student = null;
try {
student = (Student) sqlMapClient.queryForObject(
"selectStudentById", id);
} catch (SQLException e) {
e.printStackTrace();
}
return student;
} }

7. TestIbatis.java

package com.ibatis.test;
import java.sql.Date;
import java.util.List; import com.ibatis.daoimpl.StudentDaoImpl;
import com.ibatis.entity.Student; 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)); }
}

iBatis 的优缺点:

长处:

1、 降低代码量,简单。

2、 性能增强。

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

4、 增强了移植性。

缺点:

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

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

ibatis 入门的更多相关文章

  1. 一个简单的iBatis入门例子

    一个简单的iBatis入门例子,用ORACLE和Java测试 目录结构: 1.导入iBatis和oracle驱动. 2.创建类Person.java package com.ibeats;import ...

  2. Ibatis入门基本语法(转) good

    Ibatis入门基本语法 一个项目中在写ibatis中的sql语句时,where user_id in (#user_id_list# ), 运行时总是不行,后来上网查了查,才知道这里不该用#,而应该 ...

  3. ibatis入门教程

    转载自  http://www.cnblogs.com/ycxyyzw/archive/2012/10/13/2722567.html iBatis 简介: iBatis 是apache 的一个开源项 ...

  4. IBatis入门

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

  5. Ibatis入门基本语法

    1.       Ibatis是开源软件组织Apache推出的一种轻量级的对象关系映射(ORM)框架,和Hibernate.Toplink等在java编程的对象持久化方面深受开发人员欢迎. 对象关系映 ...

  6. ibatis入门教程一

    这几天研究ibatis玩,参考一篇贴子进行安装配置:蓝雪森林 选择这个帖子来跟随配置是因为这个帖子看着比较干净,但是我仍旧在配置得过程中出现了好几个问题,所以我决定在这个帖子的基础上将更多细节加上,做 ...

  7. ibatis入门实例(完整)

    一:首先展示一下我的web文件结构,首先导入Ibatis所需jar和数据库驱动,从第二步开始跟着笔者一步步来 二:数据库建测试表 CREATE TABLE STUDENT ( ID NUMBER(5) ...

  8. Java后台技术IBATIS入门

    做过.net后台开发的同志一定用过Entity FrameWork,该框架实现了实体Entity到数据库行的映射,通过操作实体DataSet,就能够直接同步修改到数据库.但是Java暂时没有类似的技术 ...

  9. IBatis和Hibernate区别

    1. 简介 Hibernate是当前最流行的O/R mapping框架.它出身于sf.net,现在已经成为Jboss的一部分了.iBATIS是另外一种优秀的O/R mapping框架,现已改名叫myB ...

随机推荐

  1. Jmeter之定时器

    转自:https://www.cnblogs.com/imyalost/p/6004678.html 一.定时器的作用域 1.定时器是在每个sampler(采样器)之前执行的,而不是之后(无论定时器位 ...

  2. Centos7 安装MongoDB的详细过程

    一.简介 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品 ...

  3. 00C#

    C# C#(读作“See Sharp”)是一种简单.现代.面向对象且类型安全的编程语言.C# 起源于 C 语言家族,因此,对于 C.C++ 和 Java 程序员,可以很快熟悉这种新的语言.C# 已经分 ...

  4. 09CSS高级定位

    CSS高级定位 定位方式——position position:static|absolute|relative static表示为静态定位,是默认设置.  absolute表示绝对定位,与下位置属 ...

  5. iPhoneX 适配H5页面的解决方案

    由于在iPhonex在状态栏增加了24px的高度,对于通栏banner规范的内容区域会有遮挡情况. 解决方案:在页面通栏banner顶部增加一层高度44px的黑色适配层,整个页面往下挪44px,这种做 ...

  6. [Python3网络爬虫开发实战] 1.9.5-Scrapyrt的安装

    Scrapyrt为Scrapy提供了一个调度的HTTP接口,有了它,我们就不需要再执行Scrapy命令而是通过请求一个HTTP接口来调度Scrapy任务了.Scrapyrt比Scrapyd更轻量,如果 ...

  7. Qt 编写应用支持多语言版本--一个GUI应用示例

    简介 上一篇博文已经说过如何编写支持多语言的Qt 命令行应用,这一篇说说Qt GUI 应用多语言支持的坑. 本人喜欢用代码来写布局,而不是用 Qt Designer 来设计布局,手写布局比 Qt De ...

  8. 精帖转载(关于stock problem)

    Note: this is a repost(重新投寄) of my original post here with updated solutions(解决方案) for this problem ...

  9. 商业研究(20):滴滴出行,进军海外包车?与OTA携程和包车创业公司,共演“三国杀”?看看分析师、投资人和权威人士等10个人的观点碰撞

     小雷友情提示:创业有风险,投资需谨慎.      前一篇文章,在探讨境外游创业公司-皇包车和易途8的时候,提到"滴滴如果进军海外包车,为海外华人提供打车和包车服务,有较大可能对海外包车公司 ...

  10. git-svn 简易 操作指南

    git-svn 简易 操作指南 本文用以为使用svn的用户提供git操作指导,方便使用git管理用户自己的 本地修改 1:下载 库 下载全部历史记录 git svn clone svn://fhnws ...