MyBatis SQL statement builder

1. Create a class, using SQL class, simply create an instance to call methods generate SQL statements

package com.jwen.provider;

import com.jwen.domain.Video;
import org.apache.ibatis.jdbc.SQL;

/**
 * Video Build dynamic sql statement
 * / 
Public  class VideoProvider {

    /**
     * Update video dynamic statement
     * @param video
     * @return
     */
    public String updateVideo(final Video video){
        return new SQL(){{

            UPDATE("video");

            // conditions written. 
            IF (video.getTitle ()! = Null ) {
                SET("title=#{title}");
            }
            if(video.getSummary()!=null){
                SET("summary=#{summary}");
            }
            if(video.getCoverImg()!=null){
                SET("cover_img=#{coverImg}");
            }
            if(video.getViewNum()!=null){
                SET("view_num=#{viewNum}");
            }
            if(video.getPrice()!=null){
                SET("price=#{price}");
            }
            if(video.getOnline()!=null){
                SET("online=#{online}");
            }
            if(video.getPoint()!=null){
                SET("point=#{point}");
            }

            WHERE("id=#{id}");

        }}.toString();
    }
}

2. Use the corresponding annotations to use this example

@UpdateProvider (type = VideoSqlProvider.class, method = "updateVideo") update
@InsertProvider insert
@DeleteProvider delete
@SelectProvider inquiry

@UpdateProvider(type = VideoProvider.class,method = "updateVideo")
int update(Video Video);

Construction of additions and deletions to change search statement, the official website has a clear explanation, not repeat them here!

 

Reference article: https://www.cnblogs.com/zhangminghui/p/4903351.html

Document official website: https://mybatis.org/mybatis-3/zh/statement-builders.html

W3CSchool:https://www.w3cschool.cn/mybatis/k8ay1im3.html

Guess you like

Origin www.cnblogs.com/jwen1994/p/12113756.html