SSM backend development (mybatis)

introduction

        This article mainly records some problems encountered in back-end development about the mybatis framework

question

        1. head_url and headUrl

        When creating a table for an entity, the ‘_’ symbol is often not used to name the attributes of the entity, and small camel case is often used.

ublic class User {
    @JsonProperty("head_url")
    @Schema(name = "head_url", description = "头像", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
    private String headUrl;
}

        But in json data, the ‘_’ symbol is often used to separate two nouns 

        The data sent from the front end is in the form of `head_url` and the attribute of the back-end user object is `headUrl`. Use JsonProperty to tell the Jackson serialization library to use `head_url` when converting the object to JSON;

<insert id="addUser" parameterType="com.cloud.dto.entity.user.User">
    INSERT INTO
    user (username, password, name, phone, head_url)
    VALUES (
        #{username},
        #{password},
        #{name},
        #{phone},
        #{headUrl}
    )
</insert>

        The above example is an SQL insertion. Head_url needs to be used in the brackets after user to match the annotations in the user entity, and #{headUrl} corresponds to the get/set headUrl method in the user entity.​ 

        2. Delete multiple records based on ID

        In the mysql database, you can operate multiple records through `in` in the sql statement

DELETE
FROM
`user`
WHERE `user`.id IN (18, 19)

        But using mybatis requires a special syntax—foreach

    <delete id="deleteUser" parameterType="java.util.List">
        DELETE
        FROM user
        WHERE user.id IN
        <foreach collection="userIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

         Use collection to receive the parameters passed to mybatis, use open, separator and close to deconstruct the collection data, and finally traverse through foreach

        3、Invalid bound statement (not found):com.cloud.mapper.evaluate.ProgramMapper. getProgramList

        This problem probably means that mybatis cannot map the mapper.xml file. Check it in the following order.

        1. Check whether the file structure is correct. The organizational form in the two mapper directories must be consistent.

        

        2. Check whether the function name in mapper is consistent with the id in mapper.xml

        

        3. Check whether the mapper, service, and controller are annotated correctly. There are no @Mapper, @Service, and @RestController respectively.

        4. Check whether the namespace in mapper.xml is consistent with its own location. I put it in the wrong location when I first created the xml file, so the namespace has the wrong path. After storing the location, the namespace will not change.

Guess you like

Origin blog.csdn.net/2201_75875170/article/details/134016335