一、mybatis高级查询和分页查询
mybatis中的#表示占位符,$表示直接拼接SQL
二、高级查询和分页查询的综合
QueryObject:
public class QueryObject {
private Long id;
private String keyword;
private Integer beginAge;
private Integer endAge;
// 结合easyui来进行使用
private long page;
private long row;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public Integer getBeginAge() {
return beginAge;
}
public void setBeginAge(Integer beginAge) {
this.beginAge = beginAge;
}
public Integer getEndAge() {
return endAge;
}
public void setEndAge(Integer endAge) {
this.endAge = endAge;
}
public long getPage() {
return page;
}
public void setPage(long page) {
this.page = page;
}
public long getRow() {
return row;
}
public void setRow(long row) {
this.row = row;
}
public long getStart(){
return ( this.page - 1 ) * row;
}
}
public class PageResult {
private long total;
private List rows;
public static PageResult empty = new PageResult(0L, Collections.emptyList());
public PageResult(long total, List rows) {
this.total = total;
this.rows = rows;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
IUserService
public interface IUserService {
PageResult list(QueryObject qo);
}
UserServiceImpl
public class UserServiceImpl implements IUserService {
@Override
public PageResult list(QueryObject qo) {
SqlSession session = MybatisUtils.openSession();
IUserMapper mapper = session.getMapper(IUserMapper.class);
long count = 0;
List<User> list = null;
try {
// 查询总数
count = mapper.getCount(qo);
if(count < 0){
return PageResult.empty;
}
list = mapper.getPageResult(qo);
return new PageResult(count,list);
} catch (Exception e) {
e.printStackTrace();
session.close();
}
return null;
}
}
public interface IUserMapper {
long getCount(QueryObject queryObject);
List<User> getPageResult(QueryObject queryObject);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:名称空间,全局的唯一标识 -->
<mapper namespace="com.shenzhenair.mybatis.demo_01.IUserMapper">
<sql id="commonSql" >
<where>
<if test="keyword != null and keyword != '' ">
AND user_name LIKE concat('%',#{keyword},'%')
</if>
<if test="beginAge != null">
AND age <![CDATA[>=]]> #{beginAge}
</if>
<if test="endAge != null">
AND age <= #{endAge}
</if>
</where>
</sql>
<select id="getCount" parameterType="com.shenzhenair.mybatis.demo_01.QueryObject" resultType="long">
SELECT count(*) FROM tb_user
<include refid="commonSql" ></include>
</select>
<select id="getPageResult" parameterType="com.shenzhenair.mybatis.demo_01.QueryObject" resultType="User">
SELECT * FROM tb_user
<include refid="commonSql" ></include>
limit #{start}, #{row};
</select>
</mapper>
三、测试类
public class MybatisUtils {
static MybatisUtils instance = new MybatisUtils();
SqlSessionFactory sf = null;
public MybatisUtils() {
try {
sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession openSession(){
return instance.sf.openSession();
}
}
package com.shenzhenair.mybatis.demo_01;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestMapper {
@Test
public void testPageResult() throws IOException {
SqlSession session = MybatisUtils.openSession();
IUserMapper mapper = session.getMapper(IUserMapper.class);
QueryObject qo = new QueryObject();
qo.setPage(1);
qo.setRow(3);
IUserService service = new UserServiceImpl();
PageResult page = service.list(qo);
List<User> rows = page.getRows();
for (int i = 0; i < rows.size(); i++) {
User user = rows.get(i);
System.out.println(user);
}
session.commit();
session.close();
}
}
版权声明:本文为m0_38068812原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。