IBatisUtils .java
package dao;
import java.io.IOException;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class IBatisUtils {
private static SqlMapClient sqlMapClient;
static{
try {
Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
throw new RuntimeException("在构件SqlMapClient实例的时候发生了异常!" + e, e);
}
}
public static SqlMapClient getSqlMapClient(){
return sqlMapClient;
}
}
person_SqlMap.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="Person">
<!-- 定义该映射文件的Cache机制 -->
<cacheModel type="LRU" id="userCache">
<!-- 设定缓存存活的时间 -->
<flushInterval hours="24"/>
<!-- 设定指定的操作,清空缓存 -->
<flushOnExecute statement="updateUser"/>
<!-- 设定缓存的容量(对象) -->
<property name="size" value="1000"/>
</cacheModel>
<resultMap class="po.Person" id="map_person">
<result property="id" column="per_id"></result>
<result property="firstName" column="per_first_name"></result>
<result property="lastName" column="per_last_name"></result>
<result property="birthDate" column="per_birth_date"></result>
<result property="weightInKilograms" column="per_column_kg"></result>
<result property="heightInMeters" column="per_height_m"></result>
</resultMap>
<select id="selectAllPersons" resultClass="po.Person">
SELECT * FROM ACCOUNT
</select>
<select id="getPersonById" parameterClass="Integer" resultClass="po.Person">
SELECT PER_ID as id, PER_FIRST_NAME as firstName, PER_LAST_NAME
as lastName, PER_BIRTH_DATE as birthDate, PER_WEIGHT_KG as
weightInKilograms, PER_HEIGHT_M as heightInMeters FROM PERSON
WHERE PER_ID = #value#
</select>
<insert id="insertPerson" parameterClass="po.Person">
INSERT INTO PERSON VALUES (#id#, #firstName#, #lastName#,
#birthDate#, #weightInKilograms#, #heightInMeters#)
</insert>
<update id="updatePerson" parameterClass="po.Person">
UPDATE PERSON SET PER_FIRST_NAME = #firstName#, PER_LAST_NAME =
#lastName#, PER_BIRTH_DATE = #birthDate#, PER_WEIGHT_KG =
#weightInKilograms#, PER_HEIGHT_M = #heightInMeters# WHERE
PER_ID = #id#
</update>
<delete id="deletePerson" parameterClass="po.Person">
DELETE PERSON WHERE PER_ID = #id#
</delete>
</sqlMap>
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/jq115"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="3306"/>
</dataSource>
</transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="po/person_SqlMap.xml"/>
</sqlMapConfig>