mybatis study notes

resultType statementreturns the full class name or alias of the value type

  • resultType returns a map set, key is the column name, and value is the corresponding value

Using resultMap to realize joint table query

  • A mapping relationship is established between the resultMap query result set and the java Bean
    • Multi table query sql statement
      SELECT su.*,r.rolename FROM sys_user su,"role" r WHERE su.roleid AND =1;
    • Result set of query
  • Establish a mapping relationship between the result set and the entity class, and add the fields to be displayed in the User entity class, rolename and roleid
  • Use resultMap to establish the data to be displayed. property represents the attribute of the entity class and column represents the field of the table
  • Test class
  • console output

Note: the method in dao layer interface must correspond to the sql statement in mapper file. For example, the following error

  • junit reported the following error
  • console output
15:01:39.643 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j2.Log4j2Impl' adapter.
15:01:39.649 [main] DEBUG - Class not found: org.jboss.vfs.VFS
15:01:39.650 [main] DEBUG - JBoss 6 VFS API is not available in this environment.
15:01:39.650 [main] DEBUG - Class not found: org.jboss.vfs.VirtualFile
15:01:39.651 [main] DEBUG - VFS implementation is not valid in this environment.
15:01:39.651 [main] DEBUG - Using VFS adapter
15:01:39.652 [main] DEBUG - Find JAR URL: file:/D:/program/jee-2020-03/workspace/mybatis-demo/build/classes/cn/bdqn/pojo
15:01:39.652 [main] DEBUG - Not a JAR: file:/D:/program/jee-2020-03/workspace/mybatis-demo/build/classes/cn/bdqn/pojo
15:01:39.696 [main] DEBUG - Reader entry: Role.class
15:01:39.698 [main] DEBUG - Reader entry: User.class
15:01:39.699 [main] DEBUG - Listing file:/D:/program/jee-2020-03/workspace/mybatis-demo/build/classes/cn/bdqn/pojo
15:01:39.699 [main] DEBUG - Find JAR URL: file:/D:/program/jee-2020-03/workspace/mybatis-demo/build/classes/cn/bdqn/pojo/Role.class
15:01:39.699 [main] DEBUG - Not a JAR: file:/D:/program/jee-2020-03/workspace/mybatis-demo/build/classes/cn/bdqn/pojo/Role.class
15:01:39.700 [main] DEBUG - Reader entry: ����

The reason is that the method is written in the RoleMapper interface and the sql statement is not written in the corresponding xml file

  • RoleMapper.xml
  • When the column name of the database is different from the entity class, you need to use resultMap mapping
  • resultMap is also required for association mapping during joint query

Configure the log implementation of mybatis as log4j2, and sql statements will be printed during background printing and output

  • Use association to associate Java beans to realize query

    • When an entity class is an attribute of another entity class, such as:
  • Use the sub attribute association of resultMap to implement
  • You can also write a resultMap separately and reference it in the association, such as:
  • The test results are the same
  • When a collection exists in an entity class
  • Use the collection in the resultMap to associate the implementation
  • Test class
  • test result

The required data is displayed in resultmap, association and collection, and the unnecessary data can not be displayed

From Weizhi notes (Wiz)

Posted by spiffy577 on Sun, 15 May 2022 08:29:56 +0300