StudentMapper.xml
<?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="monkey1024">
<!-- parameterType可省略 -->
<insert id="insertStudent" parameterType="student">
INSERT INTO t_student(name,age,score) VALUES (#{name},#{age},#{score})
<!-- 获取自增的主键 -->
<selectKey resultType="int" keyProperty="id" order="AFTER">
select @@identity
</selectKey>
</insert>
<!-- 这里的#{xxx}只是起着占位符的作用 -->
<delete id="deleteStudent">
delete from t_student where id=#{id}
</delete>
<update id="updateStudent">
update t_student set name=#{name},age=#{age},score=#{score} where id=#{id}
</update>
<!-- resultType要写上单条数据对应的类 -->
<!-- 查询中尽量不要出现*会影响sql的执行效率 -->
<select id="selectAllStudent" resultType="student">
select id,name,age,score from t_student
</select>
<select id="selectStudentById" resultType="student">
select id,name,age,score from t_student where id=#{id}
</select>
<select id="selectStudentByName" resultType="student">
<!-- select id,name,age,score from t_student where name like '%' #{name} '%' -->
<!-- 有sql注入问题 -->
select id,name,age,score from t_student where name like '%${value}%'
</select>
<!-- 结果映射 -->
<resultMap id="studentMapper" type="student">
<id column="id" property="id"/>
<result column="password" property="pwd"/>
<result column="age" property="age"/>
</resultMap>
<select id="selectStudentPwd" resultMap="studentMapper">
select id,name,age,score,password from t_student where id=#{id}
</select>
<!-- <select id="selectStudentPwd" resultType="student">
select id,name,age,score,password pwd from t_student where id=#{id}
</select> -->
</mapper>