模糊查询 like
concat关键字
<select id="selecttj" resultMap="BaseResultMap">
select <include refid="Base_Column_List"/> from book_info
<where>
<if test="bookName!=null and bookName!=''">
book_name=#{bookName} like concat('%',#{bookName},'%')
</if>
<if test="bookAuthor!=null and bookAuthor!=''">
book_author like concat('%',#{bookAuthor},'%')
</if>
<if test="bookPrice!=null and bookPrice!=''">
book_price like concat('%',#{bookPrice},'%')
</if>
<if test="bookPub!=null and bookPub!=''">
book_pub like concat('%',#{bookPub},'%')
</if>
</where>
</select>
分页查询PageHelper
sql分页查询语句
select * from 表名 limit 0,5;
1,引入PageHelper依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
2,加入拦截
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
3,测试
public class Testdao {
private SqlSession session;
@Before
public void batis()throws Exception{
Reader resourceAsReader = Resources.getResourceAsReader("batis.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
session = build.openSession();
}
@Test
public void seecef()throws Exception{
//使用分页功能 request.getParamter("page") request.getParamter("pageSize")
BookIDao mapper = session.getMapper(BookIDao.class);
PageHelper.startPage(1,20);
Book book = new Book();
book.setBookAuthor("曹");
List<Book> selecttj = mapper.selecttj(book);
//可以把查询的结果封装到PageInfo类中 包含你想要的任何信息
PageInfo<Book> pageInfo = new PageInfo<Book>(selecttj);
System.out.println("总条数"+pageInfo.getTotal());
System.out.println("当前页数"+pageInfo.getPageNum());
System.out.println("数据"+pageInfo.getList());
System.out.println(selecttj);
}
}