Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
生活随笔
收集整理的這篇文章主要介紹了
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<?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><!-- 關聯(lián)用戶表 --><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><!-- 關聯(lián)街道表 --><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><!-- 關聯(lián)區(qū)縣表 --><resultMap id="districtDaoMapper" type="district" ><id column="ID" property="id"/><result column="NAME" property="name"/></resultMap><!-- 在根據(jù)區(qū)縣id查詢一遍區(qū)縣表 --><select id="selectDirstrict" resultMap="districtDaoMapper">select * form district where id=#{district_id} </select><!--關聯(lián)類型表 --><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><!--根據(jù)id查詢房屋信息 --><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select <include refid="Base_Column_List" />from housewhere ID = #{id,jdbcType=INTEGER}</select><!-- 根據(jù)id刪除房屋信息 --><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from housewhere 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><!-- 根據(jù)id修改房屋信息 --><update id="updateByPrimaryKey" parameterType="house" >update house setUSER_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><!-- 動態(tài)查詢房屋信息的總記錄數(shù) --><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是數(shù)據(jù)庫的字段名 --><if test="street!=null">and h.street_id = #{street.id}</if><!-- h.type_id是數(shù)據(jù)庫的字段名 --><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><!-- 分頁動態(tài)查詢房屋信息 --><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><!-- 查詢?nèi)康姆课菪畔?--><select id="getHouseInfo" resultType="house">select * from house </select><!-- 分頁查詢?nèi)康姆课菪畔?--><select id="getHousePage" parameterType="hashmap" resultMap="BaseResultMap">select * from house limit #{startRow},#{endRow}</select>
</mapper>
總結
以上是生活随笔為你收集整理的Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么设置手动获取IP地址和DNS的地址?
- 下一篇: Mybatis+mysql动态分页查询数