<?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="com.pj.project.tb_district.TbDistrictMapper">

	<!-- 增 [G] -->
	<insert id="add">
		insert into 
		tb_district (id, pid, district, level) 
		values (#{id}, #{pid}, #{district}, #{level}) 
	</insert>

	<!-- 删 -->
	<delete id="delete">
		delete from tb_district 
		where id = #{id}
	</delete>

	<!-- 改 [G] -->
	<update id="update">
		update tb_district set
		id = #{id}, 
		pid = #{pid}, 
		district = #{district}, 
		level = #{level}
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, pid, district, level from tb_district  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.tb_district.TbDistrict">
		<result property="id" column="id" />
		<result property="pid" column="pid" />
		<result property="district" column="district" />
		<result property="level" column="level" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from tb_district 
	</sql>
	
	<!-- 查 - 根据id -->
	<select id="getById" resultMap="model">
		<include refid="select_sql"></include>
		where id = #{id}
	</select>
	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
			<if test=' this.isNotNull("id") '> and id = #{id} </if>
			<if test=' this.isNotNull("pid") '> and pid = #{pid} </if>
			<if test=' this.isNotNull("district") '> and district = #{district} </if>
			<if test=' this.isNotNull("level") '> and level = #{level} </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id desc </when>
			<when test='sortType == 2'> pid desc </when>
			<when test='sortType == 3'> district desc </when>
			<when test='sortType == 4'> level desc </when>
			<otherwise> id desc </otherwise>
		</choose>
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
