ibatis 学习笔记 3
SQL Map XML Files
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>String selectPerson = “SELECT * FROM PERSON WHERE ID=?”;PreparedStatement ps = conn.prepareStatement(selectPerson);ps.setInt(1,id);
<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 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><insert id="insertAuthor" parameterType="domain.blog.Author"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1 selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR} )insert><sql id=”userColumns”> id,username,password sql><select id=”selectUsers” parameterType=”int” resultType=”hashmap”> select <include refid=”userColumns”/> from some_table where id = #{id}select><select id=”selectUsers” parameterType=”int” resultType=”User”> select id, username, password from users where id = #{id}select><insert id=”insertUser” parameterType=”User” > insert into users (id, username, password) values (#{id}, #{username}, #{password})insert><select id=”selectUsers” parameterType=”int” resultType=”hashmap”> select id, username, hashedPassword from some_table where id = #{id}sql>public class Student {private int id;private String name;private String major;private Date birth;private double score;}<select id="queryStudentById" parameterType="int" resultType="cn.pf.ibatis.domain.Student">select table_id as "id",table_name as "name",table_major as "major",table_birth as "birth",table_score as "score"from Student where id = #{id}select><resultMap id="studentResultMap" type="cn.pf.ibatis.domain.Student"><id property="id" column="id" /><result property="name" column="name"/><result property="major" column="major"/><result property="birth" column="birth"/><result property="score" column="score"/>resultMap><select id="queryStudentById" parameterType="int" resultMap="studentResultMap">selectid as "id",name as "name",major as "major",birth as "birth",score as "score"from Student where id = #{id}select><select id="selectBlogDetails" parameterType="int" resultMap="detailedBlogResultMap">selectB.id as blog_id,B.title as blog_title,B.author_id as blog_author_id,A.id as author_id,A.username as author_username,A.password as author_password,A.email as author_email,A.bio as author_bio,A.favourite_section as author_favourite_section,P.id as post_id,P.blog_id as post_blog_id,P.author_id as post_author_id,P.created_on as post_created_on,P.section as post_section,P.subject as post_subject,P.draft as draft,P.body as post_body,C.id as comment_id,C.post_id as comment_post_id,C.name as comment_name,C.comment as comment_text,T.id as tag_id,T.name as tag_namefrom Blog Bleft outer join Author A on B.author_id = A.idleft outer join Post P on B.id = P.blog_idleft outer join Comment C on P.id = C.post_idleft outer join Post_Tag PT on PT.post_id = P.idleft outer join Tag T on PT.tag_id = T.idwhere B.id = #{id}select><resultMap id="detailedBlogResultMap" type="Blog"><constructor><idArg column="id" javaType="int"/>constructor><result property="title" column="blog_title"/><association property="author" column="blog_author_id" javaType=" Author"><id property="id" column="author_id"/><result property="username" column="author_username"/><result property="password" column="author_password"/><result property="email" column="author_email"/><result property="bio" column="author_bio"/><result property="favouriteSection" column="author_favourite_section"/>association><collection property="posts" ofType="Post"><id property="id" column="post_id"/><result property="subject" column="post_subject"/><association property="author" column="post_author_id" javaType="Author"/><collection property="comments" column="post_id" ofType=" Comment"><id property="id" column="comment_id"/>collection><collection property="tags" column="post_id" ofType=" Tag" ><id property="id" column="tag_id"/>collection><discriminator javaType="int" column="draft"><case value="1" resultType="DraftPost"/>discriminator>collection>resultMap><id property="id" column="post_id"/><result property="subject" column="post_subject"/>
constructor:<constructor><idArg column="id" javaType="int"/><arg column=”username” javaType=”String”/>constructor>constructor属性可以配置通过类构造函数注入属性的一些参数。考虑User类这个构造函数public class User { //… public User(int id, String username) { //… } //…}<association property="author" column="blog_author_id" javaType=" Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/>association>
<resultMap id=”blogResult” type=”Blog”> <association property="author" column="blog_author_id" javaType="Author" select=”selectAuthor”/>resultMap><select id=”selectBlog” parameterType=”int” resultMap=”blogResult”> SELECT * FROM BLOG WHERE ID = #{id}select><select id=”selectAuthor” parameterType=”int” resultType="Author"> SELECT * FROM AUTHOR WHERE ID = #{id}select><resultMap id="blogResult" type="Blog"> <id property=”blog_id” column="id" /> <result property="title" column="blog_title"/> <association property="author" column="blog_author_id" javaType="Author" resultMap=”authorResult”/>resultMap><resultMap id="authorResult" type="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/>resultMap>
<resultMap id="blogResult" type="Blog"> <id property=”blog_id” column="id" /> <result property="title" column="blog_title"/> <association property="author" column="blog_author_id" javaType="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> association>resultMap>
<select id="selectBlog" parameterType="int" resultMap="blogResult">selectB.id as blog_id,B.title as blog_title,B.author_id as blog_author_id,A.id as author_id,A.username as author_username,A.password as author_password,A.email as author_email,A.bio as author_biofrom Blog B left outer join Author A on B.author_id = A.idwhere B.id = #{id}select><collection property="posts" ofType="domain.blog.Post"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <result property="body" column="post_body"/>collection>
<resultMap id=”blogResult” type=”Blog”> <collection property="posts" javaType=”ArrayList” column="blog_id" ofType="Post" select=”selectPostsForBlog”/>resultMap><select id=”selectBlog” parameterType=”int” resultMap=”blogResult”> SELECT * FROM BLOG WHERE ID = #{id}select><select id=”selectPostsForBlog” parameterType=”int” resultType="Author"> SELECT * FROM POST WHERE BLOG_ID = #{id}select><resultMap id="blogResult" type="Blog"> <id property=”id” column="blog_id" /> <result property="title" column="blog_title"/> <collection property="posts" ofType="Post"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <result property="body" column="post_body"/> collection>resultMap>
<select id="selectBlog" parameterType="int" resultMap="blogResult">selectB.id as blog_id,B.title as blog_title,B.author_id as blog_author_id,P.id as post_id,P.subject as post_subject,P.body as post_body,from Blog Bleft outer join Post P on B.id = P.blog_idwhere B.id = #{id}select>discriminator:<discriminator javaType="int" column="draft"><case value="1" resultType="DraftPost"/>discriminator><resultMap id="vehicleResult" type="Vehicle"> <id property=”id” column="id" /> <result property="vin" column="vin"/> <result property="year" column="year"/> <result property="make" column="make"/> <result property="model" column="model"/> <result property="color" column="color"/> <discriminator javaType="int" column="vehicle_type"> <case value="1" resultMap="carResult"/> <case value="2" resultMap="truckResult"/> <case value="3" resultMap="vanResult"/> <case value="4" resultMap="suvResult"/> discriminator>resultMap>
<cacheeviction="FIFO"flushInterval="60000"size="512"readOnly="true"/>