关于Mybaits

mybatis 返回多表多字段用

mybatis 返回多表多字段用

resultType=”java.util.Map”轻松解决问题。
不用加什么DTO.这样前端要什么字段就返回什么字段。不用在对多余的字段为null进行其他处理。个人亲测有效。

<select id="searchMyEvaluate" resultType="java.util.Map" parameterType="java.lang.Long">
SELECT ru.`head_image` as head_image,re.`content`,re.`evaluate_time`,re.`evaluate_score`
FROM rt_user ru
LEFT JOIN rt_order ro ON ru.`user_id` = ro.`seller_id`
LEFT JOIN rt_evaluate re ON ro.`order_id` = re.`order_id`
WHERE re.is_delete = 0
and ru.user_id = #{userId,jdbcType=BIGINT}
</select>
---------------------
作者:IT技匠
来源:CSDN
原文:https://blog.csdn.net/qq_20516587/article/details/80596045
版权声明:本文为博主原创文章,转载请附上博文链接!

1
2
3
<select id="getByIds"  parameterType="java.lang.String"  resultType="java.util.List">
    SELECT l.label_name FROM label l WHERE l.id IN(#{labelIds})
  </select>
 
1
返回值定义为 resultType="java.util.List" <br><br>会报错,需要将返回值改为:  <br>  resultType="java.lang.String"<br><br>为啥一直没搞清楚

MyBatis传入多个参数的问题

一、单个参数:

复制代码
public List<XXBean> getXXBeanList(String xxCode);  

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">

  select t.* from tableName t where t.id= #{id}  

</select>  

其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字,

select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。
复制代码

二、多参数:

复制代码
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{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);  

<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') 
复制代码

五、多参数传递之注解方式示:    

复制代码
例子:
 
public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);
 
xml配置这样写:
 
<select id="getAddrInfo"  resultMap="com.xxx.xxx.AddrInfo">
       SELECT * FROM addr__info 
    where addr_id=#{addrId} and corp_id=#{corpId} </select> 以前在<select>语句中要带parameterType的,现在可以不要这样写。
复制代码

六、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法:

将参数放入Map,再取出Map中的List遍历。如下:

复制代码
List<String> list_3 = new ArrayList<String>();
Map<String, Object> map2 = new HashMap<String, Object>();
list.add("1");
list.add("2");
map2.put("list", list); //网址id

map2.put("siteTag", "0");//网址类型
复制代码
public List<SysWeb> getSysInfo(Map<String, Object> map2) {
  return getSqlSession().selectList("sysweb.getSysInfo", map2);
}
复制代码
<select id="getSysInfo" parameterType="java.util.Map" resultType="SysWeb">
  select t.sysSiteId, t.siteName, t1.mzNum as siteTagNum, t1.mzName as siteTag, t.url, t.iconPath
   from TD_WEB_SYSSITE t
   left join TD_MZ_MZDY t1 on t1.mzNum = t.siteTag and t1.mzType = 10
   WHERE t.siteTag = #{siteTag } 
   and t.sysSiteId not in 
   <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
       #{item}
   </foreach>
 </select>
复制代码

if

EmpMapper.xml配置

<select id="getEmpByIf" resultType="Emp" parameterType="Emp">
    select * from emp where 1 = 1
    <if test="job != null and job != ''">
        and job = #{job}
    </if>
    <if test="deptno != null ">
        and deptno = #{deptno}
    </if>
</select>

choose、when、otherwise

<select id="getEmpByChoose" resultType="Emp" parameterType="Emp">
    select * from emp where 1 = 1
    <choose>
        <when test="job != null">
            and job = #{job}
        </when>
        <when test="deptno != null">
            and deptno = #{deptno}
        </when>
        <otherwise>
            and mgr = #{mgr}
        </otherwise>
    </choose>
</select>

where

<select id="getEmpByWhere" resultType="Emp" parameterType="Emp">
    select * from emp
    <where>
        <if test="job != null and job != ''">
            and job = #{job}
        </if>
        <if test="deptno != null">
            and deptno = #{deptno}
        </if>
    </where>
</select>

从配置的SQL语句及结果来看,where能在第一次满足添加条件时自动补全where这个单词,而且如果有and会替换掉,如果满足条件拼接的第二个语句没有and,会报错

set

<update id="updateEmpBySet" parameterType="Emp">
    update emp
    <set>
        <if test="ename != null and ename != ''">
            ename = #{ename},
        </if>
        <if test="job != null and job != ''">
            job = #{job},
        </if>
    </set>
    where empno = #{empno}
</update>

可以看出,set会在成功拼接的条件前加上SET单词且最后一个”,”号会被无视掉,但是有可能需要”,”的地方不能省略”,”否则异常。


MySQL带IN关键字的查询

转载自

https://www.cnblogs.com/a198720/articles/4718484.html

1. 当查询的参数只有一个时 
  findByIds(List<Long> ids)
 1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
复制代码
 
 <select id="findByIdsMap" resultMap="BaseResultMap">
         Select
         <include refid="Base_Column_List" />
         from jria where ID in
                  <foreach item="item" index="index" collection="list" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 
 
复制代码

 findByIds(Long[] ids)
 1.b 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
复制代码
 
  <select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="array" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 
 
复制代码

2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)
 这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
         下面是一个示例
复制代码
 
         Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("name", name);
         params.put("ids", ids);
        mapper.findByIdsMap(params);
 
 <select id="findByIdsMap" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="ids" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
   </select> 
 
复制代码
 
完整的示例如下:
例如有一个查询功能,Mapper接口文件定义如下方法:
List<Jria> findByIds(Long... ids);
使用 in 查询的sql拼装方法如下:
复制代码
 
 <select id="findbyIds" resultMap="BaseResultMap">
                 select
                 <include refid="Base_Column_List" />
          from jria where ID in
                  <foreach item="item" index="index" collection="array" 
                         open="(" separator="," close=")">
                        #{item}
                </foreach>
  </select> 
 
复制代码
 
 
原文地址:https://www.cnblogs.com/heroinss/p/9887430.html