<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
		"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="com.house365.beans.entity.DictionaryEntity">
	<typeAlias alias="dictionaryEntity"
			   type="com.house365.beans.entity.DictionaryEntity" />
	<resultMap id="dictionary"
			   class="com.house365.beans.entity.DictionaryEntity">
		<result property="id" column="id" />
		<result property="dicKey" column="dic_key" />
		<result property="dicValue" column="dic_value" />
		<result property="dicName" column="dic_name" />
		<result property="city" column="city" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="dictionary.columns">
		<![CDATA[
        id, dic_key, dic_value, dic_name, city
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="dictionaryEntity">
		<![CDATA[
	        INSERT INTO dictionary (
	        	id,
	        	dic_key,
	        	dic_value,
	        	dic_name,
	        	city
	        ) VALUES (
	        	#id#,
	        	#dicKey#,
	        	#dicValue#,
	        	#dicName#,
	        	#city#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
				   keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="dictionaryEntity">
		<![CDATA[
	        UPDATE dictionary SET
		        dic_key = #dicKey#,
		        dic_value = #dicValue#,
		        dic_name = #dicName#,
		        city = #city#
	        WHERE
		        id = #id#
	    ]]>
	</update>

	<!-- 通过ID删除 -->
	<delete id="delete">
		<![CDATA[
	        DELETE FROM dictionary WHERE
	        id = #id#
	    ]]>
	</delete>

	<!-- 通过ID列表批量删除 -->
	<delete id="deleteByIds" parameterClass="java.lang.String">
		<![CDATA[
	        DELETE FROM dictionary WHERE
	        id IN ($ids$)
	    ]]>
	</delete>

    <delete id="deleteByKey" parameterClass="java.lang.String">
        <![CDATA[
	        DELETE FROM dictionary WHERE dic_key = #key#
	    ]]>
    </delete>

	<!-- 通过ID查询 -->
	<select id="getById" resultMap="dictionary">
		SELECT
		<include refid="dictionary.columns" />
		<![CDATA[
	        FROM dictionary
	        WHERE
		        id = #id#
	    ]]>
	</select>

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="dictionary.queryPage">
		<dynamic prepend="WHERE">
			<!-- 默认不开启实体作为条件 -->
			<!--
		    <isNotNull property="entity">
				<isNotEmpty prepend="AND" property="entity.id">
		            <![CDATA[
					id = #entity.id#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.dicKey">
		            <![CDATA[
					dic_key = #entity.dicKey#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.dicValue">
		            <![CDATA[
					dic_value = #entity.dicValue#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.dicName">
		            <![CDATA[
					dic_name = #entity.dicName#
		            ]]>
				</isNotEmpty>
			</isNotNull>
			 -->
			<isNotNull property="searchFilters">
				<isNotEmpty prepend="AND" property="searchFilters.id">
					<![CDATA[
					id $searchFilters.id.operation$ #searchFilters.id.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.dicKey">
					<![CDATA[
					dic_key $searchFilters.dicKey.operation$ #searchFilters.dicKey.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.dicValue">
					<![CDATA[
					dic_value $searchFilters.dicValue.operation$ #searchFilters.dicValue.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.dicName">
					<![CDATA[
					dic_name $searchFilters.dicName.operation$ #searchFilters.dicName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.city">
					<![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.value#
		            ]]>
				</isNotEmpty>
			</isNotNull>
		</dynamic>
		<dynamic prepend="ORDER BY">
			<isNotNull property="orderColumn">
				<![CDATA[
				$orderColumn$
				]]>
				<isNotNull property="orderMode">
					$orderMode$
				</isNotNull>
			</isNotNull>
		</dynamic>
	</sql>

	<!-- 为分页查询符合条件的记录数 -->
	<select id="queryByPage_count" resultClass="int">
		SELECT count(*) FROM dictionary
		<include refid="dictionary.queryPage" />
	</select>

	<!-- 分页查询指定页记录 -->
	<select id="queryByPage" resultMap="dictionary">
		SELECT
		<include refid="dictionary.columns" />
		<![CDATA[
	        FROM dictionary
	    ]]>
		<include refid="dictionary.queryPage" />
		<![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
	</select>

	<!-- 查询所有记录 -->
	<select id="queryAll" resultMap="dictionary">
		SELECT
		<include refid="dictionary.columns" />
		<![CDATA[
		    FROM dictionary
	    ]]>
		<include refid="dictionary.queryPage" />
	</select>


</sqlMap>
