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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 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
目录
相关文章
|
16天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
20 0
|
1月前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
21 1
|
1月前
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
15 1
|
15小时前
|
SQL Java 数据库连接
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
|
18天前
|
Java 关系型数据库 MySQL
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
【mybatis-plus】自定义多数据源,动态切换数据源事务失效问题
|
29天前
|
Java 数据库连接 mybatis
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
19 0
|
1月前
|
Java 数据库连接 mybatis
Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
20 1
|
3天前
|
SQL 存储 关系型数据库
MySQL Cluster集群安装及使用
MySQL Cluster集群安装及使用
|
18天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
7天前
|
关系型数据库 MySQL 数据库
《MySQL 简易速速上手小册》第1章:MySQL 基础和安装(2024 最新版)
《MySQL 简易速速上手小册》第1章:MySQL 基础和安装(2024 最新版)
33 4