• [mybatis]传值和返回结果


    一、传值:
    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>
  • 相关阅读:
    MongoDB存储
    python 查看文件名和文件路径
    Python遍历文件个文件夹
    Python图片缩放
    python opencv
    Python3 关于UnicodeDecodeError/UnicodeEncodeError: ‘gbk’ codec can’t decode/encode bytes类似的文本编码问题
    jmter使用
    HttpRunnerManager使用
    PostMan使用
    工作中的思想
  • 原文地址:https://www.cnblogs.com/afeng2010/p/10123792.html
Copyright © 2020-2023  润新知