<select id="findAllEmployee" resultMap="rm"> <![CDATA[ SELECT e._id, e._name, e._sex, e._hired_date FROM t_emp AS e LEFT OUTER JOIN t_dep AS d ON e._dep_id = d._id ]]> </select>
<select id="findEmployeeById" resultMap="rm"> <![CDATA[ SELECT _id, _name, _sex, _hired_date FROM t_emp WHERE _id = #id# ]]> </select>
<select id="findEmployeeByName" resultMap="rm"> <![CDATA[ SELECT _id, _name, _sex, _hired_date FROM t_emp WHERE _name LIKE '%$name$%' ]]> </select> </sqlMap>
<?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.xml
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
useStatementNamespaces="true"
/>
<sqlMap resource="com/csdn/config/Employee.map.xml" />
</sqlMapConfig>
驱动和url配置在Spring的数据源里。
一个映射文件的例子 Employee.map.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>
<typeAlias alias="Employee" type="com.tarena.ibatis.domain.Employee"/>
<typeAlias alias="Department" type="com.tarena.ibatis.domain.Department"/>
<resultMap class="Employee" id="rm">
<result property="id" column="_id"/>
<result property="name" column="_name"/>
<result property="sex" column="_sex"/>
<result property="hiredDate" column="_hired_date"/>
</resultMap>
<resultMap class="Department" id="rm2">
<result property="id" column="_id"/>
<result property="name" column="_name"/>
<result property="location" column="_location" />
</resultMap>
<delete id="deleteEmployee" parameterClass="Employee">
<![CDATA[
DELETE FROM
t_emp
WHERE
_id = #id#
]]>
</delete>
<update id="updateEmployee" parameterClass="Employee">
<![CDATA[
UPDATE
t_emp
SET
_name = #name#,
_sex = #sex#,
_hired_date = #hiredDate#
WHERE
_id = #id#
]]>
</update>
<insert id="saveEmployee" parameterClass="Employee">
<![CDATA[
INSERT INTO
t_emp (_name, _sex, _hired_date)
VALUES
(
#name#, #sex#, #hiredDate#
)
]]>
</insert>
<select id="findAllEmployee" resultMap="rm">
<![CDATA[
SELECT
e._id, e._name, e._sex, e._hired_date
FROM
t_emp AS e
LEFT OUTER JOIN
t_dep AS d
ON
e._dep_id = d._id
]]>
</select>
<select id="findEmployeeById" resultMap="rm">
<![CDATA[
SELECT
_id, _name, _sex, _hired_date
FROM
t_emp
WHERE
_id = #id#
]]>
</select>
<select id="findEmployeeByName" resultMap="rm">
<![CDATA[
SELECT
_id, _name, _sex, _hired_date
FROM
t_emp
WHERE
_name LIKE '%$name$%'
]]>
</select>
</sqlMap>
TransactionManager 可以帮你简化事务管理。
DataSource 可以帮你轻松运用JDBC连接池。你谷歌一下"Spring ibatis" 就行了。 网上别人的文章多得是啊。