MyBatis 高级查询和分页查询

MyBatis 专栏收录该内容
7 篇文章 0 订阅

一、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 &lt;=  #{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();
    }


}

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值