MyBatis逆向工程 Update四种更新方法

MyBatis逆向工程创建了四种更新方法分别为updateByExampleSelectiveupdateByExampleupdateByPrimaryKeySelectiveupdateByPrimaryKey四种方法。下面以拥有{id, name, author, prices, status}属性的Book类为例分别对四种方法进行解析

1、updateByExampleSelective

int updateByExampleSelective(@Param("record") Book record, @Param("example") BookExample example);

参数1:要更新成的Book类
参数2:更新设置的条件

下面分别为对应Mapper中的方法及对应的sql语句:

<update id="updateByExampleSelective" parameterType="map">
    update book
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.name != null">
        name = #{record.name,jdbcType=VARCHAR},
      </if>
      <if test="record.author != null">
        author = #{record.author,jdbcType=VARCHAR},
      </if>
      <if test="record.price != null">
        price = #{record.price,jdbcType=VARCHAR},
      </if>
      <if test="record.status != null">
        status = #{record.status,jdbcType=INTEGER},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>

对应的sql语句如下所示:

update book 
{id,name,author, price, status}  //要更新的属性中选择
set .....
where
条件

使用:

public class MyBatisTest {

    SqlSession session;
    InputStream in;


    BookMapper bookMapper;

    Book book;
    @Before
    public void init() throws Exception{

        in = Resources.getResourceAsStream("SqlMapConfig.xml");

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        SqlSessionFactory factory = builder.build(in);
        session = factory.openSession();
        bookMapper = session.getMapper(BookMapper.class);
    }

    @After
    public void destory() throws Exception{
        session.close();
        in.close();
    }
    /*
    * 测试更新数据 updateByExampleSelective updateByExample
    *             updateByPrimaryKeySelective updateByPrimaryKey 四个方法
    * */
    @Test
    public void testUpdateByExampleSelective(){
    	//创建要更新成的类
        Book newbook = new Book(1, "高性能MySql", "Baron Scbwart", "99", null);
        //创建条件类
        BookExample bookExample = new BookExample();
        //设置对应的where条件
        BookExample.Criteria criteria = bookExample.createCriteria();
        criteria.andIdEqualTo(1);
        criteria.andNameLike("%高%");
        //对数据进行更新
        bookMapper.updateByExampleSelective(newbook, bookExample);
        session.commit();
    }

更新结果:

Book{id=1, name='高性能MySql', author='Baron Scbwart', price='99', status=1}

2、updateByExample

对应的方法:

int updateByExample(@Param("record") Book record, @Param("example") BookExample example);

Mapper中的sql语句:

<update id="updateByExample" parameterType="map">
    update book
    set id = #{record.id,jdbcType=INTEGER},
      name = #{record.name,jdbcType=VARCHAR},
      author = #{record.author,jdbcType=VARCHAR},
      price = #{record.price,jdbcType=VARCHAR},
      status = #{record.status,jdbcType=INTEGER}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>

对应sql语句

update book 
id,name,author, price, status  //这里为更新所有属性
set .....
where
条件

由上可知updateByExampleupdateByExampleSelective参数功能类似,不同的是updateByExample给出的Book参数需要设置所有的属性,不能如上面那样status设置为null。

3、updateByPrimaryKeySelective

对应方法:

int updateByPrimaryKeySelective(Book record);

Mapper中的sql语句:

<update id="updateByPrimaryKeySelective" parameterType="com.zhangbing.bean.Book">
    update book
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="author != null">
        author = #{author,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

对应sql语句:

update book 
{id,name,author, price, status}  //要更新的属性中选择
set .....
where
id = #{id,jdbcType=INTEGER}

updateByPrimaryKeySelective只有一个Book参数,会根据传入的book的id更新对应的book,参数可以选择。

4、updateByPrimaryKeySelective

对应方法:

int updateByPrimaryKey(Book record);

Mapper中的sql语句:

  <update id="updateByPrimaryKey" parameterType="com.zhangbing.bean.Book">
    update book
    set name = #{name,jdbcType=VARCHAR},
      author = #{author,jdbcType=VARCHAR},
      price = #{price,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>

对应sql语句:

update book 
id,name,author, price, status  //要更新所有属性
set .....
where
id = #{id,jdbcType=INTEGER}

updateByPrimaryKey只有一个Book参数,会根据传入的book的id更新对应的book,参数不可以选择。

发布了44 篇原创文章 · 获赞 27 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42784951/article/details/102664124