东方飘雪

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 maven引入

<dependency>
            <groupId>com.github.pagehelper</groupId><!--mybatis分页插件-->
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version><!--注意版本-->
 </dependency>

 引入maven后,实际操作代码如下:

controller

    /**demo   分页查询
     *
     * @param pageNum  当前页数
     * @param pageSize 当前页最多显示多少行
     * @return
     */
    @ResponseBody   //返回json数据
    @GetMapping("/demo/findByPaging")
    public  String findByPaging(Integer pageNum, Integer pageSize)throws ControllerException {
        int age = 27;
        JSONObject result = new JSONObject();
        try{
       //使用分页插件,核心代码就这一行 pageNum当前页数 pageSize 当前页最多显示多少行 PageHelper.startPage(pageNum,pageSize);
         Page
<EmployeesDemo> data = demoService.findByPaging( age);//age为查询条件 result.put("employees",data);//data为返回数据 //获取页面总数 result.put("pages",data.getPages()); //获取数据总数 result.put("total",data.getTotal()); }catch (Exception e){ e.printStackTrace(); throw new ControllerException("分页查询失败",e,userContext); } return result !=null?result.toString():null; }

service

    /**
     * 分页查询结果
     * @param age 查询条件  年龄
     * @return
     */
    public Page<EmployeesDemo> findByPaging(int age)throws ServiceException {
        Page<EmployeesDemo> pages = null;
        try{
            pages =demoDao.findByPaging(age);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("分页查询结果出错 参数age="+age, e);
        }
        return pages;
    }

 dao

    /**demo
     * 分页查询
     * @param age  查询条件  年龄
     * @return
     */
    public Page<EmployeesDemo> findByPaging( int age);

mapper

    <select id="findByPaging" resultType="com.nsoft.gkzp.demo.entity.EmployeesDemo"  parameterType="Integer">
        select * from EmployeesDemo
        where age >= #{name}
    </select>

参考:https://blog.csdn.net/qq_28988969/article/details/78082116  

另: 

1.parameterType为传入参数类型,resultType为结果类型(由于mybatis我在配置时设置了驼峰转换,故sql语句查出的结果为驼峰写法时,会自动转换映射到EmployeesDemo实体中)

2.如果传入的参数为实体时,可写为 parameterType =“com.nsoft.gkzp.demo.entity.EmployeesDemo”。

3. 如果为多表查询,不好建实体类情况(即查出的结果和实体类字段对应不上时,即用不了实体类);此时返回Hashmap即可,如下:

controller

import org.json.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

    /**
     * 获取用户信息
     * @param pageNum  当前页数
     * @param pageSize 当前页最多显示多少行
     * @return
     * @throws Exception
     */
    @RequestMapping("/user/getUserInfos")
    public String getUserInfos( Integer pageNum, Integer pageSize) throws Exception{

        JSONObject result = new JSONObject();
        try{
            PageHelper.startPage(pageNum,pageSize);
            Page<HashMap> data = sysUserService.getUserInfos( );

            result.put("data",data);
            //获取页面总数
            result.put("limit",data.getPages());
            //获取数据总数
            result.put("total",data.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            //   throw new ControllerException("分页查询失败",e,userContext);
        }

        return result !=null?result.toString():null;
    }

Services实现类

    /**
     * 获取用户数据
     * @return
     * @throws ServiceException
     */
    public Page getUserInfos() throws ServiceException{
        Page<HashMap> list = null;
        try {
            list = sysUserDao.getUserInfos();

        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("查询获取用户信息时出错", e);
        }
        return list;
    }

dao接口

import com.github.pagehelper.Page;
  
  /**
     * 获取用户数据
     * @return
     */
    public Page getUserInfos();

mapper

<select id="login" resultType="com.nsoft.gkzp.system.sysuser.entity.SysUser">
    select * from sys_user
    <where>
        <if test="loginName != null">
             loginname =#{loginName}
        </if>
        <if test="password != null">
            and Password=#{password}
        </if>
    </where>
</select>

 <select id="getUserInfos" resultType="java.util.HashMap">
        select k.id,k.loginName,t.name,t.Gender,RIGHT(IDCardNo,4) IDCardNo
        from   sys_user k
        left join HR_Recruit_entryInfo_base t  on t.IsNewest=1 and k.id = t.loginUserId
        where k.nstatusid =1
    </select>

 

4.如果sql语句查询出的结果字段想改成其他名称,这时一种方法是表字段写别名,另一种是使用resultMap

 <resultMap id="BaseResultMap" type="com.nsoft.dgc.managecenter.entity.DgcSyslog">
        <id column="ID" jdbcType="DECIMAL" property="id" />
        <result column="TABLE_NAME" jdbcType="VARCHAR" property="tableName" />
        <result column="TYPE" jdbcType="DECIMAL" property="type" />
        <result column="TYPE_NAME" jdbcType="VARCHAR" property="typeName" />
        <result column="EDITOR_ID" jdbcType="DECIMAL" property="editorId" />
        <result column="EDITOR_TIME" jdbcType="TIMESTAMP" property="editorTime" />
        <result column="IP_ADDRESS" jdbcType="VARCHAR" property="ipAddress" />
        <result column="DESCRIPTION" jdbcType="VARCHAR" property="description" />
        <result column="EDITOR_NAME" jdbcType="VARCHAR" property="editorName" />
    </resultMap>
    <select id="findByPaging" resultMap="BaseResultMap" parameterType="com.nsoft.dgc.managecenter.entity.DgcSyslog" >
        SELECT
            m.*,
            n.USER_NAME editor_Name
        FROM
            dgc_syslog m,
            DGC_OA_MEMBER n
        WHERE
            m.EDITOR_ID = n.USER_ID
<!--         <where>-->
            <if test="startDate != null and startDate != ''">
               and to_char(m.editor_time,'yyyy-mm-dd') >= '${startDate}'
            </if>
            <if test="endDate != null and endDate != ''">
                and to_char(m.editor_time,'yyyy-mm-dd') &lt;= '${endDate}'  <!-- mybatis不用使用 <= 符号,必须使用转义字符 &lt;=  -->
            </if>
            <if test="queryType>=0 ">
                and m.type = ${queryType}
            </if>
            <if test="message !=null and message !='' ">
                and ( n.USER_NAME like '%${message}%' or  m.ip_address like '%${message}%' or m.type_name like '%${message}%' or m.description like '%${message}%' )
            </if>
<!--         </where>-->
        order by m.editor_time desc
    </select>

 

这里有一篇非常好的文档,一定要看看

https://my.oschina.net/sunpr/blog/340951

我截图划了重点如下:

 

posted on 2019-08-31 16:59  东方飘雪  阅读(606)  评论(0编辑  收藏  举报