Related articles

MyBatis series summary: MyBatis series


First, the entity class to achieve the addition, deletion, change and check

  • MyBatis # 2 is here

  • Preconditions with the same, here is not repeated, here only talk about the implementation!

1.1, add > insert

① Abstract interface

Integer addUserInfo(User user);
Copy the code

(2) the XML

  • Note how the keywords are written here

  • “Like” should be wrapped with a wave mark to prevent conflict with keywords!

 	<insert id="addUserInfo" parameterType="com.dbright.pojo.User">
        insert into user(name,age,`like`)values(#{name},#{age},#{like} )
    </insert>
Copy the code

(3) the test class

  • session.commit();This is a committed transaction! You have to, or the statement won’t work!
	@Test
    public void addUserInfo(a) {
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        / / data
        User user = new User();
        user.setName("Noodles");
        user.setAge(22);
        user.setLike("Honor of Kings");
        Integer statusNum = mapper.addUserInfo(user);
        System.out.println(statusNum);

        session.commit();// Commit the transaction
        session.close();
    }
Copy the code

④ Execution Result

1.2. Delete > Delete

① Abstract interface

Integer deleteUserInfo(int id);
Copy the code

(2) the XML

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

(3) the test class

  • The deletion also requires a committed transaction to take effect!
	@Test
    public void deleteUserInfo(a) {
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        Integer statusNum = mapper.deleteUserInfo(26);
        System.out.println(statusNum);

        session.commit();// Commit the transaction
        session.close();
    }
Copy the code

④ Execution Result

1.3. Change > update

① Abstract interface

Integer updateUserInfo(User user);
Copy the code

(2) the XML

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

(3) the test class

	@Test
    public void updateUserInfo(a) {
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        / / data
        User user = new User();
        user.setId(30);
        user.setName("Ding Da");
        user.setAge(23);
        user.setLike("Phishing");
        Integer statusNum = mapper.updateUserInfo(user);
        System.out.println(statusNum);

        session.commit();// Commit the transaction
        session.close();
    }
Copy the code

④ Execution Result

  • The original:

  • After the execution:

Query > select

① Abstract interface

	List<User> getUserInfo(a);

    User getUserInfoById(int id);

    User getUserInfoByIF(@Param("name") String name , @Param("age") Integer age);
Copy the code

(2) the 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>
Copy the code

(3) the test class

	@Test
   public void selectUser(a) {
       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(a) {
      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(a) {
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        User userInfo = mapper.getUserInfoByIF("Ding Da".23);
        System.out.println(userInfo);

        session.close();
    }
Copy the code

④ Execution Result

  • The data are as follows:

  • Query all information in the table

  • To be queried

  • Multi-condition Query

2. Map implementation of adding, deleting, modifying and checking

  • I won’t go through too many examples here, because mapper will automatically match the values in the map to the key

  • Very convenient, in the actual work, use Map query most

  • That is, if you use a tool like PostMan or Yapi

  • If you use Swaager, you must use entity classes for development

  • Example:

1) interface class

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

(2) the XML

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

(3) the test class

 	@Test
    public void addUserInfo(a) {
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        / / data
        Map<String,Object> map = new HashMap<>();
        map.put("name"."Noodles");
        map.put("age"."50");
        map.put("like"."Honor of Kings");
        Integer statusNum = mapper.addUserInfo(map);
        System.out.println(statusNum);

        session.commit();// Commit the transaction
        session.close();
    }

Copy the code

④ Execution Result


I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah