iBatis
一、iBatis 简介
iBatis 是 apache 的一个开源项目,一个 O/R Mapping 解决方案, iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能, iBatis 是能满足要求又足够灵活的,最简单的解决方案。
二、搭建环境
导入相关的 jar 包 , 包括 iBatis 的 jar 文件、数据库驱动包。
三、配置文件
配置文件包括:
1 . JDBC 连接的属性文件
SqlMap.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:coyote
username=scott
password=admin
注: SqlMap.properties 名字约定俗称,不建议使用其它名字。 这里以 Oracle 数据库为例,可以换成其他数据库相关的连接属性。
2 .总配置文件
<? 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 >
< properties resource = "com/cn/ibatis/SqlMap.properties" />
< 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/cn/ibatis/Student.xml" />
</ sqlMapConfig >
注:可以看到在总配置文件读取 JDBC 属性文件,和映射文件
3 .映射文件( map 文件)
<? 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 alias = "Student" type = "com.cn.ibatis.Student" />
<!--
select all students
id="selectAllStudent" SQL 语句的 ID 号
-->
< select id = "selectAllStudent" resultClass = "Student" >
select *
from student
</ select >
<!--
根据 ID 查询, parameterClass 代表参数类型
#sid# 表示占位符,类似 JDBC 的问号, parameterClass="int" 指明该占位符所占位置的值得类型
-->
< select id = "selectStudentById" parameterClass = "int" resultClass = "Student" >
select *
from student
where sid=#sid#
</ select >
<!--
添加学生对象,注意 value 值顺序
-->
< insert id = "insertStudent" parameterClass = "Student" >
insert into student (sid, sname, major, birth, score)
values (#sid#,#sname#,#major#,#birth#,#score#)
</ insert >
<!--
删除学生信息
-->
< delete id = "deleteStudentById" parameterClass = "int" >
delete
from student
where sid = #id#
</ delete >
<!--
修改学生对象
parameterClass="Student" 不区分大小写,可以写成 student
-->
< update id = "updateStudentById" parameterClass = "Student" >
update student
set sname=#sname#,
major=#major#,
birth=#birth#,
score=#score#
where sid=#sid#
</ update >
<!--
模糊查询
-->
< select id = "selectStudentByName" parameterClass = "String" resultClass = "Student" >
select *
from student
where sname like '%$sname$%'
</ select >
</ sqlMap >
注: id 代表其实就是 sql 的一个别称, parameterClass ,参数类型,对应具体方法参数, resultClass ,结果类型,对应方法的返回值类型。
四、读取配置
// 读取总配置文件 ,SqlMapClient 是 ibatis 运转的核心
private static SqlMapClient sqlMapClient = null ;
static {
try {
// 创建输入流以备读取对象
Reader inputStream = com.ibatis.common.resources.Resources
.getResourceAsReader ( "com/cn/ibatis/SqlMapConfig.xml" );
sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient (inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
五、CRUD
CRUD(create,retrieve,update,delete) :增删改查
借助 SqlMapClient 中的方法: queryForObject 、 queryForList 、 insert 、 delete 、 update
六、模糊查询
模糊查询中注意一点,在映射文件中,占位符需要用 $
< select id = "selectStudentByName" parameterClass = "String"
resultClass = "Student" >
select *
from student
where sname like '%$sname$%'
</ select >
七、自动主键
主键一般由数据库序列控制,外部程序控制主键生成会带来一些列的问题,如果外部程序插入的是相同的逐渐,程序就报错了。
创建一个序列
create sequence studentPKSequence start with 1 increment by 1
/
从虚表中查询序列
select studentPKSequence.nextVal from dual
/
<!--
使用序列自动生成主键功能
-->
< insert id = "insertStudentBySequence" parameterClass = "Student" >
< selectKey resultClass = "int" keyProperty = "sid" >
select studentPKSequence.nextVal
from dual
</ selectKey >
insert into student (sid, sname, major, birth, score)
values (#sid#,#sname#,#major#,#birth#,#score#)
</ insert >
八、优缺点
1 .优点
减少了 61% 的代码量,简单,性能提高, SQL 语句和 JAVA 代码分离,移植性增强
2 .缺点
SQL 语句需要自己写,参数只能有一个