美文网首页Java
玩转MyBatis(二):MyBatis实现“增删改查”的两种方

玩转MyBatis(二):MyBatis实现“增删改查”的两种方

作者: 废柴程序员 | 来源:发表于2021-07-15 15:27 被阅读0次

    一、实体类实现增删改查

    image.png

    1.1、增 > insert

    ①抽象接口

    Integer addUserInfo(User user);
    

    ②xml

    • 这里要注意关键字的书写方式
    • like要用括号来包起来,防止跟关键字引起冲突!
        <insert id="addUserInfo" parameterType="com.dbright.pojo.User">
            insert into user(name,age,`like`)values(#{name},#{age},#{like} )
        </insert>
    

    ③测试类

    • session.commit(); 这个是提交事务!必须得写,不然语句不会生效!
        @Test
        public void addUserInfo() {
            SqlSession session = MybatisUtils.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
    
            //造数据
            User user = new User();
            user.setName("面条");
            user.setAge(22);
            user.setLike("王者荣耀");
            Integer statusNum = mapper.addUserInfo(user);
            System.out.println(statusNum);
    
            session.commit();//提交事务
            session.close();
        }
    

    ④执行结果

    image.png image.png

    1.2、删 > delete

    ①抽象接口

    Integer deleteUserInfo(int id);
    

    ②xml

        <delete id="deleteUserInfo" parameterType="int">
            delete from user where id = #{id}
        </delete>
    

    ③测试类

    • 删除也需要提交事务才会生效!
        @Test
        public void deleteUserInfo() {
            SqlSession session = MybatisUtils.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
            Integer statusNum = mapper.deleteUserInfo(26);
            System.out.println(statusNum);
    
            session.commit();//提交事务
            session.close();
        }
    

    ④执行结果

    image.png

    1.3、改 > update

    ①抽象接口

    Integer updateUserInfo(User user);
    

    ②xml

        <update id="updateUserInfo" parameterType="com.dbright.pojo.User">
            update user set name = #{name},age = #{age},`like` = #{like} where id = #{id}
        </update>
    

    ③测试类

        @Test
        public void updateUserInfo() {
            SqlSession session = MybatisUtils.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
    
            //造数据
            User user = new User();
            user.setId(30);
            user.setName("丁大大");
            user.setAge(23);
            user.setLike("钓鱼");
            Integer statusNum = mapper.updateUserInfo(user);
            System.out.println(statusNum);
    
            session.commit();//提交事务
            session.close();
        }
    

    ④执行结果

    • 原先:
    image.png
    • 执行后:
    image.png

    1.4、查 > select

    ①抽象接口

        List<User> getUserInfo();
    
        User getUserInfoById(int id);
    
        User getUserInfoByIF(@Param("name") String name , @Param("age") Integer age);
    

    ②xml

        <select id="getUserInfo" resultType="com.dbright.pojo.User">
            select * from user
        </select>
    
        <select id="getUserInfoById" resultType="com.dbright.pojo.User">
            select
                   *
            from
                 user
            where
                id = #{id}
        </select>
    
        <select id="getUserInfoByIF" resultType="com.dbright.pojo.User">
            select
                   *
            from
                 `user`
            where
                `name` = #{name}
            and
                age = #{age}
        </select>
    

    ③测试类

        @Test
       public void selectUser() {
           SqlSession session = MybatisUtils.getSession();
           UserMapper mapper = session.getMapper(UserMapper.class);
           List<User> users = mapper.getUserInfo();
    
           for (User map: users){
               System.out.println(map);
          }
           session.close();
      }
      @Test
      public void getUserInfoById() {
          SqlSession session = MybatisUtils.getSession();
          UserMapper mapper = session.getMapper(UserMapper.class);
    
          User userInfo = mapper.getUserInfoById(30);
          System.out.println(userInfo);
    
          session.close();
      }
    
        @Test
        public void getUserInfoByIF() {
            SqlSession session = MybatisUtils.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
    
            User userInfo = mapper.getUserInfoByIF("丁大大",23);
            System.out.println(userInfo);
    
            session.close();
        }
    

    ④执行结果

    • 数据如下:
    image.png
    • 查询表里所有信息
    image.png
    • 待条件查询
    image.png
    • 多条件查询
    image.png

    二、Map实现增删改查

    • 这里不举太多例子了,因为mapper会自动将map中的值根据key对应起来
    • 十分方便,在实际工作中,使用Map查询居多
    • 前提是前后端联调使用的是PostMan或者Yapi等联调工具
    • 如果使用swaager联调,必须使用实体类来进行开发
    • 例子:

    ①接口类

    Integer addUserInfo(Map<String,Object> map);
    

    ②xml

        <insert id="addUserInfo" parameterType="map">
            insert into user(name,age,`like`)values(#{name},#{age},#{like} )
        </insert>
    

    ③测试类

        @Test
        public void addUserInfo() {
            SqlSession session = MybatisUtils.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
    
            //造数据
            Map<String,Object> map = new HashMap<>();
            map.put("name","面条");
            map.put("age","50");
            map.put("like","王者荣耀");
            Integer statusNum = mapper.addUserInfo(map);
            System.out.println(statusNum);
    
            session.commit();//提交事务
            session.close();
        }
    

    ④执行结果


    image.png

    作者:大鱼丶
    原文链接:https://juejin.cn/post/6982743087143452708

    相关文章

      网友评论

        本文标题:玩转MyBatis(二):MyBatis实现“增删改查”的两种方

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