本人是刚学java的萌新,最近才刚刚接触spring和mybatis,也是第一次写博客,技术和语言描述上如果有出现毛病,还请大家多多指教。
做数据库课设的时候用mybatis来进行数据库操作,用mysql存储数据,为了实现通过关键字搜索查找书籍,用mybatis的注解方式实现模糊查询,先在下面贴上运行成功的例子
先在这里放上运行结果
//BookMapper
@Select("select * from book where book_name like CONCAT('%',#{keyWord},'%');")
public List<Book> findByKeyWord(String keyWord);
//Book
package entity;
/**
* 图书实体类
* 该类对应数据库中的book表
*/
public class Book {
private String bookId;//书号可以是isbn
private String bookName;//书名
private String author;//作者
private String descp;//图书简介
private String kind;//图书种类
private int count;//馆藏数,馆藏数为0即不可借
private String publisher; //出版社
private String publishTime;//出版年份
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescp() {
return descp;
}
public void setDesc(String descp) {
this.descp = descp;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", author=" + author + ", descp=" + descp + ", kind="
+ kind + ", count=" + count + ", publisher=" + publisher + ", publishTime=" + publishTime + "]";
}
}
//BookService类
package service;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import entity.Book;
import mapper.BookMapper;
public class BookService {
public static BookMapper mapper;
public static SqlSession session;
/**
* 通过关键字进行模糊查询
*/
public List<Book> findByKeyWord(String keyWord){
try {
parse();
return mapper.findByKeyWord(keyWord);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close(session);
}return null;
}
public void parse() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
mapper = session.getMapper(BookMapper.class);
if(mapper==null) {
System.out.println("连接数据库失败或解析xml文件失败");
}
}
/**
* 关闭session
* @param session
*/
public void close(SqlSession session) {
session.commit();
session.close();
}
}
我在数据库中已经插入两本书的信息,分别是凉宫春日的忧郁,以及凉宫春日的烦闷。
所以以凉宫春日作为搜素的关键字。
//测试模糊查询
package test;
import service.BookService;
public class MapperTest {
public static void main(String[]args){
BookService bs=new BookService();
String keyWord="凉宫春日";
bs.findByKeyWord(keyWord);
}
}
mybatis的配置文件mybatis-config.xml放在src目录下
通过 < setting name=“mapUnderscoreToCamelCase” value=“true”/>将数据库字段名转为实体类变量名。比如字段book_name转化为bookName。
通过< setting name=“logImpl” value=“STDOUT_LOGGING” />打印查询语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis的配置文件>
<configuration>
<!-- 全局配置 -->
<settings>
<!-- 开启自动驼峰命名规则(camel case)映射,即从数据库列名 A_COLUMN 到属性名 aColumn 的类似映射 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--打印查询语句-->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<typeAliases>
<package name="entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/library_system?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="mapper.UserMapper"/>
<mapper class="mapper.UserInfoMapper"/>
<mapper class="mapper.BookMapper"/>
</mappers>
</configuration>
mysql中的concatt用来将字符串拼接起来,CONCAT(’%’,#{keyWord},’%’)即‘%#{keyWord}%’,比如:
select * from book where book_name like concat (’%’,‘凉宫春日‘,’%‘);
的查询结果等同于
select * from book where book_name like ’%凉宫春日%‘;
在啰嗦一点,”%"是定义通配符,表示0个或多个字符。
比如,使用select * from book where book_name like ’%凉宫春日%‘;
可以搜索出凉宫春日,凉宫春日的忧郁,忧郁的凉宫春日这几类结果(假如数据库有这几本书),
再者,使用select * from book where book_name like ’凉宫春日%‘;
只能查询到凉宫春日,凉宫春日的忧郁这两类结果(假如数据库有这几本书)。
这个关键字查询其实也不能说已经做好了,因为关键字可以是书名的关键字,也可以是作者姓名的关键字,出版社姓名的关键字,怎么实现这一功能,是目前又遇到的问题。