ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mybatis 动态Sql

2021-06-25 17:00:07  阅读:197  来源: 互联网

标签:片段 userExtend Sql List ids sql mybatis 动态 id


一、什么是动态sql

对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装

二、where

   <select id="findUserlist"
        parameterType="com.xxx.mybatis.po.UserQueryVo"
        resultType="com.xxx.mybatis.po.UserExtend">
        select * from t_user
        <!-- where可以自动去掉条件中的第一个and -->
        <where>
            <if test="userExtend!=null">
                <if test="userExtend.sex!=null and userExtend.sex!=''">
                    and sex = #{userExtend.sex}
                </if>
                <if test="userExtend.username!=null and userExtend.username!=''">
                    and userName LIKE CONCAT('%',#{userExtend.username},'%' )
                </if>
            </if>
        </where>
    </select>

三、sql片段

其它的statement中就可以引用sql片段,方便程序员进行开发

定义sql片段

    <sql id="query_user_where">
        <if test="userExtend!=null">
            <if test="userExtend.sex!=null and userExtend.sex!=''">
                and sex = #{userExtend.sex}
            </if>
            <if test="userExtend.username!=null and userExtend.username!=''">
                and userName LIKE CONCAT('%',#{userExtend.username},'%' )
            </if>
        </if>
    </sql>

引用sql片段,如果refid指定id不在本mapper.xml文件中,需要前面加命名空间点(格式:namespace+"."+id)

        <where>
            <include refid="query_user_where" />
        </where>

sql片段不要写where

四、foreach

  向sql传递数组或List,mybatis使用foreach解析

1、在输入参数类型中添加List<Integer> ids传入多个id

public class UserQueryVo {
    private List<Integer> ids;

2、mapper.xml

<select id="findUserlist"
        parameterType="com.xxx.mybatis.po.UserQueryVo"
        resultType="com.xxx.mybatis.po.UserExtend">
        select * from t_user
        <where>
            <include refid="query_user_where"/>
            <if test="ids!=null">
                <!-- 
                collection:指定输入对象中集合属性
                item:遍历集合中项目名
                open:sql开始遍历前和别的sql语句的连接串(and / or)
                close:结束遍历时拼接的串
                separator:遍历的项目之间拼接的字符串
                 -->
                 <!-- 实现
                     and (id=1 OR id=10 OR id=16)
                  -->
                <foreach collection="ids" item="id" open="And (" close=")"
                    separator="Or">
                    id=#{id}
                </foreach>
                
                 <!-- 实现
                     and IN(1,10,16)
                  --><!--
                <foreach collection="ids" item="userid" open="And IN(" close=")"
                    separator=",">
                    #{userid}
                </foreach> -->
            </if>
        </where>
    </select>

3、测试代码

    @Test
    void testFindUserList() {
    
    UserMapper userMapper = sqlSessionFactory.openSession().getMapper(UserMapper.class);
    UserQueryVo queryVo=new UserQueryVo();
    UserExtend userExtend=new UserExtend();
    userExtend.setSex("0");
    userExtend.setUsername("小");
    
    queryVo.setUserExtend(userExtend);
    List<Integer> ids=new ArrayList<Integer>();
    ids.add(1);
    ids.add(10);
    ids.add(9);
    queryVo.setIds(ids );
    List<UserExtend> list = userMapper.findUserlist(queryVo);
    System.out.println(list);
    }

 

标签:片段,userExtend,Sql,List,ids,sql,mybatis,动态,id
来源: https://www.cnblogs.com/WarBlog/p/14931670.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有