Advanced usage of Mybatis statements

Environment: MySQL5.6, jdk1.8

Suggestion : all parameters plus@Param

The meaning of resultMap : The result obtained in the query is injected into the property through the property's setter method. If resultMap is not defined, although the database query is successful, it will return null

insert

Insert auto-incrementing primary key

If an auto-increment primary key is set, the value of the primary key field must be 0 when inserting, otherwise it will not auto-increment;

Returns the value of the auto-incrementing primary key after inserting a field

<insert id="insert" parameterType="com.iss.blog.po.Article" useGeneratedKeys="true" keyProperty="article.articleId">
    INSERT INTO ub_article
    (article_id,......
    article_create_time,article_modify_time
    )
    VALUES (#{article.articleId},#{article.userId},......
    #{article.articleCreateTime,jdbcType=TIMESTAMP}, #{article.articleModifyTime,jdbcType=TIMESTAMP})
</insert>

Set useGeneratedKeys="true"and specify the primary key keyProperty; the setter method that will be called after successful insertion will keyPropertybackfill the primary key. Note that it must be added @Param, otherwise an error will be reported.
Returns 1 if the insertion is successful; returns 0 if it fails

Insert time field

Three steps:
use class fields java.util.Date, use database fields DATETIME, and use timestamp in the inserted value, such as#{article.articleCreateTime,jdbcType=TIMESTAMP}

The MySQL 5.7 version does not handle the time field well. If there is any problem, you can refer to the default value of the datetime in the version after mysql 5.7 to set '0000-00-00'

Insert if not present

 insert IGNORE into bd_article_tag_map (article_id,article_tag_id)
        values (#{articleTagMap.articleId}, #{articleTagMap.articleTagId})
        

The key is to set the database field to unique, where the combination of 2 fields is unique; according to the unique field to determine whether to insert, the success of the insertion returns 1, and the failure returns 0;

update

Update if field is not empty

<update id="updateByArticleId" parameterType="com.iss.blog.po.Article">
    UPDATE ub_article
    <set>
        <if test="articleTitle!=null">
            article_title =#{articleTitle},
        </if>
        <if test="articleSummary!=null">
            article_summary=#{articleSummary},
        </if>
        <if test="articleClick!=-1">
            article_click=#{articleClick},
        </if>
    </set>
    WHERE article_id=#{articleId}
</update>

Note: The comma after the field cannot be omitted

delete

Usage of foreach

interface method

public int deleteIfNotExist(@Param("articleId")int articleId,@Param("list") List<Integer> tagIdList);

xml

<delete id="deleteIfNotExist" parameterType="java.util.List">
    DELETE  FROM bd_article_tag_map
    WHERE article_id=#{articleId}
    AND article_tag_id NOT IN
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
    </foreach>
</delete>

select

left join union query

The meaning of join: add the attributes of the two tables.
The meaning of left join: all the attributes of the left table will be displayed, and if the attributes of the right table are missing, fill in null

<select id="selectByArticleId" resultMap="TagResultMap" parameterType="int">
    SELECT * FROM  bd_article_tag t1
    LEFT JOIN bd_article_tag_map t2 ON t1.article_tag_id = t2.article_tag_id
    WHERE t2.article_Id = #{articleId}
 </select>

The following article_id field exists in both tables and must be prefixed with the table; here the table prefix needs to be written as the alias of the table, otherwise an error will be reported that this field cannot be found.

  <select id="selectArticleListByArticleTagId" resultMap="ArticleResultMap" parameterType="int">
    SELECT t1.article_id,user_id,article_archive_id,
    article_title,
    article_summary,article_click,article_status,
    article_type,article_publish,article_original,
    article_create_time,article_modify_time
    FROM ub_article t1
    LEFT JOIN bd_article_tag_map t2 ON t1.article_id = t2.article_id
    WHERE t2.article_tag_id = #{articleTagId}
</select>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325067414&siteId=291194637