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>

原文地址:https://www.cnblogs.com/a1111/p/7459751.html