This is the 30th day of my participation in the Wenwen Challenge

The input mapping

  1. why
    • ParameterType can only pass in one parameter, or the wrapper class of a POJO if there are more than one
  2. What can be entered (that is, the parameterType parameter)
    • A simple type
    • hashmap
    • The packaging type of the POJO
  3. demand
    • To complete the comprehensive query of user information, it is necessary to import very complex query conditions (may include user information and other information, such as goods and orders).
  4. class
    1. Pojo class
      • Use reverse engineering to automatically generate POJO classes
      public class User {property method}Copy the code
    2. A wrapper class
      • Pojos are typically generated using reverse engineering
      • Instead of modifying the POJO directly after it is generated, attributes are added dynamically by adding a wrapper class
      public class UserCustom extends User{
      	
      	
      	// The user information can be extended
      }
      Copy the code
      1. Package type class
        • The wrapper class values above are applied to a single POJO class to dynamically add attributes
        • Wrapper type classes can wrap many different classes for multiple table queries
        • The following class wraps the UserCustom class, so it contains all the attributes of the User
        • For example, if you want to query user information according to the order number of the notebook, you can package the order class through this class to achieve multiple table queries at a time
      public class UserQueryVo {
      	
      	// Set the query criteria for the package
      	
      	// User query criteria
      	//UserCustom is a wrapper class for User with custom attributes, so use UserCustom to define variables
      	private UserCustom userCustom;
      
      	public UserCustom getUserCustom(a) {
      		return userCustom;
      	}
      
      	public void setUserCustom(UserCustom userCustom) {
      		this.userCustom = userCustom;
      	}
      	
      	// Can wrap other classes...
      	// Order, merchandise
      	
      }
                  
      Copy the code
  5. The mapping file
    • User.sex =#{userCustom.sex}
    • This is because the UserQueryVo class contains the userCustom property, which in turn contains the sex properties
    • User. sex is used because the sex property in the user table is used
<! -- Query user information -->
	<select id="findUserList" parameterType="cn.sju.po.UserQueryVo" resultType="cn.sju.po.UserCustom">
		select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
	</select>
Copy the code
  1. The test class
// Query user information comprehensively
	@Test
	public void testFindUserList(a) throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();

		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		UserQueryVo userQueryVo = new UserQueryVo();
		UserCustom userCustom = new UserCustom();
		userCustom.setSex("Female");
		userCustom.setUsername("Army");
		userQueryVo.setUserCustom(userCustom);
		List<UserCustom> list = userMapper.findUserList(userQueryVo);
		
		sqlSession.close();
		System.out.println(list);
	}
Copy the code



The output mapping

A, resultType

  1. Mapping requirements
    • ResultType is used for output mapping. The column can be mapped successfully only when the queried column name is the same as the attribute name in the POJO.
    • If the column name queried is inconsistent with the attribute name in the POJO, no POJO object is created.
    • Pojo objects are created as long as the queried column name matches one of the attributes in the POJO. (But the value of attributes with different names is null)
  2. Output simple type
    1. demand
      • The total number of comprehensive query lists for user information
    2. mapper.xml
    <! -- Query user information -->
    	<select id="findUserCount" parameterType="cn.sju.po.UserQueryVo" resultType="int">
    		select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    	</select>    
    Copy the code
    1. mapper.java
    // Query user information comprehensively
    	public int findUserCount(UserQueryVo userQueryVo) throws Exception;
    Copy the code
    1. The test code
    // Number of rows to query user information
    		@Test
    		public void testFindUserCount(a) throws Exception {
    			SqlSession sqlSession = sqlSessionFactory.openSession();
    
    			UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    			
    			UserQueryVo userQueryVo = new UserQueryVo();
    			UserCustom userCustom = new UserCustom();
    			userCustom.setSex("Female");
    			userCustom.setUsername("Army");
    			userQueryVo.setUserCustom(userCustom);
    			int i = userMapper.findUserCount(userQueryVo);
    			
    			sqlSession.close();
    			System.out.println(i);
    		}
    Copy the code
    1. summary
      • The result set is only one row and one column, and the output can be mapped using simple types.
  3. Output poJO objects and a list of POJOs
    • Whether the output is a single POJO object or a list (the list includes poJOs),
    1. ResultType specifies the same type in mapper. XML.
      • Both are of type User
    2. The methods specified in mapper.java return value types are different
      1. Outputs a single POJO object, and the method returns a single object type
      public User findUserById(int id) throws Exception;
      Copy the code
      1. Outputs the POJO object list. The method returns list
      public List<User> findUserByName(String name) throws Exception;
      Copy the code
    3. The generated dynamic proxy object determines whether to call selectOne(a call that returns a single object) or selectList (a call that returns a collection object) based on the return value type of the mapper method in the mapper.java file.

Second, the resultMap

  1. The principle of
    1. ResultType You can specify a POJO to map the query result to a POJO, but the attribute name of the POJO must be the same as the column name of the SQL query for the mapping to be successful.
    2. If the field name of an SQL query is inconsistent with the attribute name of a POJO, you can use a resultMap to map the field name to the attribute name. In essence, the query result needs to be mapped to a POJO object.
    3. ResultMap allows you to map query results to complex TYPES of POJOs. For example, poJOs and Lists are included in the query result mapping object to achieve one-to-one and one-to-many queries.
  2. Mapper.xml
    • : This property represents the unique identity of the query result set and is very important. Define multiple if multiple fields are compound unique constraints.
    • Property: Represents the Property of the Person class.
    • Column: indicates the name of the Column queried in SQL.
    • Column and property together map the SQL query fields to the specified POJO class properties.
    • : Ordinary results, which are attributes of poJOs.
<! ResultMap: Specifies the ID of the defined resultMap. If this resultMap exists in another mapper file, you need to add namespace in front of it.
<! SQL > select * from poJO;
	<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
		SELECT id id_,username username_ FROM USER WHERE id=#{value}
	</select>
<! Define a resultMap to remap to the same attribute name -->
<! Type: Indicates the type of the Java object to be mapped. Alias ID can be used: Unique identifier for resultMap -->
	 <resultMap type="user" id="userResultMap">
	 	<! Property: Type Specifies the attribute name of the specified POJO type. Finally, a mapping relationship is created between column and Property.
	 	<id column="id_" property="id"/>
	 	<! Property: type Specifies the attribute name of the specified POJO type. ResultMap specifies the mapping between column and property.
	 	<result column="username_" property="username"/>
	 
	 </resultMap>
Copy the code
  1. Map
public List<User> findUserListResultMap(a) throws Exception;
Copy the code