MyBatis动态SQL

  1. 配置依赖
		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
  1. 在SpringBoot启动类添加MapperScan注解扫描mapper包
@MapperScan("com.exceeddata.rollout.api.mapper")
  1. 在Mapper类中使用动态SQL
package com.exceeddata.rollout.api.mapper;

import com.exceeddata.rollout.api.model.Rollout;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface RolloutMapper {

    @Insert("insert into rollout(user_id, type_id, software_id, software_version, project_id, version_module_id, roStatus)" +
            " values (#{userId}, #{typeId}, #{softwareId}, #{softwareVersion}, #{projectId}, #{versionModuleId}, #{roStatus})")
    @Options(useGeneratedKeys=true, keyProperty="rolloutId")
    Long insertRollout(Rollout rollout);

    @Update("update rollout set hawkbit_rollout_id = #{hawkbit_rollout_id} " +
            " where rollout_id = #{rollout_id} ")
    Long updateRolloutWithHawkbitDsId(@Param("hawkbit_rollout_id") final String hawkbitRolloutId,
                                      @Param("rollout_id") final Long rolloutid);

    @Update("update rollout set roStatus = #{roStatus} " +
            "where hawkbit_rollout_id = #{hawkbit_rollout_id} ")
    Long updateRoStatus(@Param("hawkbit_rollout_id") final String hawkbitRolloutId,
                        @Param("roStatus") final String roStatus);

    @Select("<script> select R.rollout_id as rolloutId, UNIX_TIMESTAMP(R.rollout_time)*1000 as rolloutTime, R.hawkbit_rollout_id as hawkbitRolloutId, R.software_id as softwareId, R.software_version as softwareVersion, R.project_id as projectId, R.version_module_id as versionModuleId, R.user_id as userId, R.roStatus " +
            "from  rollout as R " +
            "where R.type_id = #{type_id} " +
            "<if test=\"user_id != null\"> and R.user_id = #{user_id} </if>" +
            "<if test=\"software_id != null\"> and software_id = #{software_id} </if>" +
            "<if test=\"software_version != null\"> and software_version = #{software_version} </if>" +
            "<if test=\"roStatus != null\"> and roStatus = #{roStatus} </if>" +
            "order by R.rollout_time desc " +
            "limit #{limit} offset #{limitOffset} </script>")
    List<Rollout> getRolloutList(@Param("software_id") final String softwareId,
                                 @Param("software_version") final String softwareVersion,
                                 @Param("user_id") final String userId,
                                 @Param("type_id") final int rolloutType,
                                 @Param("roStatus") final String roStatus,
                                 @Param("limit") final int limit,
                                 @Param("limitOffset") final int limitOffset);

    @Select("<script> select count(1)" +
            "from  rollout as R " +
            "where R.type_id = #{type_id} " +
            "<if test=\"user_id != null\"> and R.user_id = #{user_id} </if>" +
            "<if test=\"software_id != null\"> and software_id = #{software_id} </if>" +
            "<if test=\"software_version != null\"> and software_version = #{software_version} </if>" +
            "<if test=\"roStatus != null\"> and roStatus = #{roStatus} </if>" +
            "order by R.rollout_time desc " +
            "limit #{limit} offset #{limitOffset} </script>")
    int getRolloutCount(@Param("software_id") final String softwareId,
                       	@Param("software_version") final String softwareVersion,
                       	@Param("user_id") final String userId,
                       	@Param("type_id") final int rolloutType,
                        @Param("roStatus") final String roStatus,
                       	@Param("limit") final int limit,
                       	@Param("limitOffset") final int limitOffset);

    @Select("select R.rollout_id as rolloutId, UNIX_TIMESTAMP(R.rollout_time)*1000 as rolloutTime, R.hawkbit_rollout_id as hawkbitRolloutId, R.software_id as softwareId, R.software_version as softwareVersion, R.project_id as projectId, R.version_module_id as versionModuleId, R.user_id as userId " +
            "from rollout as R " +
            "where rollout_id = #{rollout_id} ")
    Rollout getRollout(@Param("rollout_id") final int rolloutId);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值