Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
<?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" >
<mapper namespace="houseDao" >
  <resultMap id="BaseResultMap" type="house" >
    <id column="ID" property="id" jdbcType="INTEGER" />
    <result column="TITLE" property="title" jdbcType="VARCHAR" />
    <result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
    <result column="PRICE" property="price" jdbcType="REAL" />
    <result column="PUBDATE" property="pubdate" jdbcType="DATE" />
    <result column="FLOORAGE" property="floorage" jdbcType="INTEGER" />
    <result column="CONTACT" property="contact" jdbcType="VARCHAR" />
    <!-- 开始映射外键 -->
    <!-- 映射用户表 -->
    <association property="users" column="user_id" select="selectUsers"/>
    <!-- 映射类型表 -->
    <association property="types" column="type_id" select="selectTypes"/>
    <!-- 映射街道表 -->
    <association property="street" column="street_id" select="selectStreet"/>
  </resultMap>
  <!-- 关联用户表 -->
  <resultMap id="usersMapper" type="users" >
    <id column="ID" property="id" jdbcType="INTEGER" />
    <result column="NAME" property="name" jdbcType="VARCHAR" />
    <result column="PASSWORD" property="password" jdbcType="VARCHAR" />
    <result column="TELEPHONE" property="telephone" jdbcType="VARCHAR" />
    <result column="USERNAME" property="username" jdbcType="VARCHAR" />
    <result column="ISADMIN" property="isadmin" jdbcType="VARCHAR" />
  </resultMap>
  <!-- 关联街道表 -->
  <resultMap id="streetMapper" type="street" >
    <id column="ID" property="id" />
    <result column="NAME" property="name" jdbcType="VARCHAR" />
  <association property="district" column="district_id" select ="selectDirstrict"/>
  </resultMap>
  <!-- 关联区县表 -->
    <resultMap id="districtDaoMapper" type="district" >
      <id column="ID" property="id"/>
      <result column="NAME" property="name"/>
   </resultMap>
     <!-- 在根据区县id查询一遍区县表 -->
      <select id="selectDirstrict" resultMap="districtDaoMapper">
        select * form district where id=#{district_id}  
      </select>
  <!--关联类型表  -->
  <resultMap id="typeMapper" type="types" >
      <id column="ID" property="id"/>
      <result column="NAME" property="name" jdbcType="VARCHAR" />
   </resultMap>
  <!-- 用户表 -->
  <select id="selectUsers" resultMap="usersMapper">
    select * from users where id=#{user_id}
  </select>
  <!-- 街道表 -->
   <select id="selectStreet" resultMap="streetMapper">
    select * from street where id=#{street_id}
  </select>
  <!-- 类型表 -->
    <select id="selectTypes" resultMap="typeMapper">
    select * from types where id=#{type_id}
  </select>
  <sql id="Base_Column_List" >
    ID, USER_ID, TYPE_ID, TITLE, DESCRIPTION, PRICE, PUBDATE, FLOORAGE, CONTACT, STREET_ID
  </sql>
  <!--根据id查询房屋信息 -->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from house
    where ID = #{id,jdbcType=INTEGER}
  </select>
  <!-- 根据id删除房屋信息 -->
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from house
    where ID = #{id,jdbcType=INTEGER}
  </delete>
  <!-- 添加房屋信息 -->
  <insert id="insert" parameterType="house" >
    insert into house
     (
       USER_ID, TYPE_ID, TITLE, 
      DESCRIPTION, PRICE,  PUBDATE, 
      FLOORAGE, CONTACT, STREET_ID
    )
    values
     (
        #{users.id,jdbcType=INTEGER}, 
       #{types.id,jdbcType=INTEGER},  #{title,jdbcType=VARCHAR},
       #{description,jdbcType=VARCHAR}, #{price,jdbcType=REAL}, 
       #{pubdate,jdbcType=DATE}, #{floorage,jdbcType=INTEGER},
       #{contact,jdbcType=VARCHAR}, #{street.id,jdbcType=INTEGER}
     )
  </insert>
  <!-- 根据id修改房屋信息 -->
  <update id="updateByPrimaryKey" parameterType="house" >
  update house set
    USER_ID     = #{users.id,jdbcType=INTEGER},
    TYPE_ID     = #{types.id,jdbcType=INTEGER},
    TITLE     = #{title,jdbcType=VARCHAR},
    DESCRIPTION = #{description,jdbcType=VARCHAR},
    PRICE   = #{price,jdbcType=REAL},
    PUBDATE   = #{pubdate,jdbcType=DATE},
    FLOORAGE  = #{floorage,jdbcType=INTEGER},
    CONTACT   = #{contact,jdbcType=VARCHAR},
    STREET_ID   = #{street.id,jdbcType=INTEGER}
  where ID    = #{id,jdbcType=INTEGER}
  </update>
  <!-- 动态查询房屋信息的总记录数 -->
  <select id="reCount" parameterType="houseC" resultType="Integer">
  select count(0) from house h
  <where>
    <if test="priceBegin!=null">
       and h.price > #{priceBegin}
    </if>
    <if test="priceEnd!=null">
      and h.price   <![CDATA[<]]>  #{priceEnd}
    </if>
    <!-- h.street_id是数据库的字段名 -->
    <if test="street!=null">
       and h.street_id = #{street.id}
     </if>
     <!-- h.type_id是数据库的字段名 -->
     <if test="types!=null">
       and h.type_id = #{types.id}  
     </if> 
    <if test="floorageBegin!=null">
       and h.floorage > #{floorageBegin}  
    </if>
    <if test="floorageEnd!=null">
      and h.floorage <![CDATA[<]]>  #{floorageEnd}
    </if>
  </where>
  </select>
  <!-- 分页动态查询房屋信息 -->
  <select id="getHouseInfoByDymanic" parameterType="hashmap" resultMap="BaseResultMap">
      select * from house h
  <where>
    <if test="priceBegin!=null">
       and h.price > #{priceBegin}
    </if>
    <if test="priceEnd!=null">
      and h.price   <![CDATA[<]]>  #{priceEnd}
    </if>
    <if test="street!=null">
       and h.street_id = #{street.id}
     </if>
    <if test="types!=null||!types==null">
       and h.type_id = #{types.id}  
     </if>
    <if test="floorageBegin!=null">
       and h.floorage > #{floorageBegin}  
    </if>
    <if test="floorageEnd!=null">
      and h.floorage <![CDATA[<]]>  #{floorageEnd}
    </if>
  </where>
        limit #{stratRow},#{endRow}
  </select>
  <!-- 查询全部的房屋信息 -->
  <select id="getHouseInfo" resultType="house">
      select * from house 
  </select>
  <!-- 分页查询全部的房屋信息 -->
  <select id="getHousePage" parameterType="hashmap" resultMap="BaseResultMap">
      select * from house limit #{startRow},#{endRow}
  </select>
</mapper>
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
31 0
|
2月前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
22 1
|
2月前
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
18 1
|
23小时前
|
SQL 安全 BI
基于jeecg-boot的nbcio-boot因升级mybatis-plus到3.5.3.1和JSQLParser 到4.6而引起的在线报表配置报错处理
基于jeecg-boot的nbcio-boot因升级mybatis-plus到3.5.3.1和JSQLParser 到4.6而引起的在线报表配置报错处理
|
1天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
6 1
|
2天前
|
XML Java 数据库连接
MyBatis 解决上篇的参数绑定问题以及XML方式交互
MyBatis 解决上篇的参数绑定问题以及XML方式交互
5 0
|
9天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
29 4
|
14天前
|
SQL Java 数据库连接
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
|
18天前
|
XML Java 数据库连接
Javaweb之Mybatis的XML配置文件的详细解析
Javaweb之Mybatis的XML配置文件的详细解析
16 0
|
25天前
|
XML Java 数据库连接
java对象有集合mybatis如何映射
java对象有集合mybatis如何映射
19 4