Mybatis如何使用注解实现动态SQL配置?

2026-04-28 03:171阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计434个文字,预计阅读时间需要2分钟。

Mybatis如何使用注解实现动态SQL配置?

javaMybatis 动态注解主要利用 org.apache.ibatis.jdbc.SQL 类实现。通过 return new SQL(){} 返回,同时将拼接好的 SQL 语句通过 toString() 转换为 String 类型。例如,查询操作:

javapublic class UserProvider { public String getUserById(int id) { return new SQL() {{ SELECT(*); FROM(users); WHERE(id=#{id}); }}.toString(); }}

Mybatis如何使用注解实现动态SQL配置?

tt.text

Mybatis动态注解主要是通过org.apache.ibatis.jdbc.SQL这个类实现,通过return new SQL(){}进行返回,同时要把 拼接好的SQL语句通过toString()转换成String类型,例子: 查询: public class UserProvider { public String SelectWithSQL(final Map param){ return new SQL(){ { SELECT("*"); FROM("tb_user"); if(param.get("tb_id")!=null){ WHERE("tb_id=#{tb_id}"); } if(param.get("tb_name")!=null){ WHERE("tb_name=#{tb_name}"); } if(param.get("tb_sex")!=null){ WHERE("tb_sex=#{tb_sex}"); } if(param.get("tb_age")!=null){ WHERE("tb_age=#{tb_age}"); } } }.toString(); } } 上述代码中首先通过new SQL(){}进行返回一个动态的SQL语句,之后通过{}进行动态SQL的拼接,其中SELECT()、FROM() 这些都是该类中的常用方法,通过传入参数取值进行判断后返回相应的where语句,最终合成一个完整的SQL语句。 插入: public String InsertWithSQL(final User user){ return new SQL(){ { INSERT_INTO("tb_user"); if(user.getId()!=null){ VALUES("tb_id","#{id}"); } if(user.getName()!=null){ VALUES("tb_name","#{name}"); } if(user.getSex()!=null){ VALUES("tb_sex","#{sex}"); } if(user.getAge()!=null){ VALUES("tb_age","#{age}"); } } }.toString(); } 插入语句的动态SQL注解是通过多次VALUES进行操作的,根据传入的属性进行判断后才进行插入。 更新: public String UpdateWithSQL(final User user){ return new SQL(){ { UPDATE("tb_user"); if(user.getId()!=null){ SET("tb_id=#{id}"); } if(user.getName()!=null){ SET("tb_name=#{name}"); } if(user.getSex()!=null){ SET("tb_sex=#{sex}"); } if(user.getAge()!=null){ SET("tb_age=#{age}"); } WHERE("tb_id=#{id}"); } }.toString(); } 和插入没什么太大的区别,主要是通过SET元素进行修改。 删除: public String DeleteWithSQL(final User user){ return new SQL(){ { DELETE_FROM("tb_user"); if(user.getId()!=null){ WHERE("tb_id=#{id}"); } if(user.getName()!=null){ WHERE("tb_name=#{name}"); } if(user.getSex()!=null){ WHERE("tb_sex=#{sex}"); } if(user.getAge()!=null){ WHERE("tb_age=#{age}"); } if(user.getId()!=null){ WHERE("tb_id=#{id}"); } } }.toString(); }

本文共计434个文字,预计阅读时间需要2分钟。

Mybatis如何使用注解实现动态SQL配置?

javaMybatis 动态注解主要利用 org.apache.ibatis.jdbc.SQL 类实现。通过 return new SQL(){} 返回,同时将拼接好的 SQL 语句通过 toString() 转换为 String 类型。例如,查询操作:

javapublic class UserProvider { public String getUserById(int id) { return new SQL() {{ SELECT(*); FROM(users); WHERE(id=#{id}); }}.toString(); }}

Mybatis如何使用注解实现动态SQL配置?

tt.text

Mybatis动态注解主要是通过org.apache.ibatis.jdbc.SQL这个类实现,通过return new SQL(){}进行返回,同时要把 拼接好的SQL语句通过toString()转换成String类型,例子: 查询: public class UserProvider { public String SelectWithSQL(final Map param){ return new SQL(){ { SELECT("*"); FROM("tb_user"); if(param.get("tb_id")!=null){ WHERE("tb_id=#{tb_id}"); } if(param.get("tb_name")!=null){ WHERE("tb_name=#{tb_name}"); } if(param.get("tb_sex")!=null){ WHERE("tb_sex=#{tb_sex}"); } if(param.get("tb_age")!=null){ WHERE("tb_age=#{tb_age}"); } } }.toString(); } } 上述代码中首先通过new SQL(){}进行返回一个动态的SQL语句,之后通过{}进行动态SQL的拼接,其中SELECT()、FROM() 这些都是该类中的常用方法,通过传入参数取值进行判断后返回相应的where语句,最终合成一个完整的SQL语句。 插入: public String InsertWithSQL(final User user){ return new SQL(){ { INSERT_INTO("tb_user"); if(user.getId()!=null){ VALUES("tb_id","#{id}"); } if(user.getName()!=null){ VALUES("tb_name","#{name}"); } if(user.getSex()!=null){ VALUES("tb_sex","#{sex}"); } if(user.getAge()!=null){ VALUES("tb_age","#{age}"); } } }.toString(); } 插入语句的动态SQL注解是通过多次VALUES进行操作的,根据传入的属性进行判断后才进行插入。 更新: public String UpdateWithSQL(final User user){ return new SQL(){ { UPDATE("tb_user"); if(user.getId()!=null){ SET("tb_id=#{id}"); } if(user.getName()!=null){ SET("tb_name=#{name}"); } if(user.getSex()!=null){ SET("tb_sex=#{sex}"); } if(user.getAge()!=null){ SET("tb_age=#{age}"); } WHERE("tb_id=#{id}"); } }.toString(); } 和插入没什么太大的区别,主要是通过SET元素进行修改。 删除: public String DeleteWithSQL(final User user){ return new SQL(){ { DELETE_FROM("tb_user"); if(user.getId()!=null){ WHERE("tb_id=#{id}"); } if(user.getName()!=null){ WHERE("tb_name=#{name}"); } if(user.getSex()!=null){ WHERE("tb_sex=#{sex}"); } if(user.getAge()!=null){ WHERE("tb_age=#{age}"); } if(user.getId()!=null){ WHERE("tb_id=#{id}"); } } }.toString(); }