mybatis注解开发-动态SQL

实体类以及表结构

在mybatis-config.xml中注册mapper接口

--------------------------

动态查询@SelectProvider

EmployeeMapper接口

package Intefaceproxy.Dyno;import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.SelectProvider;
import model.Employee;public interface EmployeeMapper {//动态查询  type:指定一个类    method:使用这个类中的selectWhitParamSql方法返回的sql字符串  作为查询的语句@SelectProvider(type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider.class,method="selectWhitParamSql")List<Employee> selectWithParam(Map<String,Object> param);
}

返回sql语句的类

package Intefaceproxy.Dyno;import java.util.Map;import org.apache.ibatis.jdbc.SQL;public class EmployeeDynaSqlProvider {//方法中的关键字是区分大小写的  SQL SELECT WHERE//该方法会根据传递过来的map中的参数内容  动态构建sql语句public String selectWhitParamSql(Map<String, Object> param) {return new SQL() {{SELECT("*");FROM("tb_employee");if (param.get("id")!=null) {WHERE("id=#{id}");}if(param.get("loginname")!=null) {WHERE("loginname=#{loginname}");}if(param.get("password")!=null) {WHERE("password=#{password}");}if(param.get("name")!=null) {WHERE("name=#{name}");}if(param.get("sex")!=null) {WHERE("sex=#{sex}");}if(param.get("age")!=null) {WHERE("age=#{age}");}if(param.get("phone")!=null) {WHERE("phone=#{phone}");}if(param.get("sal")!=null) {WHERE("sal=#{sal}");}if(param.get("state")!=null) {WHERE("state=#{state}");}}}.toString();}
}

测试:

当然也可以传递employee对象

接口:

//传递employee对象
@SelectProvider(type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider.class,method="selectWhitEmployeeSql")
List<Employee>selectWithEmployee(Employee employee);

返回sql的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//selectWhitEmployeeSql
    public String selectWhitEmployeeSql(Employee employee) {
        return new SQL() {
            {
                SELECT("*");
                FROM("tb_employee");
                if (employee.getId()!=null) {
                    WHERE("id=#{id}");
                }
                if(employee.getLoginname()!=null) {
                    WHERE("loginname=#{loginname}");
                }
                if(employee.getPassword()!=null) {
                    WHERE("password=#{password}");
                }
                if(employee.getName()!=null) {
                    WHERE("name=#{name}");
                }
                if(employee.getSex()!=null) {
                    WHERE("sex=#{sex}");
                }
            }
        }.toString();
    }

测试:

------------------------------

动态插入@InsertProvider

1
2
3
4
//动态插入
    @InsertProvider(type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider.class,method="insertEmployeeSql")
    @Options(useGeneratedKeys=true,keyProperty="id")
    int insertEmployee(Employee employee);

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//insertEmployeeSql
    public String insertEmployeeSql(Employee employee) {
        return new SQL() {
            {
                INSERT_INTO("tb_employee");
                if(employee.getLoginname()!=null) {
                    VALUES("loginname","#{loginname}");
                }
                if(employee.getPassword()!=null) {
                    VALUES("password""#{password}");
                }
                if(employee.getName()!=null) {
                    VALUES("name""#{name}");
                }
                if(employee.getSex()!=null) {
                    VALUES("sex""#{sex}");
                }
                if(employee.getAge()!=null) {
                    VALUES("age""#{age}");
                }
                if(employee.getPhone()!=null) {
                    VALUES("phone""#{phone}");
                }
                if(employee.getSal()!=null) {
                    VALUES("sal""#{sal}");
                }
                if(employee.getState()!=null) {
                    VALUES("state""#{state}");
                }
            }
        }.toString();
    }

测试:

-------------------------

@UpdateProvider

1
2
3
//动态更新
    @UpdateProvider(type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider.class,method="updateEmployeeSql")
    void updateEmployee(Employee employee);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//updateEmployeeSql
    public String updateEmployeeSql(Employee employee) {
        return new SQL() {
            {
                UPDATE("tb_employee");
                if(employee.getLoginname()!=null) {
                    SET("loginname=#{loginname}");
                }
                if(employee.getPassword()!=null) {
                    SET("password=#{password}");
                }
                if(employee.getName()!=null) {
                    SET("name=#{name}");
                }
                if(employee.getSex()!=null) {
                    SET("sex=#{sex}");
                }
                if(employee.getAge()!=null) {
                    SET("age=#{age}");
                }
                if(employee.getPhone()!=null) {
                    SET("phone=#{phone}");
                }
                if(employee.getSal()!=null) {
                    SET("sal=#{sal}");
                }
                if(employee.getState()!=null) {
                    SET("state=#{state}");
                }
                WHERE("id=#{id}");
            }
        }.toString();
    }

测试:

----------------------------

@DeleteProvider

1
2
3
//动态删除
@DeleteProvider(type=Intefaceproxy.Dyno.EmployeeDynaSqlProvider.class,method="deleteEmployeeSql")
void deleteEmployee(Employee employee);

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//deleteEmployeeSql
public String deleteEmployeeSql(Employee employee) {
    return new SQL() {
        {
            DELETE_FROM("tb_employee");
            if(employee.getLoginname()!=null) {
                WHERE("loginname=#{loginname}");
            }
            if(employee.getPassword()!=null) {
                WHERE("password=#{password}");
            }
            if(employee.getName()!=null) {
                WHERE("name=#{name}");
            }
        }
    }.toString();
}

 测试:

 

