SSM framework learning MyBatis (two)

Last chapter review

We introduced a few things about MyBatis in MyBatis (1):

  • Persistence framework
  • Almost avoid all JDBC code and manual setting of parameters and obtaining result sets
  • The ORM idea is used to realize the encapsulation
    ORM of the result set : object Relational Mapping
  • Corresponding to the database table and the entity class and the attributes of the entity class, so that we can operate the entity class to realize the operation of the database table

Next, let's take a look at how the addition, deletion, modification, and checking operations in MyBatis are implemented:

In the last chapter, we introduced querying based on specified fields, querying all data, and inserting a piece of data. Do you have any impressions? Let us review it together!

First look at the directory structure: The
Insert picture description here
dao layer puts the interface , the declaration of the data operation method, the
pojo layer puts the entity class and the get and set methods corresponding to the entity. The
utils package stores the configuration file of MyBatis to read and obtain the SqlSessionFactory object. And other operations
[see MyBatis (1) for specific code]

The UserMapper statement in the dao layer:

1. Query according to the specified field

//    创建一个查询方法
    @Select("select * from user where id = #{id}")
    List<User> queryUserById(@Param("id") int id);

2. Query all data

    @Select("select * from user")
    List<User> findAll();

3. Insert a piece of data

    //创建一个插入的方法
    @Insert("insert into user(id,username,password) values(#{id},#{username},#{password})")
    int insertUser(Map map);

The realization of the test function:

1. Query according to the specified field

 @Test
    public void TestQuery(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.queryUserById(1);
        for (User user : userList) {
    
    
            System.out.println(user);
        }
        sqlSession.close();
    }

2. Query all data

    @Test
    public void findAll(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();
        for (User user : list) {
    
    
            System.out.println(user);
        }
        sqlSession.close();
    }

3. Insert a piece of data

@Test
    public void insertUser(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap map = new HashMap();
        map.put("id",3);
        map.put("username","user2");
        map.put("password","1234");
        mapper.insertUser(map);
        sqlSession.close();
    }

The above is part of the content in MyBatis (1), let us learn how to delete and modify data

1. Delete data with specified conditions

1. The first is to add a delete method statement in UserMapper.java

//    这是一个删除的方法
    @Delete("delete from user where id = #{id}")
    int deleteUser(@Param("id") int id);

2. Then implement this method in the test class

 @Test
    public void DeleteUser(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int count = mapper.deleteUser(1);
        if (count>0){
    
    
            System.out.println("User already be deleted!");
        }else{
    
    
            System.out.println("Delete failure!");
        }
        sqlSession.close();
    }

Let's take a look at what is
Insert picture description here
printed in the log. We can see from the printed log that the deleted SQL statement has been executed.

In order to verify whether to delete, we check the database:
Insert picture description here
The user information with id 1 has been deleted!

2. Modify a specified user information

1. First, add a statement to modify the method in UserMapper.java

//    这是一个修改方法
  @Update("update user set password = #{password} where id = #{id}")
  int updateUser(Map map);

2. Then implement this method in the test class

@Test
    public void UpdateUserInfo(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap map = new HashMap();
        map.put("password","test123");
        map.put("id",2);
        int count = mapper.updateUser(map);
        if (count>0){
    
    
            System.out.println("User already be updated!");
        }else{
    
    
            System.out.println("Update failure!");
        }
        sqlSession.close();
    }

Let's take a look at what is printed in the log.
Insert picture description here
There are two parameters (Parameters), password and id.
Among them, we need to change the password to test123

In the log, we can see that an update statement was executed

Let's see if the password of the user with id 2 in the database has been changed.
Insert picture description here
It can be seen that our modification operation was executed successfully!

to sum up:

1. Declare the operations and methods to be performed in the Mapper interface of the dao layer
2. Implement the corresponding methods in the test

Adding, deleting, modifying and checking will be frequently used in the later web projects integrated with the SSM framework, and it is more important, so I hope you will practice repeatedly! ! !

The above is the whole content of our use of MyBatis to add, delete, modify and check! In subsequent articles, I will continue to introduce some commonly used plug-ins, how to add, delete, modify and check in Mapper.xml, and how to realize paging and fuzzy queries. I hope you will continue to pay attention!

【Seeking attention! Please like it! Seek three links! ! !

[The specific code will be uploaded to gitee and github, and after the end of the content, I will upload it to the network disk, and the link will be placed in the comment area]

For more details, please pay attention:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45088667/article/details/109440192