1. Problems encountered:

Use the role ID to obtain the menu ID in the ROLE_MENU table, and then use the menu ID to obtain the permission code of the menu item. Use Mybatis to connect and query, and the code is as follows

    // SysRoleMapper 
    // Query the corresponding permission code through the menu ID connection
    @Select("select menu_id from sys_role_menu where role_id = #{roleId}")
    @Results({ @Result(column = "menu_id",property = "permissionCode", one = @One(select = "com.project.system.mapper.SysMenuMapper.getPermissionCodeByMenuID")) })
    List<String> getPermissionByUserRole(Integer roleId);

    // Connect the query object SysMenuMapper
    @Select("select permission_code from sys_menu where id = #{menuID}")
    String getPermissionCodeByMenuID(@Param("menuID") Integer menuID);

Copy the code

Expected query a List < permissionCode >, but the return is a List < MenuID >, and print out the SQL statements, nor getPermissionCodeByMenuID this method the corresponding SQL statement execution.

2. Conclusion:

When using @Results for join queries, you should use an entity class or a Map object to accept the return value.

Due to their limited level, in the process of debugging source code, did not find mybatis is specific how to deal with the connection query,

My guess is that Mybatis has a place to judge the relationship between the returned type and the SQL statement. If there is no match, the SQL statement will not be executed directly. It will take too long to draw.

Change the code to the following, problem solved;

    @Select("select menu_id from sys_role_menu where role_id = #{roleId}")
    @Results({ @Result(column = "menu_id",property = "permissionCode", one = @One(select = "com.project.system.mapper.SysMenuMapper.getPermissionCodeByMenuID")) })
    List<Map<String,String>> getPermissionByUserRole(Integer roleId);
Copy the code

Print SQL statement:

3. Basic principle of Mybatis:

In the process, through debugging source code, see some information on the Internet, understand some mybatis framework and basic principles, here to make a small summary:

Mybatis is encapsulated on the basis of JDBC, provides a convenient and powerful API for users to operate the database;

Main member of MyBatis

  • Configuration All the Configuration information of MyBatis is saved in the Configuration object, and most of the Configuration in the Configuration file is stored in this class
  • SqlSessionAs the main top-level API of MyBatis work, it represents the session when interacting with the database, and completes the necessary function of adding, deleting, changing and searching the database
  • Executor MyBatis executer, the core of MyBatis scheduling, is responsible for the generation of SQL statements and the maintenance of query cache
  • StatementHandlerEncapsulates JDBC Statement operations and performs operations on JDBC Statements, such as setting parameters
  • ParameterHandler Is responsible for converting user-passed parameters to the corresponding data type of the JDBC Statement
  • ResultSetHandler Converts the ResultSet object returned by JDBC into a collection of type List
  • TypeHandler Responsible for mapping and conversion between Java data types and JDBC data types (dataflow column types, if you like)
  • MappedStatementMappedStatement maintain a < select | update | delete | insert > node encapsulation
  • SqlSource Is responsible for dynamically generating SQL statements based on user-passed ParameterObjects, encapsulating the information into BoundSql objects, and returning them
  • BoundSqlRepresents dynamically generated SQL statements and parameter information

MappedStatement object

SQL statements defined in XML or Mapper files are loaded into the memory during initialization and stored in the form of Map objects, which can be obtained by key when used later

  key = "com.project.mapper.SysRoleMapper.getRole"
  value = {MappedStatement@11731}...Copy the code

Reference: blog.csdn.net/luanlouis/a…