Mybatis application scenarios: dynamic parameter transfer, two-field query, user existence judgment

Table of contents

1. Dynamic parameter transfer

1. Scene description

2. Implementation process

 3. Code testing

2. Two-field query

1. Scene description

2. Implementation process

3. Code testing

4. Points to note

3. Judgment of user existence

1. Scene description

2. Implementation process

3. Code testing


1. Dynamic parameter transfer

1. Scene description

        When performing database queries, parameters need to be dynamically passed in, such as table names, column names or field values. For example: Find how many data records there are for the user named Tom in the student table, or how many users whose age is equal to 23. In this case, you need to dynamically pass parameters.

2. Implementation process

(1) If the table name and column name are dynamically passed in, the values ​​of the attribute variables in SQL will be changed to ${xxxx} instead of #{xxx}

<select id="testDym" parameterType="String" resultType="com.example.mplearning.entity.Student">
        select * from ${tableName} where ${column} = ${colVal}
    </select>

(2)Mpper file

Student testDym(Map<String, Object> map);

(3)Service layer

void testDym();
    @Override
    public void testDym() {
        Map<String, Object> map = new HashMap<>();
        map.put("tableName", "t_student");
        map.put("colVal",  "'" + "Tom" + "'");
        map.put("column", "name");
        System.out.println(orgMapper.testDym(map));
        map.put("column", "name");
        map.put("column", "age");
        map.put("colVal", "45");
        System.out.println(orgMapper.testDym(map));
    }

(4)Controller layer

@GetMapping("/testDym")
    public R testDym(){
        orgService.testDym();
        return R.ok();
    }

 3. Code testing

        Description: There is a user with name equal to Tom in the database, but there is no user with age=45, so the result is null. 

2. Two-field query

1. Scene description

        When using MyBatis to query the database, sometimes for the data table, only two columns of data need to be queried, one column is used as the key of the map, and the other column is used as the value of the map, and then the query results are returned in the form of a map.

2. Implementation process

(1) Assuming that you want to query the name and address, you can define a Map using name as key and address as value, and then use it in the query statement.

    <resultMap id="stuMap" type="HashMap">
        <result property="key" column="name" javaType="java.lang.String"/>
        <result property="value" column="address" javaType="java.lang.String" />
    </resultMap>

    <select id="selectTwoParam" resultMap="stuMap">
        select name, address from t_person;
    </select>

(2)Mapper file

List<Map<String, String>> selectTwoParam();

(3)Controller file

        The Service layer code will not be displayed. Just like the Mapper layer, the Controller layer code is directly displayed here.

    @GetMapping("/two")
    public R getTwoParam(){
        List<Map<String, String>> list = orgService.selectTwoParam();
        return R.ok().data( "data",list);
    }

3. Code testing

4. Points to note

        Regarding the query here, another thing to note is that some people will write this in the Mapper interface:

        This method of value is only suitable for encapsulation of one record, but is not suitable for multiple records. For example: If you select name, address from t_student where age = 25, and there is only one record of Student queried, then this is correct and the result can be obtained. However, if there are multiple Students, an error will be reported, as shown below:

        Therefore, in the case of multiple records, it needs to be written in the way used before, that is:

List<Map<String, String>> selectTwoParam();

3. Judgment of user existence

1. Scene description

        Sometimes in business scenarios, we need to make unique judgments. For example: when a user logs in, we need to judge whether the user exists in the user table, so there is no need to write select * from t_user where id = ?. Because in this case, if the efficiency of a data table with a particularly large amount of data is relatively slow, what method should be used? please look below.

2. Implementation process

(1) Define sql in the xml file.

    <select id="stuExist" parameterType="int" resultType="Integer">
        select 1 from t_student where id = #{id}
    </select>

Here is another way to write it, you can try:

<select id="findName" resultType="int" parameterType="String" >
     select IFNULL(sum(id),0) from user where name=#{name}
</select>

(2)Mapper file

    Integer stuExist(int id);

(3)Controller layer

    @GetMapping("/isExist/{id}")
    public R isExist(@PathVariable Integer id){
        Integer val = orgService.stuExist(id);
        return R.ok().data("message", val == null ? "用户不存在" : "用户存在");
    }

3. Code testing

Guess you like

Origin blog.csdn.net/weixin_47382783/article/details/133973018