Java中使用动态mybatis语句经典用法

Posted 大树叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中使用动态mybatis语句经典用法相关的知识,希望对你有一定的参考价值。

在Java中使用动态mybatis语句访问DB的例子如下,官方建议不用XML.

package com.test.econtract.esign2bcore.dao;

import com.test.econtract.esign2bcore.dao.model.PlatformInfoDO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface PlatformInfoMapper 

   @Insert(value = 
           "<script> ",
           "INSERT INTO platform_info ",
           "<trim prefix='(' suffix=')' suffixOverrides=','> ",
           "<if test='platName!=null'>plat_name,</if> ",
           "<if test='appId!=null'>app_id,</if> ",
           "<if test='appSecrect!=null'>app_secrect,</if> ",
           "<if test='signFrom!=null'>sign_from,</if> ",
           "<if test='description!=null'>description,</if> ",
           "<if test='deletedAt!=null'>deleted_at,</if> ",
           "</trim> ",
           "VALUES ",
           "<trim prefix='(' suffix=')' suffixOverrides=','> ", 
           "<if test='platName!=null'>#platName,</if> ",
           "<if test='appId!=null'>#appId,</if> ",
           "<if test='appSecrect!=null'>#appSecrect,</if> ",
           "<if test='signFrom!=null'>#signFrom,</if> ",
           "<if test='description!=null'>#description,</if> ",
           "<if test='deletedAt!=null'>#deletedAt,</if> ",
           "</trim> ",
           "</script> ",
   )
   @Options(useGeneratedKeys = true, keyColumn = "id")
   Integer insert(PlatformInfoDO insert);

   @Update(value = 
           "<script> ",
           "UPDATE platform_info",
           "<set> ",
           "<if test='id!=null'>id = #id,</if> ",
           "<if test='platName!=null'>plat_name = #platName,</if> ",
           "<if test='appId!=null'>app_id = #appId,</if> ",
           "<if test='appSecrect!=null'>app_secrect = #appSecrect,</if> ",
           "<if test='signFrom!=null'>sign_from = #signFrom,</if> ",
           "<if test='description!=null'>description = #description,</if> ",
           "<if test='deletedAt!=null'>deleted_at = #deletedAt,</if> ",
           "</set> ",
           "WHERE id = #id ",
           "</script>", 
   )
   Integer update(PlatformInfoDO update);

   @Select(value = 
           "<script> ",
           "SELECT * FROM platform_info",
           "<where> ",
           "<if test='id!=null'>AND id = #id</if> ",
           "<if test='platName!=null'>AND plat_name = #platName</if> ",
           "<if test='appId!=null'>AND app_id = #appId</if> ",
           "<if test='appSecrect!=null'>AND app_secrect = #appSecrect</if> ",
           "<if test='signFrom!=null'>AND sign_from = #signFrom</if> ",
           "<if test='description!=null'>AND description = #description</if> ",
           "<if test='deletedAt!=null'>AND deleted_at = #deletedAt</if> ",
           "</where> ",
           "LIMIT 0,1 ",
           "</script>", 
   )
   PlatformInfoDO selectOne(PlatformInfoDO query);

   @Select("SELECT * FROM platform_info WHERE id = #id AND deleted_at = 0")
   PlatformInfoDO selectOneById(Integer id);

   @Select(value = 
           "<script> ",
           "SELECT * FROM platform_info",
           "<where> ",
           "<if test='id!=null'>AND id = #id</if> ",
           "<if test='platName!=null'>AND plat_name = #platName</if> ",
           "<if test='appId!=null'>AND app_id = #appId</if> ",
           "<if test='appSecrect!=null'>AND app_secrect = #appSecrect</if> ",
           "<if test='signFrom!=null'>AND sign_from = #signFrom</if> ",
           "<if test='description!=null'>AND description = #description</if> ",
           "<if test='deletedAt!=null'>AND deleted_at = #deletedAt</if> ",
           "</where> ",
           "</script>", 
   )
   List<PlatformInfoDO> selectMany(PlatformInfoDO query);

   @Delete(value = 
           "<script> ",
           "DELETE FROM platform_info",
           "<where> ",
           "<if test='id!=null'>AND id = #id</if> ",
           "<if test='platName!=null'>AND plat_name = #platName</if> ",
           "<if test='appId!=null'>AND app_id = #appId</if> ",
           "<if test='appSecrect!=null'>AND app_secrect = #appSecrect</if> ",
           "<if test='signFrom!=null'>AND sign_from = #signFrom</if> ",
           "<if test='description!=null'>AND description = #description</if> ",
           "<if test='deletedAt!=null'>AND deleted_at = #deletedAt</if> ",
           "</where> ",
           "</script>", 
   )
   Integer delete(PlatformInfoDO delete);


@Select(value = 
           "<script> ",
           "SELECT * FROM live_verify ",
           "WHERE deleted_at = 0 ",
           "<if test='liveVerify.id!=null'>AND id = #liveVerify.id</if> ",
           "<if test='liveVerify.fkId!=null'>AND fk_id = #liveVerify.fkId</if> ",
           "<if test='liveVerify.subFkId!=null'>AND sub_fk_id = #liveVerify.subFkId</if> ",
           "<if test='liveVerify.systemType!=null'>AND system_type = #liveVerify.systemType</if> ",
           "<if test='liveVerify.username!=null'>AND username = #liveVerify.username</if> ",
           "<if test='liveVerify.idNoType!=null'>AND idNo_type = #liveVerify.idNoType</if> ",
           "<if test='liveVerify.idNoEncrypt!=null'>AND idNo_encrypt = #liveVerify.idNoEncrypt</if> ",
           "<if test='liveVerify.orderNo!=null'>AND order_no = #liveVerify.orderNo</if> ",
           "<if test='liveVerify.verifyStatus!=null'>AND verify_status = #liveVerify.verifyStatus</if> ",
           "<if test='liveVerify.verifyFrom!=null'>AND verify_from = #liveVerify.verifyFrom</if> ",
           "<if test='liveVerify.verifyImage!=null'>AND verify_image = #liveVerify.verifyImage</if> ",
           "AND verify_status IN ",
           "<foreach collection='statusList' item='item' open='(' close=')' separator=',' > ",
           "#item ",
           "</foreach> ",
           "ORDER BY ID DESC LIMIT 0,1 ",
           "</script> "
   )
   LiveVerifyDO selectLiveVerifyByMultiResult(@Param("liveVerify") LiveVerifyDO liveVerifyDO, @Param("statusList") List<Integer> list);
   


 "<foreach collection='dataRecordList' open='(' close=')' separator=',' index='index' item='item' >",

以上是关于Java中使用动态mybatis语句经典用法的主要内容,如果未能解决你的问题,请参考以下文章

Java实战之路:SpringBoot项目中使用Mybatis打印Sql语句

mybatis 基本用法

Java EE开发平台随手记5——Mybatis动态代理接口方式的原生用法

MyBatis动态SQL————MyBatis动态SQL标签的用法

MyBatis-动态sql语句-if用法——MySQL系列学习笔记

MyBatis-动态sql语句-if用法——MySQL系列学习笔记