User getUserById(int id);
// 注:该中情况下'任意字符'不能为空,否则报错
// select * from <TABEL> where id = #{任意字符}
select * from <TABLE> where id = #{id}
User getUser(User user); // user.getName user.getAge
select * from <TABLE> where name = #{name} and age = #{age}
1.2 使用`@Param`注解
1.2.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} | #{param1}
1.2.2 参数为自定义对象
参数注释为: #{注解名称.对象属性} | #{param1.对象属性}
示例:
User getUserById(@Param(value="keyId") int id);
select * from <TABEL> where id = #{keyId}
// or
select * from <TABLE> where id = #{param1}
User getUser(@Param(value="usr") User user); // user.getName user.getAge
select * from <TABLE> where name = #{user.name} and age = #{user.age}
// or
select * from <TABLE> where name = #{param1.name} and age = #{param1.age}
2. 接口方法有两个及两个以上参数
2.1 不使用`@Param`注解
2.1.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{参数位置[0..n-1]} | #{param[1..n]}
2.1.2 参数为自定义对象
参数注释为: #{参数位置[0..n-1].对象属性} | #{param[1..n].对象属性}
示例:
User getUser(String name, int age);
select * from <TABLE> where name = #{0} and age = #{1}
// or
select * from <TABLE> where name = #{param1} and age = #{param2}
User getUser(User usr, int flag);
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}
2.2 使用`@Param`注解
2.2.1 参数为基本类型或为基本包装类型(int,Integer,String...)
参数注释为: #{注解名称} | #{param[1..n]}
2.2.2 参数为自定义对象
参数注释为: #{注解名称.对象属性} | #{param[1..n].对象属性}
示例:
User getUser(@Param(value="xm") String name, @Param(value="nl") int age);
select * from <TABLE> where name = #{xm} and age = #{nl}
// or
select * from <TABLE> where name = #{param1} and age = #{param2}
// or
select * from <TABLE> where name = #{xm} and age = #{param2}
User getUser(@Param(value="usr") User user, @Param(value="tag") int flag);
select * from <TABLE> where name = #{usr.name} and age = #{usr.age} and flag = #{tag}
// or
select * from <TABLE> where name = #{param1.name} and age = #{param1.age} and flag = #{param2}
// or
select * from <TABLE> where name = #{usr.name} and age = #{param1.age} and flag = #{param2}
2.2.3 部分参数使用`@Param`注解
当采用部分参数使用`@Param`注解时,参数注释为将以上两种情况结合起来即可。
示例:
User getUser(String name, @Param(value="nl") age, int gendar);
// 对于age的访问不能是 #{1} 只能是 #{param2} | #{nl}
select * from <TABLE> where name = #{0} and age = #{nl} and gendar = #{param3)