第27步- 加入ibatis支持
相对Hibernate和Apache OJB 等“一站式”ORM解决方案而言,ibatis 是一种“半自动化”的ORM实现。
从http://www.ibatis.com 下载ibatis软件包。我下载的是iBATIS_DBL-2.1.6.589。解开压缩文件。把lib下的三个jar文件添加到项目中,可以新创建一个ibatis用户项目库。我们以Oracle为例,演示ibatis的ORM实现,所以新创建一个jdbc用户项目库,把classes12.jar添加到项目中。
我们先在Oracle中创建一个product表。
CREATE TABLE ORA.PRODUCT
(
id INTEGER NOT NULL primary key,
description VARCHAR2 (255),
price NUMBER (15, 2)
) tablespace users
/在本例中,我们使用Jakarta DBCP的配置方式
在src/db下创建一个核心配置文件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">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
<!-- The properties (name=value) in the file specified here can be used placeholders in this config file (e.g. “${driver}”. The file is relative to the classpath and is completely optional. -->
<properties resource="sql-map-config.properties"/>
<!-- These settings control SqlMap configuration details, primarily to do with transaction management. They are all optional (see the Developer Guide for more). -->
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false"/>
<!-- Type aliases allow you to use a shorter name for long fully qualified class names. -->
<typeAlias alias="product" type="bus.Product"/>
<!-- Configure a datasource to use with this SQL Map using SimpleDataSource. Notice the use of the properties from the above resource -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths are relative to the classpath. For now, we only have one… -->
<sqlMap resource="bus/sqlmap/Product.xml"/>
</sqlMapConfig>在WEB-INF/classes下创建一个sql-map-config.properties
# This is just a simple properties file that simplifies automated configuration
# of the SQL Maps configuration file (e.g. by Ant builds or continuous
# integration tools for different environments… etc.)
# These values can be used in any property value in the file above (e.g. “${driver}”)
# Using a properties file such as this is completely optional.
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:mynoss
username=ora
password=ora在src/bus下创建一个sqlmap文件夹,在其下面新建一个Product.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="getProduct" parameterClass="int" resultClass="bus.Product">
SELECT ID, DESCRIPTION, PRICE
FROM PRODUCT
WHERE ID = #id#
</select>
<!-- Use Person object (Java Bean) properties as parameters for insert. Each of the parameters in the #hash# symbols is a Java Beans property. -->
<insert id="insertProduct" parameterClass="bus.Product">
INSERT INTO PRODUCT (ID, DESCRIPTION, PRICE)
VALUES (#id#, #description#, #price#)
</insert>
<!-- Use Person object (Java Bean) properties as parameters for update. Each of the parameters in the #hash# symbols is a Java Beans property. -->
<update id="updateProduct" parameterClass="bus.Product">
UPDATE PRODUCT
SET DESCRIPTION = #description#,
PRICE = #price#
WHERE ID = #id#
</update>
<!-- Use Person object (Java Bean) "id" properties as parameters for delete. Each of the parameters in the #hash# symbols is a Java Beans property. -->
<delete id="deleteProduct" parameterClass="bus.Product">
DELETE PRODUCT WHERE ID = #id#
</delete>
</sqlMap>
在src/db下增加MyAppSqlConfig.java
package db;import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;import java.io.Reader;public class MyAppSqlConfig {
private static final SqlMapClient sqlMap ; static {
try {
String resource = "db/sql-map-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
// If you get an error at this point, it matters little what it was. It is going to be
// unrecoverable and we will want the app to blow up good so we are aware of the
// problem. You should always log such errors and re-throw them in such a way that
// you can be made immediately aware of the problem.
e.printStackTrace();
throw new RuntimeException("Error initializing MyAppSqlConfig class. Cause: "+e);
}
}
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}
这是获取sqlMap对象的一个公共类
相对Hibernate和Apache OJB 等“一站式”ORM解决方案而言,ibatis 是一种“半自动化”的ORM实现。
从http://www.ibatis.com 下载ibatis软件包。我下载的是iBATIS_DBL-2.1.6.589。解开压缩文件。把lib下的三个jar文件添加到项目中,可以新创建一个ibatis用户项目库。我们以Oracle为例,演示ibatis的ORM实现,所以新创建一个jdbc用户项目库,把classes12.jar添加到项目中。
我们先在Oracle中创建一个product表。
CREATE TABLE ORA.PRODUCT
(
id INTEGER NOT NULL primary key,
description VARCHAR2 (255),
price NUMBER (15, 2)
) tablespace users
/在本例中,我们使用Jakarta DBCP的配置方式
在src/db下创建一个核心配置文件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">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
<!-- The properties (name=value) in the file specified here can be used placeholders in this config file (e.g. “${driver}”. The file is relative to the classpath and is completely optional. -->
<properties resource="sql-map-config.properties"/>
<!-- These settings control SqlMap configuration details, primarily to do with transaction management. They are all optional (see the Developer Guide for more). -->
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false"/>
<!-- Type aliases allow you to use a shorter name for long fully qualified class names. -->
<typeAlias alias="product" type="bus.Product"/>
<!-- Configure a datasource to use with this SQL Map using SimpleDataSource. Notice the use of the properties from the above resource -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths are relative to the classpath. For now, we only have one… -->
<sqlMap resource="bus/sqlmap/Product.xml"/>
</sqlMapConfig>在WEB-INF/classes下创建一个sql-map-config.properties
# This is just a simple properties file that simplifies automated configuration
# of the SQL Maps configuration file (e.g. by Ant builds or continuous
# integration tools for different environments… etc.)
# These values can be used in any property value in the file above (e.g. “${driver}”)
# Using a properties file such as this is completely optional.
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:mynoss
username=ora
password=ora在src/bus下创建一个sqlmap文件夹,在其下面新建一个Product.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="getProduct" parameterClass="int" resultClass="bus.Product">
SELECT ID, DESCRIPTION, PRICE
FROM PRODUCT
WHERE ID = #id#
</select>
<!-- Use Person object (Java Bean) properties as parameters for insert. Each of the parameters in the #hash# symbols is a Java Beans property. -->
<insert id="insertProduct" parameterClass="bus.Product">
INSERT INTO PRODUCT (ID, DESCRIPTION, PRICE)
VALUES (#id#, #description#, #price#)
</insert>
<!-- Use Person object (Java Bean) properties as parameters for update. Each of the parameters in the #hash# symbols is a Java Beans property. -->
<update id="updateProduct" parameterClass="bus.Product">
UPDATE PRODUCT
SET DESCRIPTION = #description#,
PRICE = #price#
WHERE ID = #id#
</update>
<!-- Use Person object (Java Bean) "id" properties as parameters for delete. Each of the parameters in the #hash# symbols is a Java Beans property. -->
<delete id="deleteProduct" parameterClass="bus.Product">
DELETE PRODUCT WHERE ID = #id#
</delete>
</sqlMap>
在src/db下增加MyAppSqlConfig.java
package db;import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;import java.io.Reader;public class MyAppSqlConfig {
private static final SqlMapClient sqlMap ; static {
try {
String resource = "db/sql-map-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
// If you get an error at this point, it matters little what it was. It is going to be
// unrecoverable and we will want the app to blow up good so we are aware of the
// problem. You should always log such errors and re-throw them in such a way that
// you can be made immediately aware of the problem.
e.printStackTrace();
throw new RuntimeException("Error initializing MyAppSqlConfig class. Cause: "+e);
}
}
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}
这是获取sqlMap对象的一个公共类
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货