spring boot-mybatis三种动态sql(5)
脚本sql
@Select("<script>select * from user <if test=\"id !=null \">where id = #{id} </if></script>")
public List<User> findUserById(User user);
很明显,在java中写xml可读性和维护性太差,尤其当SQL很长时,这样写是很痛苦的。
在方法中构建sql
@Mapper
public interface MybatisDao {
//使用UserDaoProvider类的findUserById方法来生成sql
@SelectProvider(type = UserDaoProvider.class, method = "findUserById")
public List<User> findUserById(User user); class UserDaoProvider {
public String findUserById(User user) {
String sql = "SELECT * FROM user";
if(user.getId()!=null){
sql += " where id = #{id}";
}
return sql;
}
}
这比<script>更加清晰,适用于查询语句不是很长、条件不多的场景,SQL很直观。但是在写很长的SQL时,这样拼接SQL同样会很痛苦
结构化SQL
public String findUserById(User user) {
return new SQL(){{
SELECT("id,name");
SELECT("other");
FROM("user");
if(user.getId()!=null){
WHERE("id = #{id}");
}
if(user.getName()!=null){
WHERE("name = #{name}");
}
//从这个toString可以看出,其内部使用高效的StringBuilder实现SQL拼接
}}.toString();
}
List传值错误
@SelectProvider(type = UserDaoProvider.class, method = "find")
public List<Map> find(List list); class UserDaoProvider {
public String find(List list) {
这是一个最简单的list传参,但是在运行时会报传参错误。这是mybatis内部机制造成的,其参数需要是key/value结构,当遇到这里不是key/value结构的list时,mybatis会自己把它转换成key/value结构,key就是他的名字"list",value就是他的值List,要正确传参需要使用key/value结构的map,如下
@SelectProvider(type = UserDaoProvider.class, method = "find")
public List<Map> find(List list); class UserDaoProvider {
public String find(Map map) {
List list = (List) map.get("list");
spring boot-mybatis三种动态sql(5)的更多相关文章
- spring boot(8)-mybatis三种动态sql
脚本sql XML配置方式的动态SQL我就不讲了,有兴趣可以自己了解,下面是用<script>的方式把它照搬过来,用注解来实现.适用于xml配置转换到注解配置 @Select(" ...
- Spring Boot (10) mybatis三种动态sql
脚本SQL xml配置方式见mybatis讲解,下面是用<script>的方式把它照搬过来,用注解来实现.适于xml配置转换到注解配置 @Select("<script&g ...
- spring与mybatis三种整合方法
spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...
- Spring Boot + Mybatis多数据源和动态数据源配置
文章转自 https://blog.csdn.net/neosmith/article/details/61202084 网上的文章基本上都是只有多数据源或只有动态数据源,而最近的项目需要同时使用两种 ...
- Mybatis(六):spring与mybatis三种整合方法
1.采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean. spring-mybatis.xml: <?xml ve ...
- Spring boot 集成三种定时任务方式
三种定时任务方式分别为 org.springframework.scheduling.annotation.Scheduled java.util.concurrent.ScheduledExecut ...
- Spring boot 集成三种拦截方式
三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...
- Spring Boot实践——三种拦截器的创建
引用:https://blog.csdn.net/hongxingxiaonan/article/details/48090075 Spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可 ...
- spring与mybatis五种整合方法
1.采用数据映射器(MapperFactoryBean)的方式 不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数. (1)Spring配置文件: <!-- 引入jdbc ...
随机推荐
- 下载java生成PDF
/** * 下载打印PDF * @param request * @param response * @throws ServletException * @throws IOException * ...
- Java_集合操作_将元素插入List的指定位置
package test; import java.util.ArrayList; import java.util.List; public class test { public static v ...
- mysql 查询时间戳(TIMESTAMP)转成常用可读时间格式
from_unixtime()是MySQL里的时间函数 date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d' 主要是将返回值 ...
- LaTeX技巧206:使用gather输入多行公式的技巧
上文中提到了几个输入多行公式的环境,gather也是其中之一,gather输入的好处是每一行,他都会按照前文的编号计数器进行向下计数,这样保证了公式编号的连贯性.所以,当我们输入公式的每一行公式需要独 ...
- [转]Linux的SOCKET编程详解
From : http://blog.csdn.net/hguisu/article/details/7445768 1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在 ...
- perf 高级命令简介
perf 高级命令简介 1.使用 tracepoint 当 perf 根据 tick 时间点进行采样后,人们便能够得到内核代码中的 hot spot. 使用ls命令来演示 sys_enter 这个tr ...
- spring data jpa 查询No property ... found for...Did you mean '...'?
原文地址:https://blog.csdn.net/earthhour/article/details/79271816 实体类字段定义: private String sku_no; dao中接口 ...
- JavaScript:String 对象
ylbtech-JavaScript:String 对象 1.返回顶部 String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); ...
- 里诺全系列注册机+暗桩patch
一直有坛友私信更新里诺,今天花了一天时间,将里诺全系列更新完毕,权当送给坛友们的新年礼物吧! 全系列开放至元旦假期结束,后面就随机开放了. <ignore_js_op> 使用说明: 1.选 ...
- AppWidgetProvider 桌面插件 Widget 广播 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...