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