iBATIS它是又一个O/R Mapping解决方案,和Hibernate相比,iBATIS最大的特点就是小巧,上手很快。如果你不需要太多复杂的功能,iBATIS是能满足你的要求又足够灵活的最简单的解决方案。
我们先建一个表Account,包括字段
id
firstname;
lastname;
emailaddress;
随便填入一些数据。然后编写iBatis必须的配置文件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>
<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/myweb"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="123456"/>
</dataSource>
</transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="Account.xml"/>
<!-- List more here...
<sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/>
-->
</sqlMapConfig>
然后注意到这个配置文件还引用了一个Account.xml,iBatis把每个需要O/R Mapping的Java对象关联到一个xml配置文件,我们需要把一个Account表映射到一个Account类:
package demo;
public class Account {
private int id;
private String firstName;
private String lastName;
private String emailAddress;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
Account.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 namespace="Account">
<!-- 使用类型的别名,以避免每次输入的完整类名. -->
<typeAlias alias="Account" type="demo.Account"/>
<resultMap id="AccountResult" class="Account">
<result property="id" column="ID"/>
<result property="firstName" column="FIRST_NAME"/>
<result property="lastName" column="LAST_NAME"/>
<result property="emailAddress" column="EMAIL"/>
</resultMap>
<!-- 使用不带参数的选择Result Map Account类. -->
<select id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>
<!-- 一个简单的选择没有Result Map的例子。注意
别名来匹配目标结果类的属性. -->
<select id="selectAccountById" parameterClass="int" resultClass="Account">
select
ID as id,
FIRST_NAME as firstName,
LAST_NAME as lastName,
EMAIL as emailAddress
from ACCOUNT
where ID = #id#
</select>
<!-- 插入一条数据参数是Account类 -->
<insert id="insertAccount" parameterClass="Account">
insert into account(id,first_name,last_Name,email) values(#id#,#firstName#,#lastName#,#emailAddress#)
</insert>
<!-- 跟新数据,参数是Account类 -->
<update id="updateAccount" parameterClass="Account">
update ACCOUNT set
FIRST_NAME = #firstName#,
LAST_NAME = #lastName#,
EMAIL = #emailAddress#
where
ID = #id#
</update>
<!-- 删除,int类型参数 -->
<delete id="deleteAccountById" parameterClass="int">
delete from ACCOUNT where ID = #id#
</delete>
</sqlMap>
SimpleExample测试类:
package demo;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
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("SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// Fail fast.
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("deleteAccount", id);
}
}
Test.java
package demo;
import java.sql.SQLException;
public class Test01 {
public static void main(String[] args) {
Account account = new Account();
account.setId(3);
account.setFirstName("xiao");
account.setLastName("liu");
account.setEmailAddress("abc@qq.com");
try {
SimpleExample.insertAccount(account);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Account ac = SimpleExample.selectAccountById(1);
System.out.println("id:"+ac.getId());
System.out.println("firstName:"+ac.getFirstName());
System.out.println("lastName"+ac.getLastName());
System.out.println("email:"+ac.getEmailAddress());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}