mybatis学习——增删改、别名

目录

  1. 别名
  2. INSERT
  3. DELETE
  4. UPDATE

一、别名

在mybatis.xml中配置,放在最开始
mybatis学习——增删改、别名_第1张图片


在增删改中无resultType返回值,默认为int类型
需要提交事务,数据才会发生改变:ss.commit();


二、INSERT

public void insertUserParam() {
        User user = new User();
        user.setUsername("刘六");
        user.setPassword("000");
        int n = ss.insert("dao.UserDao.insertUserParam", user);
        ss.commit();
        System.out.println(n);
    }

    INSERT INTO user(username,password) VALUES(#{username},#{password});

三、DELETE

public void deleteUser() {
        String name = "王五";
        int n = ss.delete("dao.UserDao.deleteUser", name);
        ss.commit();
        System.out.println(n);
    }
    
    <delete id="deleteUser" parameterType="String">
        DELETE FROM user WHERE
        username=#{0};
    delete>

四、UPDATE

public void updateUser() {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "李四");
        map.put("password", "888");

        int n = ss.update("dao.UserDao.updateUser", map);
        ss.commit();
        System.out.println(n);
    }

        UPDATE user SET
        password=#{password} WHERE username=#{username};
    

你可能感兴趣的:(mybatis)