mybatis开源框架-Mybatis系列

mybatis 必备手册

2020-12-06  本文已影响0人  晴天哥_王志

前言

准备

CREATE TABLE `t_student` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(255) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `clazz_id` int(11) DEFAULT NULL COMMENT '班级id',
  `number` varchar(6) DEFAULT NULL COMMENT '学号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC


public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Integer clazzId;
    private String number;
}

常用SQL

select语句

  <sql id="base_columns">
    id, name, age, clazz_id, number
  </sql>

  <select id="selectById" resultMap="BaseResultMap">
    select <include refid="base_columns"/> from t_student where id = #{id}
  </select>

  Student selectById(@Param("id") int id);
  <sql id="base_columns">
    id, name, age, clazz_id, number
  </sql>

  <select id="selectByIds" resultMap="BaseResultMap">
    SELECT <include refid="base_columns"/> from t_student where id in
    <foreach collection="ids" separator="," item="id" open="(" close=")">
      #{id}
    </foreach>
    AND age=#{age}
  </select>

  List<Student> selectByIds(@Param("ids") List<Integer> idList, @Param("age") Integer age);
  <sql id="base_columns">
    id, name, age, clazz_id, number
  </sql>

  <select id="selectByIdsV2" resultMap="BaseResultMap">
    SELECT <include refid="base_columns"/> from t_student
    <where>
      <if test="ids != null and ids.size() > 0">
        id in
        <foreach collection="ids" open="(" close=")" separator=",">
          #{id}
        </foreach>
      </if>
      <if test="age != null">
        AND age = #{age}
      </if>
    </where>
  </select>

  List<Student> selectByIdsV2(@Param("ids") List<Integer> idList, @Param("age") Integer age);
  <sql id="base_columns">
    id, name, age, clazz_id, number
  </sql>

  <select id="selectByIdsV3" resultMap="BaseResultMap">
    SELECT <include refid="base_columns"/> from t_student
    <where>
      <if test="studentList != null and studentList.size() > 0">
        id in
        <foreach collection="studentList" item="item" open="(" close=")" separator=",">
          #{item.id}
        </foreach>
      </if>
      <if test="age != null">
        AND age = #{age}
      </if>
    </where>
  </select>

  List<Student> selectByIdsV3(@Param("studentList") List<Student> studentList, @Param("age") Integer age);
  <sql id="base_columns">
    id, name, age, clazz_id, number
  </sql>

  <select id="selectByIdsV4" resultMap="BaseResultMap">
    SELECT <include refid="base_columns"/> from t_student
    <trim prefix="where" prefixOverrides="and|or">
      <if test="studentList != null and studentList.size() > 0">
        id in
        <foreach collection="studentList" item="item" open="(" close=")" separator=",">
          #{item.id}
        </foreach>
      </if>
      <if test="age != null">
        AND age = #{age}
      </if>
    </trim>
  </select>

  List<Student> selectByIdsV4(@Param("studentList") List<Student> studentList, @Param("age") Integer age);

insert语句

    <insert id="insertV1" parameterType="com.example.model.Student" keyProperty="id" useGeneratedKeys="true">
      insert into t_student (`name`, age, clazz_id, `number`)
      values (#{name}, #{age}, #{clazzId}, #{number})
    </insert>

    Integer insertV1(Student student);
    <insert id="insertV2" parameterType="com.example.model.Student" keyProperty="id" useGeneratedKeys="true">
        insert into t_student (`name`, age, clazz_id, `number`)
        values (#{student.name}, #{student.age}, #{student.clazzId}, #{student.number})
    </insert>

    Integer insertV2(@Param("student") Student student);
  <insert id="batchInsertV1">
    insert into t_student (`name`, age, clazz_id, `number`)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.name}, #{item.age}, #{item.clazzId}, #{item.number})
    </foreach>
  </insert>

  Integer batchInsertV1(List<Student> studentList);
  <insert id="batchInsertV2">
    insert into t_student (`name`, age, clazz_id, `number`)
    values
    <foreach collection="studentList" item="item" separator=",">
      (#{item.name}, #{item.age}, #{item.clazzId}, #{item.number})
    </foreach>
  </insert>

  Integer batchInsertV2(@Param("studentList") List<Student> studentList);

delete语句

  <delete id="deleteById" parameterType="java.lang.Integer">
    delete from t_student where id = #{id}
  </delete>

  int deleteById(@Param("id") int id);
  <delete id="batchDetele">
    delete from t_student
    <if test="idList != null and idList.size() > 0">
        where id in
        <foreach collection="idList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </if>
  </delete>

  int batchDetele(@Param("idList") List<Integer> idList);

update语句

    <update id="updateV1" parameterType="com.example.model.Student">
        update t_student
        <set>
            <if test="name != null">`name` = #{name},</if>
            <if test="age != null" >age = #{age},</if>
            <if test="clazzId != null">clazzId = #{clazzId},</if>
            <if test="number != null">`number` = #{number}</if>
        </set>
        where id = #{id}
    </update>

    Integer updateV1(Student student);
    <update id="updateV2" parameterType="com.example.model.Student">
        update t_student
        <trim prefix="set" suffixOverrides=",">
            <if test="name != null">`name` = #{name},</if>
            <if test="age != null" >age = #{age},</if>
            <if test="clazzId != null">clazzId = #{clazzId},</if>
            <if test="number != null">`number` = #{number},</if>
        </trim>
        where id = #{id}
    </update>

    Integer updateV2(Student student);
    <update id="batchUpdate" parameterType="java.util.List">
        update t_student
        <trim prefix="set" suffixOverrides=",">

          <trim prefix="name = case " suffix="end,">
            <foreach collection="studentList" item="item">
              <if test="item.name != null">
                  when id = #{item.id} then #{item.name}
              </if>
            </foreach>
          </trim>

          <trim prefix="age = case " suffix="end,">
              <foreach collection="studentList" item="item">
                  <if test="item.age != null">
                      when id = #{item.id} then #{item.age}
                  </if>
              </foreach>
          </trim>

          <trim prefix="clazzId = case " suffix="end,">
              <foreach collection="studentList" item="item">
                  <if test="item.clazzId != null">
                      when id = #{item.id} then #{item.clazzId}
                  </if>
              </foreach>
          </trim>

          <trim prefix="number = case " suffix="end,">
              <foreach collection="studentList" item="item">
                  <if test="item.number != null">
                      when id = #{item.id} then #{item.number}
                  </if>
              </foreach>
          </trim>
        </trim>

        where id in
        <foreach collection="studentList" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>

    Integer batchUpdate(@Param("studentList") List<Student> studentList);

参考

上一篇下一篇

猜你喜欢

热点阅读