05_mybatis动态sql

1.sql片段

1.sql片段****

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

2.需求

用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。

对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

3.定义sql片段**
  <!--定义sql片段-->
    <!--id:标识sql片段-->
    <!--经验:基于单表来定义sql片段,这样可以提高可重用性,一般不要包含where-->
    <sql id="query_user_where">
        <!--where 可以去掉条件中的第一个and-->
            <if test="userCustom!=null">
                <if test="userCustom.sex!=null and userCustom.sex!=''">
                    and user.sex=#{userCustom.sex}
                </if>
                <if test="userCustom.username!=null and userCustom.username!=''">
                    and user.username like '%${userCustom.username}%'
                </if>
            </if>
    </sql>
4.引用sql片段
 <select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
        select * from user
       <where>
          <!-- 引用sql片段,如果不在同一个mapper文件中,这时要加namespace-->
           <include refid="query_user_where"/>
       </where>
    </select>

2.foreach

1.需求

在用户查询列表和查询总数的statement中增加多个id输入查询。

sql语句如下:

两种方法:

SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
<!-- 法2-->
SELECT * FROM USER WHERE id IN(1,10,16)
2.在输入参数类型中添加List ids传入多个id**
/**
 * Description: 查询时,将多个对象的属性集合在此类
 * User: jiatp
 * Date:2019/9/3 0003 下午 4:39
*/

public class UserQueryVo {
    //用户的查询条件
    private UserCustom userCustom;
    //传入多个id
    private List<Integer> ids;
    //可以包装其它信息,商品,订单 等

    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }

    public UserCustom getUserCustom() {
        return userCustom;
    }

    public void setUserCustom(UserCustom userCustom) {
        this.userCustom = userCustom;
    }
}

3.修改mapper.xml
 <!--定义sql片段-->
    <!--id:标识sql片段-->
    <!--经验:基于单表来定义sql片段,这样可以提高可重用性,一般不要包含where-->
    <sql id="query_user_where">
        <!--where 可以去掉条件中的第一个and-->
            <if test="userCustom!=null">
                <if test="userCustom.sex!=null and userCustom.sex!=''">
                    and user.sex=#{userCustom.sex}
                </if>
                <if test="userCustom.username!=null and userCustom.username!=''">
                    and user.username like '%${userCustom.username}%'
                </if>
                <if test="ids!=null">
                    <!--<foreach collection="ids" open="and ("  close=")" item="user_id" separator="or">-->
                          <!--id=#{user_id}-->
                    <!--</foreach>-->
                    <foreach collection="ids" open="and id in("  close=")" item="user_id" separator=",">
                        #{user_id}
                    </foreach>
                </if>
            </if>
    </sql>
4.测试代码
  //综合查询,用户的所有信息 foreach
    @Test
    public void findUserListForEach(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserQueryVoMapper mapper = sqlSession.getMapper(UserQueryVoMapper.class);
        //创建包装对象
        UserQueryVo userQueryVo = new UserQueryVo();
        UserCustom userCustom = new UserCustom();
        //由于设置了动态sql
         userCustom.setSex("1");
        //userCustom.setUsername("李四");
        userQueryVo.setUserCustom(userCustom);
        //加入多个id进行查询
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(10);
        ids.add(22);
        userQueryVo.setIds(ids);
        //调用查询
        List<UserCustom> userList = mapper.findUserList(userQueryVo);
        for(UserCustom us:userList) {
            System.out.println(us);
        }
    }
}

持续补充中…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值