SSM-Mybatis(2)

Log
If a database operation, there has been an exception, we need debugging, logging is the best assistant
ever: sout, debug
now: Log factories
Insert picture description here
grasp

  • STDOUT_LOGGING
  • LOG4J
    Insert picture description here
    log4j
    What is Log4j?
  • We can control the log information delivery destination is the console
  • We can also control the output format of each log
  • By defining the level of each log information, we can control the log generation process in more detail
  • Configure flexibly through a configuration file without modifying the application code.

Paging to
reduce the amount of data

selsect * from user limit startIndex,pageSize
  1. interface
List<User> getUserByList(Map<String,Integer>map);
  1. Mapper.xml
<select id="getUserByList" parameterType="map"  resultMap="user">
        select * from mybatis.user limit #{startIndex},#{pageSize}
    </select>
  1. test
public void deleteUser(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Integer> map = new HashMap<String, Integer>;
        map.put("startIndex",1);
        map.put("pageSize",2);
        List<User>userList = mapper.getUserByLimit(map);
        for(User user:userList){
    
    
            System.out.println(user);
        }
    }

Annotation development
4. Annotations are implemented on the interface

public interface BlogMapper {
  @Select("SELECT * FROM blog WHERE id = #{id}")
  Blog selectBlog(int id);
}
  1. Need to bind the interface in the core configuration file
<mappers>
	<mapper class="com.kuang.dao.userMapper">
</mappers>

Essence: Realization of reflection mechanism.
Bottom layer: dynamic proxy, mapper can be regarded as dynamic proxy object

CRUD,
we can automatically commit transactions when the tools are created

	sqlSessionFactory.openSession(true);

Write interface, add annotations

	@Delete("delete from user where id = #{id}")
    int deleteUser(@Param("uid"),int id);

About @Param() annotation

  • Basic type of parameter or String type, you need to add
  • Reference type does not need to add
  • If there is only one basic type, it can be ignored, but it is recommended that everyone add
  • What we refer to in SQL is the attribute name set in our @param()

Guess you like

Origin blog.csdn.net/m0_46656833/article/details/112979832