<?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_item_type.TbItemTypeMapper">

	<!-- 增 [G] -->
	<insert id="add">
		insert into 
		tb_item_type (id, name, need)
		values (#{id}, #{name}, #{need})
	</insert>

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

	<!-- 改 [G] -->
	<update id="update">
		update tb_item_type set
		id = #{id}, 
		name = #{name}, 
		need = #{need}, 
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, name, need, create_time, unit from tb_item_type  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.tb_item_type.TbItemType">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="need" column="need" />
		<collection property="items" ofType="com.pj.project.tb_item.TbItem" column="id" select="getItems"/>
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from tb_item_type 
	</sql>
	<select id="getItems" resultType="com.pj.project.tb_item.TbItem">
		select * from tb_item where type_id=#{id}
	</select>
	
	<!-- 查 - 根据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.has("id") '> and id = #{id} </if>
			<if test=' this.has("name") '> and name = #{name} </if>
			<if test=' this.has("need") '> and need = #{need} </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id desc </when>
			<when test='sortType == 2'> name desc </when>
			<when test='sortType == 3'> need desc </when>
			<otherwise>  sort asc </otherwise>
		</choose>
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
