mybtis动态SQL注解 脚本动态SQL\方法中构建SQL\SQL语句构造器

mybtis动态SQL注解

动态SQL注解

分类

  1. 脚本动态SQL :XML配置方式的动态SQL,是用<script>的方式把它照搬过来,用注解来实现。适用于xml配置转换到注解配置
  2. 方法中构建SQL:@SelectProvider 、@InsertProvider、@UpdateProvider、@DeleteProvider 这些方法的使用
  3. SQL语句构造器:实现动态 SQL 的内容

脚本动态SQL

在sql语句中加入<script></script>标签,按照之前sqlmap中的动态sql的样式书写;增删改查都一样

java 复制代码
@Select("<script>"
			+ "select * from student "
			+ "<where>"
			+ "<if test =\" ssex != null\">"
			+ "and ssex = #{ssex}"
			+ "</if>"
			+ "<if test =\" classId != 0\"> and classid = #{classId} </if>"
			+ "</where>"
			+ "</script>")
	public List<Student> findStudentJiaoBean(Student s);

方法中构建SQL

注解@SelectProvider@InsertProvider@UpdateProvider@DeleteProvider

用法

  1. 创建SQL语句类:该类包含需要动态生成的SQL 语句;
  2. 创建Mapper接口类:该类和配置文件的接口文件一样,用来处理数据库操作;
  3. 利用@SelectProvider:将 SQL 语句类和 Mapper 接口类关联,利用@SelectProvider 的 type 属性和 method 属性;
  4. 测试验证:编写测试类,测试动态生成的SQL 语句是否准确。
java 复制代码
//语法
@UpdateProvider(type = 内部类名.class,method = "方法名")

eg:
@UpdateProvider(type = StudentSql.class,method = "updatestuSqlFunc")
	public int updateStudentFunc(Student s);

class StudentSql{
		public String updatestuSqlFunc(Student s) {

			String sql = "update student set ";
			String sql1 = null;
			if(s.getBirthday() != null) {
				sql += "birthday = #{birthday},";
			}
			if(s.getClassId() != 0) {
				sql += "classid = #{classId},";
			}
			if(s.getSname() != null) {
				sql += "sname = #{sname},";
			}
			if(s.getSsex() != null) {
				sql += "ssex = #{ssex},";
			}
			sql1 = sql.substring(0, sql.length()-1);
			sql1 += " where sid = #{sid}";
			return sql1;
		}
	}

在接口中定义内部类,来构建需要的动态sql语句,比使用标签的方式结构更加清晰

SQL语句构造器

sql语句构造器的常用方法

属性名 说明
SELECT 开始或插入到 SELECT 子句,可以被多次调用,参数也会添加到 SELECT子句。
FROM 开始或插入到 FROM 子句,可以被多次调用,参数也会添加到 FROM 子句
WHERE 插入新的 WHERE 子句条件,可以多次被调用
OR / AND 使用 OR / AND 来分隔当前的 WHERE 子句的条件
DELETE_FROM 开始一个 delete 语句并指定需要从哪个表删除的表名。
INSERT_INTO 开始一个 insert 语句并指定需要插入数据的表名
VALUES 插入到 insert 语句中。第一个参数是要插入的列名,第二个参数则是该列的值。
UPDATE 开始一个 update 语句并指定需要更新的表名
SET 针对 update 语句,插入到 "set" 列表中
java 复制代码
语法(都一样):
@UpdateProvider(type = 内部类名.class,method = "方法名")
    
eg:
//构造方法(查询)
@UpdateProvider(type = StudentSql.class,method = "selectstuGZQ")
public List<Student> selectStudentGZQ(Student s);
	
//构造方法(新增)
@InsertProvider(type = StudentSql.class, method = "insertstuGZQ")
public int insertStudentGZQ(Student s);
	
//构造方法(修改)
@UpdateProvider(type = StudentSql.class,method = "updatestuGZQ")	
public int updateStudentGZQ(Student s);
	
//构造方法(删除)
@DeleteProvider(type = StudentSql.class,method = "deletestuGZQ")
public int deleteStudentGZQ(int sid);

class StudentSql{
		public String selectstuGZQ(Student s) {
			return new SQL() {
				{
					SELECT("sid","birthday");
					SELECT("ssex","sname");
					SELECT("classid");
					FROM("Student");
					if(s.getSsex() != null) {
						WHERE("ssex = #{ssex}");
					}
					if(s.getClassId() != 0) {
						WHERE("classid = #{classId}");
					}
				}
			}.toString(); 
		}
		
		public String insertstuGZQ(Student s) {
			return new SQL() {
				{
					INSERT_INTO("student");
					if(s.getSname() != null) {
						VALUES("sname","#{sname}");						
					}
					if(s.getBirthday() != null) {
						VALUES("birthday", "#{birthday}");				
					}
					if(s.getSsex() != null) {
						VALUES("ssex", "#{ssex}");						
					}
					if(s.getClassId() != 0) {
						VALUES("classid", "#{classId}");				
					}
				}
			}.toString();
		}
		
		public String updatestuGZQ(Student s) {
			return new SQL() {
				{
					UPDATE("student");
					if(s.getSname() != null) {
						SET("sname=#{sname}");
					}
					if(s.getBirthday() != null) {
						SET("birthday=#{birthday}");
					}
					if(s.getSsex() != null) {
						SET("ssex=#{ssex}");
					}
					if(s.getClassId() != 0) {
						SET("classid=#{classId}");
					}
					WHERE("sid = #{sid}");
				}
			}.toString();
		}
		
		public String deletestuGZQ(int sid) {
			return new SQL() {
				{
					DELETE_FROM("student");
					WHERE("sid = #{v}");
				}
			}.toString();
		}
	}
相关推荐
环能jvav大师8 分钟前
基于R语言的统计分析基础:使用SQL语句操作数据集
开发语言·数据库·sql·数据分析·r语言·sqlite
骆晨学长27 分钟前
基于springboot的智慧社区微信小程序
java·数据库·spring boot·后端·微信小程序·小程序
@月落33 分钟前
alibaba获得店铺的所有商品 API接口
java·大数据·数据库·人工智能·学习
楠枬43 分钟前
MySQL数据的增删改查(一)
数据库·mysql
goTsHgo1 小时前
从底层原理上解释 clickhouse 保证完全的幂等性
数据库·clickhouse
hayhead2 小时前
高频 SQL 50 题(基础版)| 626. 换座位
sql·力扣
阿华的代码王国2 小时前
MySQL ------- 索引(B树B+树)
数据库·mysql
Hello.Reader3 小时前
StarRocks实时分析数据库的基础与应用
大数据·数据库
执键行天涯3 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
yanglamei19623 小时前
基于GIKT深度知识追踪模型的习题推荐系统源代码+数据库+使用说明,后端采用flask,前端采用vue
前端·数据库·flask