MyBatis SQL语句参数注解详释
来源:互联网 发布:巴蒂尔数据 编辑:程序博客网 时间:2024/04/27 16:13
在Mybatis写SQL语句的时候,避免不了参数的传入。通常情况下,参数的传入可以分为两大类:
1、使用@Param注解
2、不使用@Param注解
下面详细介绍这两种方式的参数传递规则:
一、不使用@Param注解
1、接口方法只有一个参数
1) 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{任意字符}
- User getUserById(int id);
- // select * from table where id = #{任意字符}
- select * from table where id = #{id}
2)参数是对象类型
参数注释为: #{对象属性}
- // user.getName user.getAge
- User getUser(User user);
- select * from table where name = #{name} and age = #{age}
2、接口方法有多个参数
1)参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{参数位置[0..n-1]} 或者 #{param[1..n]}
- User getUser(String name, int age);
- // SQL
- select * from table where name = #{0} and age = #{1}
- // or
- select * from table where name = #{param1} and age = #{param2}
参数注释为: #{参数位置[0..n-1].对象属性} 或者 #{param[1..n].对象属性}
- User getUser(User user, booleanflag);
- // SQL
- select * from table where name = #{0.name} and age = {0.age} and flag = #{1}
- // or
- select * from table where name = #{param1.name} and age = {param1.age} and flag = #{param2}
二、使用@Param注解
1、接口只有一个参数
1)参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} 或者 #{param1}
- User getUserById(@Param("id")int id);
- select * from table where id = #{id}
- //or
- select * from table where id = #{param1}
参数注释为: #{注解名称.对象属性} 或者 #{param1.对象属性}
- // user.getName user.getAge
- User getUser(@Param("user")User user);
- select * from table where name = #{user.name} and age = #{user.age}
- //or
- select * from table where name=#{param1.name} and age = #{param1.age}
2、接口有多个参数
1)参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} 或者 #{param[1..n]}
2 )参数为自定义对象
参数注释为: #{注解名称.对象属性} 或者 #{param[1..n].对象属性}
- MyBatis SQL语句参数注解详释
- Mybatis SQL映射语句中参数注解使用规则
- 使用基于注解的mybatis实现根据参数动态生成sql语句
- MyBatis注解应用之动态SQL语句
- Mybatis增强型注解简化SQL语句
- MyBatis注解应用之动态SQL语句
- Mybatis基于注解的sql语句
- MyBatis SQL语句动态传入参数作表名。
- MyBatis向sql语句传多个参数方法
- mybatis中sql语句参数添加判断
- 模仿与学习MyBatis - 1.6 注解与Session:sql语句
- Mybatis基于注解结合Java编写Sql语句进行CRUD
- Mybatis基于注解形式的sql语句生成
- 让mybatis sql注解支持 IN 多参数传递
- mybatis注解动态sql
- Mybatis SQL映射语句中参数注释规则
- Mybatis SQL映射语句中参数注释规则
- Mybatis中如何在SQL语句表名中使用参数
- HDU 1394 Minimum Inversion Number 树状数组
- B格闪闪,给闪闪B格的你
- Android Design Support Library的简单使用
- Fat32递归遍历文件夹下的所有文件
- Android7.0 API新特性
- MyBatis SQL语句参数注解详释
- WinInet
- GitHub 上排名前 100 的 Android 开源库介绍
- unable to execute command: Segmentation fault: 11 linker command failed due to signal
- 使用JDBC将查询结果集保存为对象时需要注意的问题
- spring-bean生命周期
- 《从零开始学Swift》学习笔记(Day 12)——说几个特殊运算符
- Apicloud 中JSON的key值为数字时。的用法
- appium学习资源汇总