在进行模糊查询时,如果是 mysql 数据库,我们常常用‘ %’ 和参数相连接;如果是 oracle 数据库,则使用连接符号 ‘||’ 。这样,就需要两套 sql 语句。而 mybatis 提供了 bind 元素,使用它,我们就不必写两套 sql,只需使用 mybatis 的语言与所需参数连接即可
此处的 pattern_bookName ,即 %bookName %
注意: 如果只有一个模糊查询,则只写一个
以下 java 代码使用 springboot 框架
package com.demo.bean;
public class Book {
private Integer id;
private String bookName;
private String bookAuthor;
private String createDate;
private String updateDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getUpdateDate() {
return updateDate;
}
public void setUpdateDate(String updateDate) {
this.updateDate = updateDate;
}
}
package com.demo.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import com.demo.bean.Book;
@Mapper
public interface BookMapper {
//添加数据
int insert(Book book);
//模糊查询
List selectBookByName(Map map);
}
book_name as bookName, book_author as bookAuthor,
create_date as createDate, update_date as updateDate
insert into book(book_name, book_author, create_date, update_date)
values(#{bookName}, #{bookAuthor}, #{createDate}, #{updateDate})
package com.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.demo.bean.Book;
import com.demo.mapper.BookMapper;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJspApplicationTests {
@Autowired
private BookMapper bookMapper;
@Test
public void contextLoads() {
Book book = new Book();
book.setBookName("隋唐演义");
book.setBookAuthor("褚人获");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
book.setCreateDate(sdf.format(new Date()));
book.setUpdateDate(sdf.format(new Date()));
bookMapper.insert(book);
System.out.println("返回的主键: "+book.getId());
}
@Test
public void query() {
Map map = new HashMap();
map.put("bookName", "经");
map.put("bookAuthor", "老");
List list = bookMapper.selectBookByName(map);
for(Book b : list) {
System.out.println(b.getBookName());
}
}
}
注意:此处 mybatis 使用 map 传参,如果map只有键没有值,则传参空字符串,即 map.put("bookAuthor", "") ,否则报空异常
除了使用 bind 元素实现模糊查询外,还可以使用 mysql 的 INSTR 函数来实现模糊查询,用法如下
还使用上边的代码
mapper层添加接口
package com.demo.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import com.demo.bean.Book;
@Mapper
public interface BookMapper {
//添加数据
int insert(Book book);
//模糊查询
List selectBookByName(Map map);
//模糊查询
List selectBookByNameINSTR(Map map);
}
测试类添加方法
@Test
public void queryINSTR() {
Map map = new HashMap();
map.put("bookName", "经");
map.put("bookAuthor", "");
List list = bookMapper.selectBookByNameINSTR(map);
for(Book b : list) {
System.out.println(b.getBookName());
}
}
运行效果如下