4.update更新和delete删除用法

Posted weihu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.update更新和delete删除用法相关的知识,希望对你有一定的参考价值。

一、update更新

UserMapper.java
package tk.mybatis.simple.mapper;

import org.apache.ibatis.annotations.Param;
import tk.mybatis.simple.model.SysRole;
import tk.mybatis.simple.model.SysUser;

import java.util.List;

/**
 * @author weihu
 * @date 2018/8/3/003 0:01
 */
public interface UserMapper {
   
    /**
     * 根据主键更新用户
     */
    int updateById(SysUser sysUser);

 
}

xml配置

UserMapper.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">
<!--sql语句映射文件-->

<!--因为已经配置了别名,所以resultType可以直接写类名,没有配置的话就要写类全名-->
<mapper namespace="tk.mybatis.simple.mapper.UserMapper">

    <!--根据主键更新用户-->
    <update id="updateById">
        update sys_user
        set user_name=#{userName},
            user_email=#{userEmail},
            user_info=#{userInfo},
            head_img=#{headImg,jdbcType=BLOB},
            create_time=#{createTime,jdbcType=TIMESTAMP}
        where id=#{id}
    </update>

</mapper>
UserMapperTest.java
package tk.mybatis.simple.mapper;

import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import tk.mybatis.simple.model.SysRole;
import tk.mybatis.simple.model.SysUser;

import java.util.Date;
import java.util.List;

/**
 * @author weihu
 * @date 2018/8/5/005 9:34
 * @desc
 */
public class UserMapperTest extends BaseMapperTest {

    /**
     * 根据主键更新用户
     */
    @Test
    public void updateUserById(){
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //创建一个用户对象
        SysUser sysUser=new SysUser();
        sysUser.setId(4L);
        sysUser.setUserName("马晓陪");
        sysUser.setUserEmail("[email protected]");
        int i = userMapper.updateById(sysUser);
        System.out.println("test"+i);
        //更新、添加、删除一定要commit提交事物,否则,数据无法写入到数据中
      sqlSession.commit();
     sqlSession.close();
} }

 

 

二、delete删除

UserMapper.java
package tk.mybatis.simple.mapper;

import org.apache.ibatis.annotations.Param;
import tk.mybatis.simple.model.SysRole;
import tk.mybatis.simple.model.SysUser;

import java.util.List;

/**
 * @author weihu
 * @date 2018/8/3/003 0:01
 */
public interface UserMapper {

    /**
     * 根据主键删除用户
     */
    int deleteUserById(Long id);
}

UserMapper.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">
<!--sql语句映射文件-->

<!--因为已经配置了别名,所以resultType可以直接写类名,没有配置的话就要写类全名-->
<mapper namespace="tk.mybatis.simple.mapper.UserMapper">

    <!--根据主键删除用户-->
    <delete id="deleteUserById">
        delete from sys_user where id=#{id}
    </delete>
</mapper>
UserMapperTest.java
package tk.mybatis.simple.mapper;

import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import tk.mybatis.simple.model.SysRole;
import tk.mybatis.simple.model.SysUser;

import java.util.Date;
import java.util.List;

/**
 * @author weihu
 * @date 2018/8/5/005 9:34
 * @desc
 */
public class UserMapperTest extends BaseMapperTest {


    /**
     * 根据主键删除用户
     */
    @Test
    public void deleteUserByIdTest(){
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int i = userMapper.deleteUserById(4L);
        //更新、添加、删除一定要commit提交事物,否则,数据无法写入到数据中
      sqlSession.commit();
        System.out.println("删除成功!"+i);
        sqlSession.close();
    }
}

 


以上是关于4.update更新和delete删除用法的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis-Plus

vue中detele删除对象属性时视图不能响应更新 - 解决办法

JS-[delete用法]

mysql:删除表数据droptruncate和delete的用法

更新和删除数据

更新和删除数据