自己也是刚刚接触mybatis的,在开发过程中发现网上有很多人在问有关mybatis的模糊查询
1.新手最开始常用的方法如下:
xml:
- <select id="selectByName" parameterType="String" resultType="Student">
- select * from Student s where s.name like #{name};
- </select>
java:
- @Override
- public List<Student> findAllByName(String name) {
- Student student = new Student();
- student.setName("%李%");
- List<Student> studentList = session.selectList("selectByName", student);
- return studentList;
- }
在开发过程我遇到过这样的问题,上面的方法不好用,每次都是传一个这样的参数. 如: %参数%,不好使,用下面这种方法很好,方便
xml:
- <select id="selectByName" parameterType="String" resultType="Student">
- select * from Student s where s.name like "%"#{name}"%";
- </select>
- @Override
- public List<Student> findAllByName(String name) {
- Student student = new Student();
- student.setName(name);
- List<Student> studentList = session.selectList("selectByName", student);
- return studentList;
- }