欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis多条件查询,动态sql,模糊查询

程序员文章站 2024-03-06 08:44:37
...

mapper.xml中:

<select id="selectShareByName" resultMap="seeShare">
        SELECT * FROM Diary
        <where>
            <!--where标签自动去掉满足条件的第一个and -->
            <if test="arg0 != null and arg0 != ''">
                and fileName LIKE CONCAT('%',#{arg0 },'%' )
            </if>
            <if test="arg1 != null and arg1 != ''">
                and username = #{arg1}
            </if>
        </where>
    </select>

DAO层:

//多条件查询,动态sql
    public List<Diary> selectShareByName(String fileName,String username){
        System.out.println(fileName+username);
        List<Diary> list = new ArrayList<>();
        DiaryMapper diaryMapper = sqlSession.getMapper(DiaryMapper.class);
        list = diaryMapper.selectShareByName(fileName,username);
        return list;
    }

配置springboot+mybaits输出sql语句

在application.properties中加入:

logging.level.com.example.demo.map=debug

可以看到:

mybatis多条件查询,动态sql,模糊查询