iBatis快速入门手札

    iBatis是一种优秀的对象与关系数据库据映射框架(O/R mapper framework),简易性是iBatis最大的优点,开发人员可以自己定义查询数据库的sql语句,通过xml将这些sql语句所得到的结果和java中的类的对象对应起来。下面是学习iBatis快速入门的一些笔记,里面的一些文件名称是和我Test项目中的名称一一对应的。

    iBatis利用xml将sql语句和java中的类的对象通过映射的关系对应起来,在iBatis中的xml文件共分为两类一类是sqlMap的配置文件sqlMapConfigExample.xml,一个是sqlMap的映射文件person.xml。在介绍这两个xml文件之前,我们先介绍下数据库中person表的定义,以及工程中Person类的定义。

person表的定义:

create table person(per_id numeric(5,0) not null,

per_first_name varchar(40) not null,

per_last_name varchar(40) not null,

per_birth_date datetime,

per_weight_kg numeric(4,2) not null,

per_height_m numeric(4,2) not null,

primary key(per_id)

)

Person类的定义:

public class Person {

private int id;

private String firstName;

private String lastName;

private Date birthDate;

private double weightInKilograms;

private double heightInMeters;

        setters and getters.....

        toString....

}

sqlMap的配置文件sqlMapConfigExample.xml主要用来配置jdbc datasource,sqlMap等属性具体的内容如下

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"

  "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

<properties resource="com/example/ibatis/sqlMapConfig.properties"/>

<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5"/>

<typeAlias alias="order" type="testdomain.Order"></typeAlias>

<transactionManager type="JDBC">

<dataSource type="SIMPLE">

<property name="JDBC.Driver" value="${driver}"></property>

<property name="JDBC.ConnectionURL" value="${url}"></property>

<property name="JDBC.Username" value="${username}"></property>

<property name="JDBC.Password" value="${password}"></property>

</dataSource>

</transactionManager>

<sqlMap resource="com/example/ibatis/person.xml"/>

</sqlMapConfig>

sqlMap的映射文件person.xml主要是将sql语句和参数对象和结果对象进行映射,具体内容如下

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"

  "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace = "Person">

<select id = "getPerson" parameterClass="java.lang.Integer" resultClass="com.example.ibatis.Person" >

   select per_id as id ,per_first_name as firstName,per_last_name as lastName,

   per_birth_date as birthDate ,per_weight_kg as weightInKilograms ,

   per_height_m as heightInMeters from person where per_id = #value#

</select>

<insert id = "insertPerson" parameterClass="com.example.ibatis.Person" >

insert into person(per_id,per_first_name,per_last_name,per_birth_date,per_weight_kg,per_height_m)

values(#id#,#firstName#,#lastName#,#birthDate#,#weightInKilograms#,#heightInMeters#)

</insert>

<update id = "updatePerson" parameterClass="com.example.ibatis.Person">

update person set per_first_name =#firstName# ,per_last_name=#lastName#,

per_birth_date = #birthDate#,per_weight_kg = #weightInKilograms#,per_height_m=#heightInMeters# where per_id =#id#

</update>

<delete id = "deletePerson" parameterClass="com.example.ibatis.Person">

delete from person where per_id=#id#

</delete>

</sqlMap>

注意:文件中DOCTYPE不要删除,而且sqlMapConfig的DOCTYPE内容和sqlMap的DOCTYPE内容不同,一个sqlMapconfig一个是sqlMap。

在sqlMapConfigExample.xml中指定了一个properties,在这个文件中主要存储了连接数据库所用到的driver,以及数据库的url,用户名以及密码,sqlmap则指向sqlMap的映射文件。

sqlMapConfig.properties内容如下:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/mysql

username=root

password=123456

在这里使用了mysql的jdbc驱动,数据库名称也叫mysql

sqlMap的映射文件person.xml中显示了sql语句与java类的映射形式,其中java类的属性使用##括起来放入到sql语句中执行。

完成sqlMap的配置文件以及映射文件则可以在sqlMap框架下编程,实现应用了。为了简化工作,我们使用了sqlMap框架中提供的Resources类来获得SqlMapClient的实例对象,实现的代码如下所示:

String resource = "com/example/ibatis/SqlMapConfigExample.xml";//sqlMapConfigExample.xml 路径

Reader reader = Resources.getResourceAsReader(resource);

sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);//获得SqlMapClient的一个实例

使用sqlMap查询 删除 更新 插入等sql操作,具体实现参照下列工程中的代码。

参考书:iBatis SQL Maps 入门教程

iBatis 入门教程 Test代码

猜你喜欢

转载自fangcraft.iteye.com/blog/1207925