maybatis逆向工程生成的mapper.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">
<!--
mybatis逆向工程生成的sql语句只会进行单表操作,不会进行复杂的操作
-->
<mapper namespace="com.example.springboot012mybatis02.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.example.springboot012mybatis02.model.Student">
<!--
id标签只能修饰主键字段
除了主键以外的字段都用result修饰
column:列,数据库中的字段名称
jdbcType:列在jdbc中的字段类型(可以省略不写)
property:映射对象的属性名称
resultMap的作用:
1.当数据库字段名称与实体类对象属性名不一致时,可以进行转换
2.当前查询结果没有对应一个表时,可以自定义一个结果集
-->
<id column="sid" jdbcType="INTEGER" property="sid" />
<result column="sname" jdbcType="CHAR" property="sname" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="stime" jdbcType="TIMESTAMP" property="stime" />
</resultMap>
<!--
sql语句片段,将公共的部分抽取出来
可以通过 <include refid="Base_Column_List" />引用
-->
<sql id="Base_Column_List">
sid, sname, age, stime
</sql>
<!--
id:与dao的名称一致
parameterType:可写可不写,写了就按这种格式生成
resultMap:查询最终映射的集合
-->
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
-- Base_Column_List将公共部分抽取出来,相当于
-- select
-- sid, sname, age, stime
select
<include refid="Base_Column_List" />
from student
where sid = #{sid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from student
where sid = #{sid,jdbcType=INTEGER}
</delete>
<!--
这种insert必须将列出的字段全部插入,不常用
-->
<insert id="insert" parameterType="com.example.springboot012mybatis02.model.Student">
insert into student (sid, sname, age,
stime)
values (#{sid,jdbcType=INTEGER}, #{sname,jdbcType=CHAR}, #{age,jdbcType=INTEGER},
#{stime,jdbcType=TIMESTAMP})
</insert>
<!--
suffixOverrides:去除多余的逗号
prefix:前缀
suffix:后缀
-->
<!-- 这种insert用的多,可以选择性插入-->
<insert id="insertSelective" parameterType="com.example.springboot012mybatis02.model.Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="sid != null">
sid,
</if>
<if test="sname != null">
sname,
</if>
<if test="age != null">
age,
</if>
<if test="stime != null">
stime,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="sid != null">
#{sid,jdbcType=INTEGER},
</if>
<if test="sname != null">
#{sname,jdbcType=CHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="stime != null">
#{stime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<!-- 常用,可以选择性更新-->
<!-- <set>可以自动去掉最后一个逗号-->
<update id="updateByPrimaryKeySelective" parameterType="com.example.springboot012mybatis02.model.Student">
update student
<set>
<if test="sname != null">
sname = #{sname,jdbcType=CHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
<if test="stime != null">
stime = #{stime,jdbcType=TIMESTAMP},
</if>
</set>
where sid = #{sid,jdbcType=INTEGER}
</update>
<!--不常用-->
<update id="updateByPrimaryKey" parameterType="com.example.springboot012mybatis02.model.Student">
update student
set sname = #{sname,jdbcType=CHAR},
age = #{age,jdbcType=INTEGER},
stime = #{stime,jdbcType=TIMESTAMP}
where sid = #{sid,jdbcType=INTEGER}
</update>
</mapper>