Ⅰ 、Ibatis项目机构
打开资源包,可以看到里面有一个simple_exzample的文件夹,在MyEclipse8.5中新建一个JAVA项目,将刚才的文件夹中内容复制到项目SRC下,这样的话呢,可以看到这样一个目录
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOgAAACVCAIAAADddyEwAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAUDklEQVR4nO2dfZAU5Z3Hv727CrO7CClesocEEyMUolIYc96SkshpqUjKiEnFXE7Al9xJFMvyIJYi0fhyKvG1KrHqXE8TRCIqMSDKS11dEPQ8OAsSRItoIHHZZdllZ3GHZWFne/p5nvujl+aZ53m6p6dnpnt65vnU1NDP08/veZ6Z/u2vf/30dxqDMba0ZePjC+dAwq3+fx77h7//8S0A0ql0Op1Op1Ijz/rur37y/Vt/vqy7o+e5X61/ePkt3R09615suX/zIdlcoymcOvsfQizDGKpiDADLmINuNoOwMHw6gOFNGA4AeOjGexPAqv9Y/5cDPQAeuPfXk88aA2DhpRe1bNtVyvlrqpQa+59MxjRN0zQH7dfx/qNfJDtdjUwg3cW/Hln9GwB3tvz04eW3AHhu8/J5t30XgNJrDQ6nyG8IzYr4aTUVg2GnCvfPv6ShYSS/4/N9e1r+0P74wjlvvv7C9394K/8OIPnhioH9GxPnzBl78U2/XzLdY4DvPb1bHNIwGGNCDQC+0mkjN9ZowKUKJJXqISTDKKOUDA6cSJ84Zu+yPZV/BzCwf+PEHz7Q9vrDuPim79x5m7LrVCr1wcrVcj1jTPZUN+/UXqtRMuS4fb3J9MAJK2MyRhljVsYkVsbNJrnt6S9fdN5151/w2roHk9ueGXvp4sHPfiG0SaXxwcrVcri10dFUUyBDjvtF8lB/X69lmgwMAGOstrYOaFDaDHT83xlN553c3gEA6TTfIIXhHl4rpK1OgisEYKeZdm6NTB2A5qkTX93ZBgwDhvH7mqdOlA3a3lnaeGYDgIfv/jaAxvEN7e8sHfeVUw1SGOXhtZAcUScJmgDkfbJue/X6CTM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slVERfm9XVkRv4jrKNAB+FGgA8jQGosxQmHRUyHXIiCMUspMd8eV8dCkyzWCuwgbfBuoJD5KhAbKsYSBZEV8BRC/iEsISaV6CMmMHv133YfbbQW6vcuePP9uk6EGIQwAuANHGDMYGBiRllZ43AKh7GGRxDM3d5SfoRvuvEpOzFYVAPT1JtMDJ6yMCaCnq93KmMTKeJtYlkEYO3Ds+CvvfupUEgIYzGAwPX9lKZyCyw1BHAyVm8ptKoD4RVxHgb5t8+pk5wHmQ4FushpbrfvJnrbuVO/l0yYvveESQhkMgBmWS8RVHn7lyd2tRhnzBHE6vyEnpn4yB7lDZId/tzaxJmYRN6gC3aAU4xMNS2+aCWBq0xjCwACDAgZ1W1UQjr1yu5AaZb33QH56yGv0+BKziHvtzPM91mvdsKj9P6EaU8aNBobWdAFQAAzU86cT5UO8nhheamIWcYNBmfHi41fZx/3kE8lh2AWgNiaZn/ZXngrJeDTVRvzWcTUaxFpIrqlm/h+FS8nmZ1z2xAAAAABJRU5ErkJggg==" alt="" width="357" height="229" />
Ⅱ、Ibatis的SQL语句配置类MySqlMapConfig.xml
<span style="font-size: medium;"><?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>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1:3306/ibatis"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/mydomain/data/Employee.xml"/> </sqlMapConfig>
</span>
使用的是MySQL数据库,所以要添加数据库驱动包,还有ibatis核心包,在看看数据库表该怎么建立,这得查看下Employee.xml了
Ⅲ、Employee.xml
<span style="font-size: medium;"><?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 namespace="Employee"> <typeAlias alias="Employee" type="com.mydomain.domain.Employee"/> <resultMap id="AccountResult" class="Employee">
<result property="id" column="ACC_ID"/>
<result property="firstName" column="ACC_FIRST_NAME"/>
<result property="lastName" column="ACC_LAST_NAME"/>
<result property="emailAddress" column="ACC_EMAIL"/>
</resultMap> <select id="selectAllAccounts" resultMap="AccountResult">
select * from Employee
</select> <select id="selectAccountById" parameterClass="int" resultClass="Employee">
select
ACC_ID as id,
ACC_FIRST_NAME as firstName,
ACC_LAST_NAME as lastName,
ACC_EMAIL as emailAddress
from Employee
where ACC_ID = #id#
</select> <insert id="insertAccount" parameterClass="Employee">
insert into Employee (
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
)values (
#firstName#, #lastName#, #emailAddress#
)
</insert> <update id="updateAccount" parameterClass="Employee">
update Employee set
ACC_FIRST_NAME = #firstName#,
ACC_LAST_NAME = #lastName#,
ACC_EMAIL = #emailAddress#
where
ACC_ID = #id#
</update> <delete id="deleteAccountById" parameterClass="int">
delete from Employee where ACC_ID = #id#
</delete> </sqlMap></span>
上面的<ResultMap>标签中有指定每个Account属性对应的数据库的列名,所以就新建数据库了
这样数据建立完成后,我们就可以测试了,这回就用到了SimpleExample.java类了
Ⅳ、SimpleExample.java
package com.mydomain.data; import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.mydomain.domain.Account; import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException; public class SimpleExample { private static SqlMapClient sqlMapper; static {
try {
Reader reader = Resources.getResourceAsReader("com/mydomain/data/MySqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
}
} public static List selectAllAccounts () throws SQLException {
return sqlMapper.queryForList("selectAllAccounts");
} public static Account selectAccountById (int id) throws SQLException {
return (Account) sqlMapper.queryForObject("selectAccountById", id);
} public static void insertAccount (Account account) throws SQLException {
sqlMapper.insert("insertAccount", account);
} public static void updateAccount (Account account) throws SQLException {
sqlMapper.update("updateAccount", account);
} public static void deleteAccount (int id) throws SQLException {
sqlMapper.delete("deleteAccountById", id);
} }
像上面的增删改查中用到的代替SQL语句的映射KEY,也是可以加上相应的XML文件的配置名称的,如:return sqlMapper.queryForList("Employee.selectAllAccounts")那么这个Account就是Account.xml的名称了
真正想看到测试结果得另写个JUNIT测试类,添加junit的jar包,这里我写了MyTest.java
Ⅴ、MyTest.java
package com.mydomain.data;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import org.junit.Test; import com.mydomain.domain.Account; public class MyTest {
@Test
public void selectAllAccounts(){
try {
List list=SimpleExample.selectAllAccounts();
System.out.println(Arrays.toString(list.toArray()));
} catch (SQLException e) {
e.printStackTrace();
}
} public void selectAccountById(){
try {
Account account=SimpleExample.selectAccountById(1);
System.out.println(account);
} catch (SQLException e) {
e.printStackTrace();
}
} public void insertAccount(){
Account account=new Account();
account.setFirstName("tom");
account.setLastName("jam");
account.setEmailAddress("china");
try {
SimpleExample.insertAccount(account);
} catch (SQLException e) {
e.printStackTrace();
}
} public void updateAccount(){
try {
Account account=SimpleExample.selectAccountById(2);
account.setFirstName("gates");
SimpleExample.updateAccount(account);
} catch (SQLException e) {
e.printStackTrace();
}
} public void deleteAccount(){
try {
SimpleExample.deleteAccount(1);
} catch (SQLException e) {
e.printStackTrace();
}
} }
Ⅵ、顺便介绍下MyBatis
MyBatis来源于iBATIS,iBATIS是一个由Clinton Begin在2001年发起的开放源代码项目,iBATIS一词来源于“internet”和“abatis”的组合。该项目最初侧重于密码软件的开发,现在是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO),它是著名的ORM开发框架,分为Java和.NET版本,有着众多的追随者。
iBATIS更名为MyBatis并迁移到Google Code,此次项目迁移后,将启用新的网站http://www.mybatis.org/,由于目前只是改了名字,因此仍然可直接浏览iBatis的文档。
MyBatis开发团队希望脱离Apache而独立发展,并保证不会修改授权协议(Apache License)、代码完全兼容、包名不会更改、也不会删除 Apache站上的任何相关资源。改名后的第一次版本MyBatis 3.0.1已经发布,基于iBatis 3.0版本,该版本非常稳定,已经有很多用户使用了数周时间,修复了一些小bug。欲下载 MyBatis 3.0.1请到它新的网站http://www.mybatis.org/。 目前版本是3.0.2