SQL Map XML Files

  categories:资料  tags:  author:

ibatis最大的点就是可以把sql语句移到xml文件中,SQL Map XML Files包含如下几个元素:

cache:配置命名空间内的缓存机制

cache-ref:引用两一个命名空间的缓存配置

resultMap:获取数据库结果集时转化为对象的映射

sql:sql命令

insert:insert语句配置

update:update语句配置

delete:delete语句配置

select:select语句配置

select <select id=”selectPerson” parameterType=”int” resultType=”hashmap”>

SELECT * FROM PERSON WHERE ID = #{id} select> 定义了一个名称为selectPerson的方法,包含一个int类型的形参,返回一个由列名和值生成的哈希表 注意这里的#{id},实际上ibatis创建了一个PreparedStatement去处理这种参数,类似的jdbc代码如下: // Similar JDBC code, NOT iBATIS… String selectPerson = “SELECT * FROM PERSON WHERE ID=?”; PreparedStatement ps = conn.prepareStatement(selectPerson); ps.setInt(1,id);

select 标签还有很多的可配置属性:

<select id=”selectPerson” parameterType=”int” parameterMap=”deprecated” resultType=”hashmap” resultMap=”personResultMap” flushCache=”false” useCache=”true” timeout=”10000” fetchSize=”256” statementType=”PREPARED”
resultSetType=”FORWARD_ONLY”>

 

insert,update,delete
<insert
id=”insertAuthor”
parameterType=”domain.blog.Author”
flushCache=”true”
statementType=”PREPARED”
keyProperty=””
useGeneratedKeys=””
timeout=”20000″>
<update
id=”insertAuthor”
parameterType=”domain.blog.Author”
flushCache=”true”
statementType=”PREPARED”
timeout=”20000″>
<delete
id=”insertAuthor”
parameterType=”domain.blog.Author”
flushCache=”true”
statementType=”PREPARED”
timeout=”20000″>

<insert id=”insertAuthor” parameterType=”domain.blog.Author”>
insert into Author (id,username,password,email,bio)
values (#{id},#{username},#{password},#{email},#{bio})
insert>
<insert id=”insertAuthor” parameterType=”domain.blog.Author”
useGeneratedKeys=”true” keyProperty=”id”>
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
insert>
<update id=”updateAuthor” parameterType=”domain.blog.Author”>
update Author set

username = #{username},
password = #{password},
email = #{email},
bio = #{bio}
where id = #{id}
update>
<delete id=”deleteAuthor” parameterType=”int”>
delete from Author where id = #{id}
delete>

 



快乐成长 每天进步一点点      京ICP备18032580号-1