1. 程式人生 > >mybatis多條件查詢,動態sql,模糊查詢

mybatis多條件查詢,動態sql,模糊查詢

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

可以看到: