<?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">
<!--suppress ALL -->
<mapper namespace="test05.mapper.StudentMapper">
    <select id="selectStudent" resultType="test05.pojo.Student">
        SELECT N_STUDENT_ID AS id,
               VC_STUDENT_CODE AS code,
               VC_STUDENT_NAME AS name,
               N_SEX AS sex,
               N_MAJOR AS major,
               N_GRADE AS grade,
               VC_DETAIL AS detail
        FROM t_student
        WHERE #{student.id} in (N_STUDENT_ID, -1)
          AND VC_STUDENT_CODE LIKE CONCAT('%', #{student.code}, '%')
          AND VC_STUDENT_NAME LIKE CONCAT('%', #{student.name}, '%')
          AND #{student.sex} in (N_SEX, -1)
          AND #{student.grade} in (N_GRADE, -1)
          AND #{student.major} in (N_MAJOR, -1)
          AND (VC_DETAIL LIKE CONCAT('%', #{student.detail}, '%') 
           OR (#{student.detail} = '' AND VC_DETAIL IS NULL))
        ORDER BY N_STUDENT_ID DESC
        LIMIT #{offset},#{pageSize};
    </select>
    <select id="selectStudentCount" resultType="int">
        SELECT COUNT(*)
        FROM t_student
        WHERE #{id} in (N_STUDENT_ID, -1)
          AND VC_STUDENT_CODE LIKE CONCAT('%', #{code}, '%')
          AND VC_STUDENT_NAME LIKE CONCAT('%', #{name}, '%')
          AND #{sex} in (N_SEX, -1)
          AND #{grade} in (N_GRADE, -1)
          AND #{major} in (N_MAJOR, -1)
          AND VC_DETAIL LIKE CONCAT('%', #{detail}, '%')
    </select>
    <insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO t_student
            (VC_STUDENT_CODE, VC_STUDENT_NAME, N_SEX, N_GRADE, N_MAJOR, VC_DETAIL)
        VALUES
            (#{code},#{name},#{sex},#{grade},#{major},#{detail});
    </insert>
    <delete id="deleteStudent">
        DELETE
        FROM t_student
        WHERE N_STUDENT_ID = #{id};
    </delete>
    <update id="updateStudent">
        UPDATE t_student
        SET VC_STUDENT_CODE=#{code},
            VC_STUDENT_NAME=#{name},
            N_SEX=#{sex},
            N_GRADE=#{grade},
            N_MAJOR=#{major},
            VC_DETAIL=#{detail}
        WHERE N_STUDENT_ID=#{id};
    </update>
</mapper>