import java.io.Serializable;
import org.apache.ibatis.type.Alias;
@Alias("role")
public class Role implements Serializable {
private static final long serialVersionUID = 598736524547906734L;
private Long id;
private String roleName;
private String note;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
public List<Role> findRolesByAnnotation(@Param("roleName") String rolename, @Param("note") String note);
<select id="findRolesByAnnotation" resultType="role">
select id,
role_name as roleName, note from t_role
where role_name like
concat('%', #{roleName}, '%')
and note like concat('%', #{note}, '%')
select>
public class RoleParams {
private String roleName;
private String note;
/**setter and getter**/
}
public List<Role> findRolesByBean(RoleParams roleParam);
<select id="findRolesByBean" parameterType="RoleParams"
resultType="role">
select id, role_name as roleName, note from t_role
where
role_name like
concat('%', #{roleName}, '%')
and note like concat('%',
#{note}, '%')
select>
sqlSession = SqlSessionFactoryUtils.openSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
RoleParams roleParam = new RoleParams();
roleParam.setNote("1");
roleParam.setRoleName("1");
List<Role> roles = roleMapper.findRolesByBean(roleParam);
有时我们需要混合使用几种方式,例如查询一个角色,可以使用角色名字和备注进行查询,与此同时还要支持分页,而分页参数的pojo如下:
public class PageParams{
private int start;
private int limit;
/**setter and getter**/
}
这时候的接口方法如下,使用注释和java bean混合的方式。
public List<Role> findByMix(@Param("params") RoleParams roleParams, @Param("page") PageParams PageParam);
对应的配置文件如下,注意这里对参数的引用方式。
<select id="findByMix" resultType="role">
select id, role_name as
roleName, note from t_role
where role_name like
concat('%',
#{params.roleName}, '%')
and note like concat('%', #{params.note}, '%')
limit #{page.start}, #{page.limit}
select>