zoukankan      html  css  js  c++  java
  • 【转载】Mybatis多参数查询映射

    转载地址:http://www.07net01.com/zhishi/402787.html

    最近在做一个Mybatis的项目,由于是接触不久,虽然看了一下资料,但在实际开发中还是暴
    露了很多问题,其中最让我头疼的就是selete的parameterType问题,网上这类的帖子虽然有
    但是不全,很多情况下很难找到你想要的答案。
    为此我对这个问题进行了总结,希望对像我这样的新手有一定的帮助。
    (一)单个参数
    public List<XXBean> getXXBeanList(String xxCode);
    <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">
    select 字段... from XXX where code = #{xxCode}
    </select>
    其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是
    采用的短名字, select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补
    充。
    (二)多参数
    public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
    <select id="getXXXBeanList" resultType="XXBean">
    select 字段... from XXX where id=#{0} code = #{1}
    </select>
    由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索
    引,索引从0开始
    (三)Map封装多参数
    public List<XXXBean> getXXXBeanList(HashMap map);
    <select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
    select 字段... from XXX where id=#{xxId} code = #{xxCode}
    </select>
    其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那
    个,map如何封装就不用了我说了吧。
    (四)List封装IN
    public List<XXXBean> getXXXBeanList(List<String> list);13-6-9 Mybatis多参数查询映射
    www.360doc.com/content/12/1029/09/7471983_244390770.shtml 2/2
    <select id="getXXXBeanList" resultType="XXBean">
    select 字段... from XXX where id in
    <foreach item="item" index="index" collection="list"
    open="(" separator="," close=")">
    #{item}
    </foreach>
    </select>
    foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')
    (五)多参数传递之注解方式示
    @Param("xxx") int xxx

  • 相关阅读:
    PBRT笔记(3)——KD树
    PBRT笔记(2)——BVH
    PBRT笔记(1)——主循环、浮点误差
    《Ray Tracing in One Weekend》、《Ray Tracing from the Ground Up》读后感以及光线追踪学习推荐
    在Node.js中使用ffi调用dll
    Node.js c++ 扩展之HelloWorld
    在Qt中配置TBB以及简单实用
    对《将Unreal4打包后的工程嵌入到Qt或者桌面中》一文的补充
    QtQuick大坑笔记之Http的Get与Post操作(带cookie)
    QtQuick自定义主题以及控件样式指引
  • 原文地址:https://www.cnblogs.com/sdjnzqr/p/4270180.html
Copyright © 2011-2022 走看看