码迷,mamicode.com
首页 > 其他好文 > 详细

[mybatis]传值和返回结果

时间:2018-12-15 17:20:21      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:ams   ike   返回结果   hash   pid   column   collect   文件中   str   

一、传值:
parameterType的形式:可以传递一个类,也可以是一个map

<update id="updateCategory" parameterType="Category" >
   update category_ set name=#{name} where id=#{id}
</update>
<select id="listCategoryByName" parameterType="string" resultType="Category">
  select * from category_ where name like concat(‘%‘,#{0},‘%‘)
</select>
<select id="listCategoryByIdAndName"  parameterType="map" resultType="Category">
   select * from   category_  where id> #{id}  and name like concat(‘%‘,#{name},‘%‘)
</select>

在代码侧的写法:

        Map<String,Object> params = new HashMap<>();
        params.put("id", 3);
        params.put("name", "cat");
        List<Category> cs = session.selectList("listCategoryByIdAndName",params);

在parameterType="string" 为基本类型时,引用可以写#{0},也可以为#{id},#{idd},中间为任意名,也可以省略parameterType不写

在parameterType="map"时,在select中可以不写parameterType

二、返回结果:

1:resultType的形式:返回的是一个类

    <select id="getCategory" parameterType="_int" resultType="Category">
        select * from   category_  where id= #{id}
    </select>

2:resultMap的形式:返回的是一个map

resultMap中的值categoryBean是该xml文件中的resultMap的id

    <resultMap type="Category" id="categoryBean">
        <id column="cid" property="id" />
        <result column="cname" property="name" />

        <!-- 一对多的关系 -->
        <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
        <collection property="products" ofType="Product">
            <id column="pid" property="id" />
            <result column="pname" property="name" />
            <result column="price" property="price" />
        </collection>
    </resultMap>

     <!--关联查询分类和产品表 -->
    <select id="listCategory" resultMap="categoryBean">
        select c.*, p.*, c.id ‘cid‘, p.id ‘pid‘, c.name ‘cname‘, p.name ‘pname‘
        from category_ c
        left join product_ p on c.id = p.cid
    </select>

 

[mybatis]传值和返回结果

标签:ams   ike   返回结果   hash   pid   column   collect   文件中   str   

原文地址:https://www.cnblogs.com/afeng2010/p/10123792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!