原文地址:https://www.cnblogs.com/Joke-Jay/p/8524722.html

mybatis注解开发-动态SQL相关推荐

  1. mybatis注解开发动态sql

    mybatis注解开发动态sql 本篇来讲一下如何使用mybatis注解模式中的动态sql 先来讲一下什么是动态sql 在我们实际开发的时候可能会出现很多方法需要一条很相似的sql语句来进行增删改查, ...

  2. Mybatis 注解开发 + 动态SQL

    Hello 大家好我是橙子同学,今天分享注解Mybatis注解开发+动态sql 目录 每文一铺垫(今天有小插曲哦) 注解开发 添加 @Insert 删除 @Delete 查询 @Select 修改 @ ...

  3. SSM—mybatis框架-注解开发-动态sql(where,set,trim,choose,when,foreach)-模糊查询写法-特殊符号处理-缓存

    文章目录 2.0.注解 2.1.动态sql 2.1.1.where 2.1.2.set 2.1.3.trim 2.1.3.1.trim的where 2.1.3.2.trim的set 2.1.4.1.c ...

  4. SpringBoot集成Groovy、Mybatis注解 实现动态SQL,帮你摆脱繁琐的XML配置

    SpringBoot的超简洁配置,为我们省去了宝贵的配置时间. Mybatis3在这方面也提供了很好的支持,通过注解让我们摆脱了繁琐的mapper xml,写DAO层的时候再也不用在java接口和xm ...

  5. MyBatis 注解实现动态SQL

    在 Mybatis 中,使用注解可以很方便的进行sql操作,但很多动态 SQL 都是由 xml 配置实现的.而随着 SpringBoot的逐渐发展,越来越多的配置由配置文件转成注解的形式.其中包括动态 ...

  6. Java神鬼莫测之MyBatis注解开发之动态SQL语句(六)

    1.Mybatis注解开发之动态SQL语句 背景:使用mybatis的注解开发动态Sql会比较麻烦, 很不方便, 所以不太推荐使用,该文章以查询作为案例,演示动态sql语句. 注意:Mybatis的动 ...

  7. MyBatis-学习笔记12【12.Mybatis注解开发】

    Java后端 学习路线 笔记汇总表[黑马程序员] MyBatis-学习笔记01[01.Mybatis课程介绍及环境搭建][day01] MyBatis-学习笔记02[02.Mybatis入门案例] M ...

  8. Mybatis注解开发(超详细)

    Mybatis注解开发 mybatis的常用注解 使用 Mybatis 注解实现基本 CRUD 项目目录结构 编写实体类 使用注解方式开发持久层接口 编写 SqlMapConfig.xml 配置文件 ...

  9. Mybatis注解开发指北

    Mybatis注解开发指北 目录 文章目录 Mybatis注解开发指北 @[toc] 0. Mybatis注解开发步骤 1. 导入相关配置文件 2. 配置数据库连接 3. 创建数据库对应的实体类(en ...

  10. SSM-MyBatis深层了解(使用注解开发和SQL构建对象)

    MyBatis进阶 今天给大家分享以下几个内容 注解实现单表开发 注解实现多表操作 MyBatis的SQL构建语句 先给大家介绍以下MyBatis注解开发的几个关键字 注解实现单表开发 在一个表中操作 ...

最新文章

  1. opencv_4.5.0/OpenCvSharp4.0 九点标定
  2. 递归和迭代_迭代与递归
  3. 计算机领域的范式,编程领域的范式转移
  4. hdi-shared Service plan的分配
  5. Python sys模块的使用
  6. python中none算变量吗_在python中对变量判断是否为None的三种方法总结
  7. php form 后台函数,Discuz!开发之后台表单生成函数介绍
  8. 使用 HTML5, javascript, webrtc, websockets, Jetty 和 OpenCV 实现基于 Web 的人脸识别
  9. php ajax session,Ajax处理用户session失效
  10. 限制码率的视频编码标准比较(包括MPEG-2,H.263, MPEG-4,以及 H.264)
  11. 安卓rtmp推流app_直播-腾讯云推流-sdk 播放地址不正确的解决方案---蜻蜓系统-uniapp-flutter通用...
  12. echarts graph图重叠_借官方关系图尝试下屏蔽鼠标浮在 links 上弹出的提示框
  13. xstream不映射字段_XStream序列化与反序列化对象
  14. centos转换linux格式,CentOS 下转换网易云音乐ncm格式为mp3
  15. Bilinear Pairing双线性配对的解释
  16. MySQL 的 help 命令你真的会用吗?
  17. zip文件类型如何恢复系统默认值--右键->打开方式中 无 “资源管理器”选项
  18. Golang 提取视频中音频,存为MP3格式 | Golang工具
  19. 迈阿密大学的计算机系咋样,美国迈阿密大学计算机专业本科.pdf
  20. UltraEdit常见问题及解决教程

热门文章

  1. 求C语言中的32个关键字及其意思?
  2. 2021-08-01创建查询关键字及数据类型
  3. java静态和动态的区别是什么_java中静态资源和动态资源的区别
  4. mysql php sdk_PHP连接MySQL数据库
  5. kubernetes 查看所有namespace、默认的namespace
  6. mybatis xml中大于、小于、if else的写法
  7. java 遍历 List 的六种方式 学习笔记
  8. mybatis的缓存基础
  9. Java面试题及答案2019_一般JAVA面试题及答案解析2019
  10. windows下安装python和mysql_Windows 10安装Python 2.7和MySQL-python