iBatis并不在运行期自动生成SQL语句,具体的SQL语句由程序员编写。通过映射文件,将SQL所需的参数,以及返回的结果字段映射到指定的POJO。相对于Hibernate等"全自动"ORM而言,iBatis以SQL开发的工作量和数据库移植性上的让步,为系统设计提供了更大的自由空间。
一个简单的iBatis例子程序。
1.数据库表结构。
use sample;
DROP TABLE T_User;
CREATE TABLE T_User (
id INT NOT NULL
, name VARCHAR(20)
, sex INT
, PRIMARY KEY (id)
);
2.POJO类
package com.iBatis.sample;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private Integer sex;
public User(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
3.配置文件
SqlMapConfig.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.p6spy.engine.spy.P6SpyDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/sample"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="1234"/>
<property name="JDBC.DefaultAutoCommit" value="true" />
<property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="120000"/>
<property name="Pool.TimeToWait" value="500"/>
<property name="Pool.PingQuery" value="select 1 from ACCOUNT"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan" value="1"/>
<property name="Pool.PingConnectionsNotUsedFor" value="1"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/iBatis/sample/User.xml" />
</sqlMapConfig>
User.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="User">
<typeAlias alias="user" type="com.iBatis.sample.User" />
<select id="getUser"
parameterClass="java.lang.String"
resultClass="user">
<![CDATA[
select
name,
sex
from t_user
where name= #name#
]]>
</select>
<update id="updateUser"
parameterClass="user">
<![CDATA[
update t_user
set
name=#name#,
sex=#sex#
where id=#id#
]]>
</update>
<insert id="insertUser"
parameterClass="user">
<![CDATA[
insert into t_user (
name,
sex=)
values (
#name#,
#sex#
)
]]>
</insert>
<delete id="deleteUser"
parameterClass="java.lang.String">
<![CDATA[
delete from t_user
where id = #id#
]]>
</delete>
</sqlMap>
4.测试类
MyTest.java
package com.iBatis.sample;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import junit.framework.TestCase;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class MyTest extends TestCase{
SqlMapClient sqlMap = null;
protected void setUp(){
try {
String resource = "com/iBatis/sample/SqlMapConfig.xml";
Reader reader;
reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void testIBatis() throws Exception{
try {
sqlMap.startTransaction();
User user = new User();
user.setId(new Integer(1));
user.setName("baiyf");
user.setSex(new Integer(1));
sqlMap.update("updateUser", user);
sqlMap.commitTransaction();
}catch(SQLException e){
e.printStackTrace();
}
finally {
sqlMap.endTransaction();
}
}
}
ID指定了操作id,可以在代码中通过指定操作id来执行此节点定义的操作。
如:
sqlMap.update("updateUser", user);
posted on 2006-07-22 17:50
knowhow 阅读(474)
评论(1) 编辑 收藏 所属分类:
ORM:Hibernate及其他