首页 > 技术文章 > Mybatis中动态sql的使用 where、like

lihp-java 2021-02-23 23:05 原文

   <select id="selectStudentsByWhere" resultType="com.bjpowernode.domain.Student">
        select id,name,email,age from student
        <where>
            <if test="name!=null and name!=''">
                name like #{name}
            </if>
            <if test="age>0">
                or age &lt;  #{age}
            </if>
        </where>
    </select>
public class TestMybatis {
    @Test
    public void testselectStudentsByWhere(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setName("%王%");
        student.setAge(21);
        List<Student> list = dao.selectStudentsByWhere(student);
        for(Student student1:list){
            System.out.println(student1);
        }
    }
}

 

推荐阅读