mybatis框架下——数据库的动态查询展示(可实现模糊查询)
首先要搭建MyBatis开发环境
为了展示mybatis框架下对数据库的动态查询,我们要先来搭建MyBatis开发环境(以下我将实现一个简单的小模块为例):
(我使用的工具是idea)
下载mybatis-3.2.2.jar包并导入工程
编写MyBatis核心配置文件(mybatis-cfg.xml)
<?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>
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.56.101/library-books"/>
<property name="user" value="kb07"/>
<property name="pwd" value="ok"/>
</properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--取别名-->
<typeAliases>
<typeAlias type="cn.kgc.kb07.entity.User" alias="user"/>
<typeAlias type="cn.kgc.kb07.entity.Book" alias="book"/>
</typeAliases>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pwd}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/kgc/kb07/dao/UserDao.xml"></mapper>
<mapper resource="cn/kgc/kb07/dao/BookDao.xml"></mapper>
</mappers>
</configuration>
创建实体类-Book(对应要查询的数据库中的图书信息)
public class Book {
private Integer book_id;
private String book_name;
private Double price;
private Integer store;
private String des;
private Integer book_type;
private Integer borrow_id;
public Book() {
}
public Book(Integer book_id, String book_name, Double price, Integer store, String des, Integer book_type, Integer borrow_id) {
this.book_id = book_id;
this.book_name = book_name;
this.price = price;
this.store = store;
this.des = des;
this.book_type = book_type;
this.borrow_id = borrow_id;
}
public Integer getBook_id() {
return book_id;
}
public void setBook_id(Integer book_id) {
this.book_id = book_id;