一个简单的iBatis入门例子,用ORACLE和Java测试
目录结构:
1.导入iBatis和oracle驱动。
2.创建类Person.java
package com.ibeats;
import java.util.Date;
public class Person {
private int id;
private String firstName;
private String lastName;
private double weightInKilograms;
private double heightInMeters;
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 double getWeightInKilograms() {
return weightInKilograms;
}
public void setWeightInKilograms(double weightInKilograms) {
this.weightInKilograms = weightInKilograms;
}
public double getHeightInMeters() {
return heightInMeters;
}
public void setHeightInMeters(double heightInMeters) {
this.heightInMeters = heightInMeters;
}
}
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>
<properties resource="com/ibeats/db.properties" />
<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="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${user}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/ibeats/Person.xml" />
</sqlMapConfig>
(文件元素说明:
resource:属性列表配置文件,以便用于数据库连接参数设置。
settings:
cacheModelsEnabled:数据高速缓存,提高程序性能,利用LRU(最近最久未使用)方法对使用过的数据保存在内存中。默认true
enhancementEnabled:指定是否用cglib中那些已优化的类来提高延迟加载的性能。默认ture
lazyLoadingEnabled:延迟加载,除非绝对需要,否则推迟加载的技术。默认true
maxRequests(已废弃):一次最多有多少个请求,默认为512
maxSessions(已废弃):任何时候只允许会话数,默认128
maxTransactions(已废弃):事务最大数目,默认32
useStatementNamespaces:在引用已映射语句时,是否需要使用限定名。默认false
transactionManager:处理所有的数据库事务。
dataSoutce:数据源工厂
property:配置项
sqlMap:配置SQLMAP文件
typeAlias:定义别名)
4.配置文件person.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="Person">
<!-- Use primitive wrapper type (e.g. Integer) as parameter and allow results to
be auto-mapped results to Person object (Java Bean) properties -->
<select id="getPerson" parameterClass="int" resultClass="com.ibeats.Person">
SELECT PER_ID as id,
PER_FIRST_NAME as firstName,
PER_LAST_NAME as lastName,
PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON
WHERE PER_ID = #id#
</select>
</sqlMap>
(文件元素说明:
parameterClass:传入参数
resultClass:传出参数
)
5.数据库配置文件db.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORACLEXC
user=scott
password=tiger
6.测试类SqlMapClient.java
package com.ibeats;
import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.*;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
public class SqlMapClient {
public static void main(String args[]) {
try {
com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
String resource = "com/ibeats/SQLMapConfig.xml";
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
Person person =(Person)sqlMap.queryForObject("getPerson",new Integer(1));
System.out.println(person.getFirstName()); //打印
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e1){
e1.printStackTrace();
}
}
}
(说明:
queryForObject():获取一条记录。
queryForList():返回一行或多行,可用参数返回固定几行,可用分页。
)
大家只要改下数据库代码就可以在自己机器上运行了!