MyBatis set标签

在 Mybatis 中,update 语句可以使用 set 标签动态更新列。set 标签可以为 SQL 语句动态的添加 set 关键字,剔除追加到条件末尾多余的逗号。

示例

要求:根据 id 修改人的名称(本节示例基于《Mybatis简单示例程序》一节的代码实现)。

PersonMapper.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="net.jhelp.mybatis.learning.orm.mapper.PersonMapper" >
  <resultMap id="BaseResultMap" type="net.jhelp.mybatis.learning.orm.entity.Person" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
    <result column="school" property="school" jdbcType="VARCHAR" />
    <result column="province" property="province" jdbcType="VARCHAR" />
    <result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
    <result column="creator" property="creator" jdbcType="VARCHAR" />
    <result column="revised_time" property="revisedTime" jdbcType="TIMESTAMP" />
    <result column="reviser" property="reviser" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, age, school, province, created_time, creator, revised_time, reviser
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from person
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from person
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="net.jhelp.mybatis.learning.orm.entity.Person" >
    insert into person (id, name, age, 
      school, province, created_time, 
      creator, revised_time, reviser
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, 
      #{school,jdbcType=VARCHAR}, #{province,jdbcType=VARCHAR}, #{createdTime,jdbcType=TIMESTAMP}, 
      #{creator,jdbcType=VARCHAR}, #{revisedTime,jdbcType=TIMESTAMP}, #{reviser,jdbcType=VARCHAR}
      )
  </insert>
</mapper>

PersonMapper 类中方法如下。

import net.jhelp.mybatis.learning.orm.entity.Person;

public interface PersonMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Person record);

    int insertSelective(Person record);

    Person selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Person record);

    int updateByPrimaryKey(Person record);
}

测试类代码如下。

package net.jhelp.mybatis.learning.test;

import lombok.extern.slf4j.Slf4j;
import net.jhelp.mybatis.learning.orm.entity.Person;
import net.jhelp.mybatis.learning.orm.mapper.PersonMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 网址:https://www.jhelpe.net
 * <p>
 * author : kame
 * date : 10/15/23 4:52 PM
 */

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MybatisTestCase {

    private  PersonMapper personMapper;

    @Autowired
    public void setPersonMapper(PersonMapper personMapper){
        this.personMapper = personMapper;
    }

    @Test
    public void test(){
        //创建一个人
        Person person = new Person();
        person.setAge(39);
        person.setName("jack");
        person.setSchool("中山大学");
        person.setCreatedTime(new java.util.Date());
        person.setProvince("广东");

        //保存
        personMapper.insertSelective(person);
        System.out.println("保存的用户ID=" + person.getId());

        Person person2 = personMapper.selectByPrimaryKey(person.getId());

        System.out.println("保存的人的名字为:" + person2.getName());
    }
}

输出结果如下。

2023-10-15T21:45:37.980+08:00 DEBUG 11238 --- [           main] n.j.m.l.o.m.P.insertSelective            : ==>  Preparing: insert into person ( name, age, school, province, created_time ) values ( ?, ?, ?, ?, ? )
2023-10-15T21:45:38.022+08:00 DEBUG 11238 --- [           main] n.j.m.l.o.m.P.insertSelective            : ==> Parameters: jack(String), 39(Integer), 中山大学(String), 广东(String), 2023-10-15 21:45:37.567(Timestamp)
2023-10-15T21:45:38.096+08:00 DEBUG 11238 --- [           main] n.j.m.l.o.m.P.insertSelective            : <==    Updates: 1
保存的用户ID=6
2023-10-15T21:45:38.105+08:00 DEBUG 11238 --- [           main] n.j.m.l.o.m.P.selectByPrimaryKey         : ==>  Preparing: select id, name, age, school, province, created_time, creator, revised_time, reviser from person where id = ?
2023-10-15T21:45:38.106+08:00 DEBUG 11238 --- [           main] n.j.m.l.o.m.P.selectByPrimaryKey         : ==> Parameters: 6(Integer)
2023-10-15T21:45:38.119+08:00 DEBUG 11238 --- [           main] n.j.m.l.o.m.P.selectByPrimaryKey         : <==      Total: 1
保存的人的名字为:jack
腾讯云推出云产品限时特惠抢购活动:2C2G云服务器7.9元/月起
本文链接:https://www.jhelp.net/p/RpXsAXW4Bi5AwVcZ (转载请保留)。
关注下面的标签,发现更多相似文章