在SpringBoot中自定义sql语句

关于在SpringBoot中自定义Sql语句


@Mapper
public interface MessageMapper {
    int countByExample(MessageExample example);

    int deleteByExample(MessageExample example);

    int deleteByPrimaryKey(String id);

    int insert(Message record);

    int insertSelective(Message record);

    List<Message> selectByExample(MessageExample example);

    Message selectByPrimaryKey(String id);

    int updateByExampleSelective(@Param("record") Message record, @Param("example") MessageExample example);

    int updateByExample(@Param("record") Message record, @Param("example") MessageExample example);

    int updateByPrimaryKeySelective(Message record);

    int updateByPrimaryKey(Message record);

    @Select("select *,count(*) as count from message WHERE toid = #{userId} GROUP BY formid ORDER BY created_date desc limit #{offset}, #{limit}")
    List<Message> selectConversationList(@Param("userId") String userId, @Param("offset") int offset, @Param("limit") int limit);

    @Update("update message set has_read = 1 where conversation_id = #{conversationId}")
    void updateMessageHasReadByConversationId(@Param("conversationId") String conversationId);
}
}

上面代码中selectConversationList()就是自定义的Sql,只需要在方法的上面加上注解@Select即可,当然还有其他的例如Delete,Update。参数怎样定义在上面也有写到

在Spring Boot整合MyBatis中,如果要使用自定义SQL语句,可以按照以下步骤进行操作: 1. 在Spring Boot的启动类上使用@MapperScan注解,并指定要扫描的mapper接口的包路径。例如,使用@MapperScan("com.springmybatis.mapper")注解来扫描com.springmybatis.mapper包下的mapper接口。 2. 创建一个Controller类,使用@RestController注解来标识这是一个Restful接口的控制器类。 3. 在Controller类中,使用@Autowired注解自动注入对应的Service实例。例如,使用@Autowired UserServiceImpl userService来注入UserServiceImpl实例。 4. 在Controller类中定义自定义SQL查询方法,并使用@GetMapping注解来标识这是一个GET请求的处理方法。例如,使用@GetMapping("/seAll")注解来处理/seAll的GET请求。 5. 在自定义SQL查询方法中,调用相应的Service方法执行自定义SQL查询。例如,使用userService.seAll()来执行自定义SQL查询,并返回查询结果。 通过以上步骤,你可以在Spring Boot整合MyBatis中使用自定义SQL查询语句。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot整合Mybatis-plus 且用xml自定义sql](https://blog.youkuaiyun.com/qq_47848696/article/details/117930682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [springboot 整合 mybatis 注解 sql 语句](https://blog.youkuaiyun.com/wsjzzcbq/article/details/91492700)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值