The difference between #{} and ${} in Mybatis and in-depth parameters

#{} and ${} difference

   #{} represents a placeholder, through #{} you can set the value of preparedStatement to the placeholder, and automatically convert between java type and jdbc type. #{} can effectively prevent sql injection. #{}Can receive simple type values ​​or pojo attribute values. If parameterType transmits a single simple type value, #{} brackets can be value or other names.
   ${} means splicing SQL strings. The content passed in parameterType can be spliced ​​into SQL through ${} without jdbc type conversion, and it will cause SQL injection problems. ${} can receive simple type values ​​or pojo attribute values. If parameterType transmits a single simple type value, ${} can only be value in parentheses.

parameterType

   The sql statement passing parameter is set through this property. The value of this property can be a basic type, a reference type (for example: String type), or an entity type (POJO type). At the same time, you can also use the packaging class of the entity class. Basic types and String We can write the type name directly, or use the package name. The method of class name, for example: java.lang.String. In the previous chapter, add, delete, modify, and check the parameters passed by the user according to the id query. The parameter passed in is Integer. When the parameter passed in by the user is saved, the entity class User can also be passed in the pojo wrapper class object, as shown below For the maven project, please refer to the previous chapter:
QueryVo packaging class:

/**
 * @ClassName QueryVo
 * @description: 该类中包含一个对象属性
 * @author: zjw
 * @Date 2021/2/25 19:04
 * @Version 1.0
 **/
public class QueryVo implements Serializable {
    
    
    //该属性是一个对象
    private User user;
    public User getUser() {
    
    
        return user;
    }
    public void setUser(User user) {
    
    
        this.user = user;
    }
}

Add methods to the UserMapper interface:

    /*** 
    * @Description 
    * @Param: [vo]
    * @return: java.util.List<com.zy.domin.User>
    * @Date: 2021/2/26
    */ 
    List<User> findQueryVo(QueryVo vo);

The core configuration file of SqlMapConfig.xml is the same as the previous chapter. UserMappe.xml adds sql statement:

    <!--传入包装类-->
    <select id="findQueryVo" parameterType="com.zy.vo.QueryVo" resultType="com.zy.domin.User">
        select * from user where username like #{
    
    user.username}
    </select>

Test code:

@Test
    public void testFindQueryVo(){
    
    
        QueryVo vo = new QueryVo();
        User user = new User();
        user.setUsername("%小%");
        vo.setUser(user);
        for (User u:userMapper.findQueryVo(vo)){
    
    
            System.out.println(u);
        }
    }

Insert picture description here

resultType

The resultType attribute can specify the type of the result set, it supports basic types and entity class types, and it is useful for the previous crud. When the entity class attribute has a requirement, the entity class attribute name must be consistent with the database field, otherwise the encapsulation cannot be realized.
dao interface:

    /***
    * @Description 查询所有
    * @Param: []
    * @return: java.util.List<com.zy.domin.User>
    * @Date: 2021/2/22
    */
    List<User> findAll();

Mapping configuration:

    <!--查询所有-->
    <select id="findAll" resultType="com.zy.domin.User">
        select * from user
    </select>

SqlMapConfig.xml, jdbc.properties, log4j.properties configuration files are the same as in the previous chapter.
The entity class, attributes and database fields are inconsistent:

public class User {
    
    
    private Integer uid;
    private String userName;
    private Date userBirthday;
    private String userSex;
    private String userAddress;
    //getXXX,setXXX,toString()方法此处省略
}

Test code:

    @Test
    public void testFindAll(){
    
    
        List<User> list = userMapper.findAll();
        for (User user : list){
    
    
            System.out.println(user);
        }
    }

Insert picture description here
From the test results, it can be seen that only userName has a value. This is because the MySQL database is case-insensitive under the Windows system. However, other attributes have no value. How to solve it, we can query by alias:
modify the mapping configuration:

    <!--查询所有-->
    <select id="findAll" resultType="com.zy.domin.User">
        select id as uid,
        username as userName,
        birthday as userBirthday,
        sex as userSex,
        address as userAddress 
        from user
    </select>

Execute the test code again to see the result:
Insert picture description hereThinking: If we have a lot of queries, will it be troublesome to use aliases? Is there any other solution? Please see below.

resultMap

The resultMap tag can establish a corresponding relationship when the query column name and the attribute name of the entity class are inconsistent. In order to achieve packaging. Use the resultMap attribute to specify the reference in the select tag. At the same time, resultMap can map query results to complex types of pojo. For example, including pojo and list in the query result mapping object to achieve one-to-one query and one-to-many query.
Mapping configuration:

<mapper namespace="com.zy.dao.UserMapper">
    <resultMap id="userMap" type="com.zy.domin.User">
        <!--
        id 标签:用于指定主键字段
        result 标签:用于指定非主键字段
        column 属性:用于指定数据库列名
        property 属性:用于指定实体类属性名称
        -->
        <id property="id" column="uid"/>
        <result property="userName" column="username"/>
        <result property="userBirthday" column="birthday"/>
        <result property="userSex" column="sex"/>
        <result property="userAddress" column="address"/>
    </resultMap>
    
    <!--查询所有-->
    <select id="findAll" resultMap="userMap">
        select id,username,birthday,sex,addressfrom user
    </select>
</mapper>

Execute the test code again to see the result:
Insert picture description hereCome on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/114072859