zoukankan      html  css  js  c++  java
  • Mybatis同时传递实体和分页数据

    在使用mybatis查询数据库时,如果需要分页可以在dao接口方法中传递两个数据,一个是页数pageNum,一个是一面显示几页pageSize,在Mybatis编译sql语句时,会将这两个分页数据插入到sql语句中,在每个参数前要加上@Param注解,注解中的内容是有规定的,页数就是pageNum,显示几页是pageSize:

    public interface CityDao{
        List<City> listCity(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);
    }

    cityMapper.xml:

    <select id="listCity" resultMap="cityMap" parameterType="edu.nf.city.entity.City">
        select city_id, country, province, city_name from city_info
    </select>

    但是,当还需要传递一些条件时,如需要根据City对象中的省份或者城市字段模糊查询,并实现分页:

    dao:

    public interface CityDao{
        List<City> listCity(City city, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);  
    }

    cityMapper.xml(这里使用动态sql查询):

    <!-- 根据province和city_name两个字段模糊查询出城市信息并实现分页,如果city对象为null,则表示查询所有
     同时,因为在前面还传了pageNum和pageSize两个分页数据,所以在sql中对city对象的取值前面需要加上city对象,
    如下: -->
    <select id="listCity" resultMap="cityMap" parameterType="edu.nf.city.entity.City">
        select city_id, country, province, city_name from city_info
        <where>
            <if test="city != null">
                <if test="city.province != null and city.province != ''">
                    province = #{city.province}
                </if>
                <if test="city.cityName != null and city.cityName != ''">
                    and city_name = #{city.cityName}
                </if>
            </if>
        </where>
    </select>
  • 相关阅读:
    servlet.txt笔记
    用数组实现集合的功能
    用父类声明的变量和用接口声明的变量的区别
    DHTML_____document对象的方法
    DHTML_____window对象的事件
    DHTML_____window对象属性
    DHTML_____window对象方法
    DHTML_____如何编写事件处理程序
    常用点击事件(鼠标、光标、键盘、body)
    鼠标滑动显示不同页面的效果——————获取鼠标相对于整个页面的坐标
  • 原文地址:https://www.cnblogs.com/zhangcaihua/p/13027039.html
Copyright © 2011-2022 走看看