美文网首页
无废话mybatis四(insert,update,delete

无废话mybatis四(insert,update,delete

作者: 毛仑上保罗先生 | 来源:发表于2018-11-02 18:02 被阅读0次

    别说啦, 快上车
    UserMapper.java中添加接口

    
        public int insert(SysUser sysUser);
    
        public int insert2(SysUser sysUser);
    
        public int insert3(SysUser sysUser);
    
        public int updateByid(SysUser sysUser);
    
        public int deleteByid(long id);
    
    

    UserMapper.xml添加下列代码

    
    <insert id="insert">
          INSERT INTO sys_user(user_name,user_password,user_email,user_info,head_img,create_time)
          VALUES
          (
            #{userName},
            #{userPassword},
            #{userEmail},
            #{userInfo},
            #{headImg},
            #{createTime, jdbcType=DATE}
          )
        </insert>
        <insert id="insert2" useGeneratedKeys="true" keyProperty="id">
            INSERT INTO sys_user(user_name,user_password,user_email,user_info,head_img,create_time)
            VALUES
            (
            #{userName},
            #{userPassword},
            #{userEmail},
            #{userInfo},
            #{headImg},
            #{createTime, jdbcType=DATE}
            )
        </insert>
    
        <insert id="insert3">
            INSERT INTO sys_user(user_name,user_password,user_email,user_info,head_img,create_time)
            VALUES
            (
            #{userName},
            #{userPassword},
            #{userEmail},
            #{userInfo},
            #{headImg},
            #{createTime, jdbcType=DATE}
            )
            <selectKey keyColumn="id" resultType="long" keyProperty="id" order="AFTER">
                select LAST_INSERT_ID()
            </selectKey>
        </insert>
    
        <update id="updateByid">
            update sys_user set user_name=#{userName}
            where id=#{id}
        </update>
    
        <delete id="deleteByid">
          delete from sys_user where id = #{id}
        </delete>
    
    

    UserMapperTest.java 测试代码

    @Test
        public void testInsert() {
            SqlSession sqlSession = getSqlSession();
            try {
                SysUser sysUser = new SysUser();
                sysUser.setUserName("obj");
                sysUser.setUserInfo("ifno");
                sysUser.setUserEmail("obj@sina.com");
                sysUser.setUserPassword("123456");
                sysUser.setCreateTime(new Date());
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                int result = userMapper.insert(sysUser);
                System.out.println(result);
            } finally {
                sqlSession.close();
            }
        }
    
        @Test
        public void testInsert2() {
            SqlSession sqlSession = getSqlSession();
            try {
                SysUser sysUser = new SysUser();
                sysUser.setUserName("obj");
                sysUser.setUserInfo("ifno");
                sysUser.setUserEmail("obj@sina.com");
                sysUser.setUserPassword("123456");
                sysUser.setCreateTime(new Date());
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                int result = userMapper.insert2(sysUser);
                System.out.println(result);
                System.out.println("返回数据库生成的id" + sysUser.getId());
            } finally {
                sqlSession.commit();
                sqlSession.close();
            }
        }
    
        @Test
        public void testInsert3() {
            SqlSession sqlSession = getSqlSession();
            try {
                SysUser sysUser = new SysUser();
                sysUser.setUserName("obj");
                sysUser.setUserInfo("ifno");
                sysUser.setUserEmail("obj@sina.com");
                sysUser.setUserPassword("123456");
                sysUser.setCreateTime(new Date());
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                int result = userMapper.insert3(sysUser);
                System.out.println(result);
                System.out.println("返回数据库生成的id" + sysUser.getId());
            } finally {
                sqlSession.commit();
                sqlSession.close();
            }
        }
    
        @Test
        public void testUpdateByid() {
            SqlSession sqlSession = getSqlSession();
            try {
                SysUser sysUser = new SysUser();
                sysUser.setId(Long.valueOf(1008));
                sysUser.setUserName("ddddddddddd");
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                int result = userMapper.updateByid(sysUser);
                System.out.println(result);
            } finally {
                sqlSession.commit();
                sqlSession.close();
            }
        }
    
        @Test
        public void testDeleteByid() {
            SqlSession sqlSession = getSqlSession();
            try {
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                int result = userMapper.deleteByid(Long.valueOf(1008));
                System.out.println(result);
            } finally {
                sqlSession.commit();
                sqlSession.close();
            }
        }
    
    

    insert,update,delete都是要求commit才行 ,因为如果不commit就是把结果保存在session中且返回结果的是0, 而且如果执行成功了返回的结果都是 1, 如果没有成功则是0
    现在我们看 xml文件的<insert id="insert2" useGeneratedKeys="true" keyProperty="id"> 这一行

    useGeneratedKeys="true"是返回生成的主键, keyProperty="id" 是将生成的主键赋值给id属性

    再看

    <selectKey keyColumn="id" resultType="long" keyProperty="id" order="AFTER">
                select LAST_INSERT_ID()
    </selectKey>
    

    其实这个和上面useGeneratedKeys="true"..... 是一样的 , 只是selectKey如果自己有逻辑的话会更好处理

    相关文章

      网友评论

          本文标题:无废话mybatis四(insert,update,delete

          本文链接:https://www.haomeiwen.com/subject/zpkqxqtx.html