[MyBatis Notes] Development with annotations

How to use annotations before methods

Bind the interface to the core configuration file:

The essence: use reflection

Bottom layer: dynamic proxy

Use annotations to add, delete, modify and check:

Auto-commit transactions: 

Tools:

public interface UserMapper {

    @Select("select * from user")
    //获取全部用户
    List<User>getUser();


//    ---------------------------------------------
    //方法存在多个参数,所有的参数前面必须加上@Param("")
    @Select("select * from user where id=#{id}")
    //获取id获取用户
    User getUserById(@Param("id") int id);
//  ------------------------------------------------

    @Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
    int addUser(User user);

// ---------------------------------------------------

    @Update("update user set name=#{name},pwd=#{pwd} where id=#{id}")
    int updateUser(Map<String,Object>map);
//------------------------------------------------------

    @Delete("delete from user where id=#{id}")
    int deleteUser(@Param("id")int id);
}
public class User {
    private int id;
    private String name;
    private String pwd;
    getter setter tostring;
}

 test class

    @Test
    public void getUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> user = mapper.getUser();
        for (User user1 : user) {
            System.out.println(user1);
        }
        sqlSession.close();
    }
    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User userById = mapper.getUserById(2);
        System.out.println(userById);
        sqlSession.close();
    }
    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(new User(6,"哈哈哈是的","76543"));
        sqlSession.close();
    }
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",2);
        map.put("name","2two");
        map.put("pwd","lasf");
        mapper.updateUser(map);
        sqlSession.close();
    }
    @Test
    public void delete(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(2);
        sqlSession.close();
    }

Mybatis detailed execution process:

  1. Resource get global configuration file

  2. Instantiate SqlsessionFactoryBuilder

  3. Parse configuration file stream XMLCondigBuilder

  4. Configration all configuration information

  5. SqlSessionFactory instantiation

  6. trasactional transaction management

  7. Create executor executor

  8. Create SqlSession

  9. Implement CRUD

  10. Check if the execution is successful

  11. commit transaction

  12. closure

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124366593