2019/12/15:黑马Mybatis学习笔记(四)—— Mybatis中的动态SQL语句

在映射配置文件IUserDao.xml中的sql语句中可以使用以下几个标签

标签

    /**
     * 根据传入参数条件查询
     * @param user 查询的条件:可能是用户名,可能是性别等
     * @return
     */
    List<User> findUserByCondition(User user);
    
    <select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user where 1 = 1
        <if test="userName != null">
            and username=#{userName}
        if>
    select>
    /**
     * 测试根据传入参数查询
     */
    @Test
    public void findUserByCondition(){
        User user = new User();
        user.setUserName("小马宝莉");
        List<User> users = userDao.findUserByCondition(user);
        for (User user1 : users) {
            System.out.println(user1);
        }
    }

如果还有条件就继续加if标签,如:


    <select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user where 1 = 1
        <if test="userName != null">
            and username=#{userName}
        if>
        <if test="userSex != null">
            and sex = #{userSex}
        if>
    select>

标签

上面的条件查询可以把标签内容嵌入标签中,这样就可以不用"where = 1”条件了


    <select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user
        <where>
            <if test="userName != null">
                and username=#{userName}
            if>
            <if test="userSex != null">
                and sex = #{userSex}
            if>
        where>
    select>

标签

使用了查询条件类QueryVo

package com.itheima.domain;

import java.util.List;

public class QueryVo {

    private User user;
    private List<Integer> ids;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

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

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

    /**
     * 根据QueryVo中提供的id集合查询用户信息
     * @param vo
     * @return
     */
    List<User> findUserInIds(QueryVo vo);

    <select id="findUserInIds" resultMap="userMap" parameterType="QueryVo">
        select * from user
        <where>
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="and id in(" close=")" item="id" separator=",">
                    #{id}
                foreach>
            if>
        where>
    select>
/**
     * 根据QueryVo中提供的id集合查询用户信息
     */
    @Test
    public void findUserInIds(){
        QueryVo vo = new QueryVo();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(43);
        ids.add(45);
        ids.add(46);
        vo.setIds(ids);

        List<User> users = userDao.findUserInIds(vo);
        for (User user : users) {
            System.out.println(user);
        }
    }

2019/12/15:黑马Mybatis学习笔记(四)—— Mybatis中的动态SQL语句_第1张图片

标签

sql标签可用于抽取重复sql语句
定义:


    <sql id="defaultUser">
        select * from user
    sql>

在查询所有中使用

    
    <select id="findAll" resultMap="userMap">
        <include refid="defaultUser">include>
    select>

你可能感兴趣的:(Mybatis)