先建一个数据库mydb, 表Account
Create database mydb
Use mydb
表Account:
create table Account(username varchar(40), password varchar(40), primary key( username) )
insert into Account
values('abc', '123456')
insert into Account
values('cba', '123456')
insert into Account
values('efg', '123456')
iBatis是又一个O/R Mapping解决方案,j2ee的O/R方案真是多,和Hibernate相比,iBatis最大的特点就是小巧,上手很快。如果你不需要太多复杂的功能,iBatis是能满足你的要求又足够灵活的最简单的解决方案。
打开ecilpse新建JAVA project: ibatisTest
新建PACKAGE: example 在其下添加类文件:Account.java
package example;
public class Account {
private String username;
private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username=username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password=password; }
}
下载ibatis 的包ibatis-2.3.4.726.jar, mysql的连接包mysql-connector-java-5.1.7-bin.jar
把两个包放到ibatisTest文件目录下,把两个包导入项目中。
接下来配置ibatis
sql-map-config.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1/mydb"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="123456"/>
</dataSource>
</transactionManager>
<sqlMap resource="Account.xml" />
</sqlMapConfig>
只需注意橙色部分,按自己需要置好数据源。
sql-map-config.xml文件引用的:Account.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Account">
<select id="getAccount" parameterClass="java.lang.String" resultClass="example.Account">
select * from Account where username = #value#
</select>
<insert id="createAccount" parameterClass="example.Account">
insert into Account (username, password)
values ( #username#, #password# )
</insert>
</sqlMap>
我们主要关心红色部分。以<select ...>为例,这里定义了一个查询方法,名字为getAccount,传入参数为String,返回类型resultClass就是example.Account类,select语句相信你已经很熟悉了,#value#是我们将要传进去的String。
<insert...>与之类似,不过注意到#username#和#password#,由于参数是Account类,它们将被Account.getUsername()和Account.getPassword()替换。所以,只要你会写SQL,就能非常容易地写出配置文件。
最后便是如何使用iBatis实现O/R映射。首先初始化iBatis获得一个SqlMapClient对象:
======================================================
com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
try {
java.io.Reader reader = om.ibatis.common.resources.Resources.getResourceAsReader ("sql-map-config.xml");
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
}
然后就可以异常方便地使用O/R Mapping了,比如查询username=admin的Account:
try {
Account accout = (Account)sqlMap.queryForObject("getAccount", "admin");
...
}
或者创建一个新的Account:
try {
Account account = new Account();
account.setUsername("micheal");
account.setPassword("1234");
sqlMap.insert("createAccount", account);
}
建一个Test.java
import example.Account;
public class Test
{
public static void main(String[] args)
{
com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
try {
java.io.Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader ("sql-map-config.xml");
sqlMap = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
}
try
{
Account accout = (Account)sqlMap.queryForObject("getAccount", "abc");//读取USERNAME = "abc"的记录
System.out.println(accout.getUsername());
System.out.println(accout.getPassword());
Account account = new Account();
account.setUsername("micheal0");
account.setPassword("1234");
sqlMap.insert("createAccount", account);//使用SQLMAP插入对像
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
运行即可.