Mybatis动态SQL注释

动态SQL注解

脚本SQL

  • <script></script>标签中间按照之前sqlmap中的动态sql的样式书写
@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> selectStudent(Student s);

方法中构建sql

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

@UpdateProvider(type = StudentSql.class,method ="xiugaiStuSql")
public int updateStudentFun(Student s);

    // 内部类
	class StudentSql{
		public String xiugaiStuSql(Student s ) {
			String sql = "update student set";
			int count = 0;
			if(s.getSname() !=null) {
				sql+=",sname =#{sname}";
			}
			if(s.getBirthday() !=null) {
				sql+=",birthday = #{birthday}";
			}
			if(s.getSsex()!=null) {
				sql+=",ssex =#{ssex}";
			}
			if(s.getClassid()!=0) {
				sql+=",classid =#{classid}";
			}
			sql+=" where sid =#{sid}";
			
			sql = sql.replaceFirst(",", " ");
			
			return sql;
		}
    }
  • @UpdateProvider:

    1. 创建sql语句类

      该类包含需要动态生成的SQL 语句

    2. 创建Mapper接口类

      该类和配置文件的接口文件一样,用来处理数据库操作

    3. 利用@UpdateProvider

      将 SQL 语句类和 Mapper 接口类关联,利用@UpdateProvider的 type 属性和 method 属性

      • type:内部类名称
      • method :方法名称
    4. 测试验证

      编写测试类,测试动态生成的SQL 语句是否准确

  • 内部类:内部类最终要返回一个sql语句,即字符串类型

  • 新增:@InsertProvider、修改:@UpdateProvider

    删除:@DeleteProvider、查询:@SelectProvider

SQL语句构造器

  • 功能:

    解决 Java 代码中嵌入 SQL 语句,通过简单地创建一个实例来调用方法生成SQL语句

  • 特点:

    没有过多的使用类如 and的连接词

@SelectProvider(type = StudentSql.class,method = "selectByGZQ")
public List<Student> selectStuByGZQ(Student s);
	
@InsertProvider(type = StudentSql.class,method = "insertByGZQ")
@Options(keyProperty = "sid",useGeneratedKeys = true)
public int insertStuByGZQ(Student s);
	
@UpdateProvider(type = StudentSql.class,method = "updateByGZQ")
public int updateStuByGZQ(Student s);
	
@DeleteProvider(type = StudentSql.class,method = "deleteByGZQ")
public int deleteStuByGZQ(int sid);

// 内部类
class StudentSql{
		// sql构造器
		public String selectByGZQ(Student s) {

			return new SQL() {
				{
					SELECT("*");
					FROM("student");
					if(s.getSsex()!=null) {
						WHERE("ssex = #{ssex}");
					}
					if(s.getClassid()!=0) {
						WHERE("classid = #{classid}");
					}
				}
			}.toString();
		}
		
		public String insertByGZQ(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 updateByGZQ(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 deleteByGZQ(int sid) {
			return new SQL() {
				{
					DELETE_FROM("student");
					WHERE("sid = #{sid}");
				}
			}.toString();
		}
}

注意事项:

  1. sql构造器写在内部类中,返回值是String类型的sql语句

    return new SQL(){
        {
            //构造器构造sql语句
        }
    }.toString();
    

    里面的{}一定写,不写报错

  2. sql构造器的参数和接口参数一致

  3. 调用对应的sql语句用对应的@xxxProvider,type写内部类名,method写方法名

  4. sql语句构造器常用的方法

    在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeMonkey-D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值