一、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("maxiaopei@163.com"); 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(); } }