1. 简单的select映射

<mapper namespace="com.mybatis3.mappers.StudentMapper">
    <select id="findStudentById" parameterType="int" resultType="Student">
        select stud_id as studId, name, email, dob 
        from Students 
        where stud_id=#{studId}
     </select>
</mapper>

映射接口:

package com.mybatis3.mappers;
public interface StudentMapper
{
 Student findStudentById(Integer id);
}

 

2. 简单的insert映射

<insert id="insertStudent" parameterType="Student">
     INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL, PHONE)
     VALUES(#{studId},#{name},#{email},#{phone})
</insert>

 映射接口:

package com.mybatis3.mappers;
public interface StudentMapper
{
 int insertStudent(Student student);
}

 自增主键:

useGeneratedKeys="true" keyProperty="studId"

<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="studId">
 INSERT INTO STUDENTS(NAME, EMAIL, PHONE)
 VALUES(#{name},#{email},#{phone})
</insert>

 

3. 简单的update映射

<update id="updateStudent" parameterType="Student">
     UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, PHONE=#{phone}
     WHERE STUD_ID=#{studId}
</update>

 

4. 简单的delete映射

<delete id="deleteStudent" parameterType="int">
     DELETE FROM STUDENTS WHERE STUD_ID=#{studId}
</delete>

 映射接口:

package com.mybatis3.mappers;
public interface StudentMapper
{
 int deleteStudent(int studId);
}

 

5. 简单的结果映射

resultMap

<resultMap id="StudentResult" type="com.mybatis3.domain.Student">
    <id property="studId" column="stud_id"/>
    <result property="name" column="name"/>
    <result property="email" column="email"/>
    <result property="phone" column="phone"/>
</resultMap>

<select id="findAllStudents" resultMap="StudentResult" >
    SELECT * FROM STUDENTS
</select>

<select id="findStudentById" parameterType="int" resultMap="StudentResult">
    SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}
</select>

 

技术改变世界