Ibatis 学习

映射

执行SQL

parameters

<select id="selectUsers" parameterType="int" resultType="User">
       select id,username,password
       from users
       where id=#{id}
</select> 
参数类型被设置为“int”,因此这个参数可以被设置成任何内容。

<insert id="insertUser" parameterType="User">
       insert into users (id,username,password)
       values (#{id},#{username},#{password})
</insert>
User对象数据类型作为参数类型,插入。

#{property,javaType=int,jdbcType=NUMERIC}
参数指定一个确切的数据类型


resultMap

<select id="selectUsers" parameterType="int" resultType="hashmap">
      select id,username,hashedPassword
      from some_table
      where id=#{id}
</select> 

基于JavaBean规范

<select id="selectUsers" parameterType="int" resultType="com.someapp.model.User">
      select id,username,hashedPassword
      from some_table
      where id=#{id}
</select> 

MyBatis 会在幕后自动创建一个 ResultMap ,基于属性名来映射列到JavaBean 的属性上。

精确匹配

<select id="selectUsers" parameterType="int" resultType="User">
      select 
            user_id as "id"
            user_name as "userName"
            hashed_password as "hashedPassword"
      from some_table
      where id= #{id}
</select>
 
解决不匹配的另一种方式

<resultMap id="userResultMap" type="User">
       <id property="id" column="user_id"/>       
       <result property="username" column="username"/>
       <result property="password" column="password"/>
</resultMap> 
引用它的语句使用resultMap属性就可以。

<select id="selectUsers" parameterType="int" resultMap="userResultMap">
      select user_id,user_name,hashed_password
      from some_table
      where id=#{id}
</select> 

高级映射

<!-- Very Complex Statement -->
<select id="selectBlogDetails" parameterType="int"
       resultMap="detailedBlogResultMap">
       select
            B.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_name
        from Blog B
            left outer join Author A on B.author_id = A.id
            left outer join Post P on B.id = P.blog_id
            left outer join Comment C on P.id = C.post_id
            left outer join Post_Tag PT on PT.post_id = P.id
            left outer join Tag T on PT.tag_id = T.id
        where B.id = #{id}
</select>

下面是一个完整的复杂结果映射例子(假设作者,博客,博文,评论和标签都是类型的别名)

<-- very Complex Result Map -->
<resultMap id="detailBlogResultMap" type="Blog">
       <constructor>
                <idArg column="blog_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"/>
                <assiciation 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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值