XML查询参数:

parameterType:可以给出类别名,全名等.

resultType:查询结果,可以为 int,float,map等不可以与resultMap同时使用。

resultMap: 映射集的引用可以配置映射规则,级联,typeHandler等,是mybatis最复杂的元素。

本文返回是resultType。

查询方法可以在parameterType设置为单个参数XML设置为int,string等,多个参数可以设置为map

<select id="getRoleUseResultMap" parameterType="long" resultMap="roleMap">
select id, role_name, note from t_role where id = #{id}
</select>

<select id="findRolesByMap" parameterType="map" resultType="role">
select id, role_name as roleName, note from t_role
where role_name like
concat('%', #{roleName}, '%')
and note like concat('%', #{note}, '%')
</select>

或者多个参数避免map可读性差时可以用在Mapper java中注解(此时XML中没有parameterType)

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>

亦或是通过java bean传递多个参数:

public class RoleParams {
private String roleName;
private String note;

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;
}
}

mapper:

public List<Role> findRolesByBean(RoleParams roleParam);

XML:parameterType="com.ssm.chapter5.param.RoleParams".

<select id="findRolesByBean" parameterType="com.ssm.chapter5.param.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>

-----------------------------------------------------------------------------------------------

测试:

case 1:简单查询

<select id="getRole" parameterType="long" resultType="com.ssm.chapter5.pojo.Role">
select id,
role_name as roleName, note from t_role where id = #{id}
</select>
mapper:
public Role getRole(Long id);

public static void testGetRole() {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
Role role = roleMapper.getRole(1L);
System.out.println(role.getRoleName());
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}

case 2: map 查询:

    <select id="findRolesByMap" parameterType="map" resultType="role">
select id, role_name as roleName, note from t_role
where role_name like
concat('%', #{roleName}, '%')
and note like concat('%', #{note}, '%')
</select>
mapper:
public List<Role> findRolesByMap(Map<String, Object> parameterMap); test:
public static void testFindRolesByMap() {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
Map<String, Object> parameterMap = new HashMap<String, Object>();
parameterMap.put("roleName", "1");
parameterMap.put("note", "1");
List<Role> roles = roleMapper.findRolesByMap(parameterMap);
System.out.println(roles.size());
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}

case 3: 注解:

xml:
<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>
mapper:
public List<Role> findRolesByAnnotation(@Param("roleName") String rolename, @Param("note") String note); test:
public static void testFindRolesByAnnotation() {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
List<Role> roles = roleMapper.findRolesByAnnotation("1", "1");
System.out.println(roles.size());
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}

case 4: java bean:

java bean:
public class RoleParams {
private String roleName;
private String note; 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;
}
} xml:
<select id="findRolesByBean" parameterType="com.ssm.chapter5.param.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> mapper:
public List<Role> findRolesByBean(RoleParams roleParam); test
public static void testFindRolesByBean() {
SqlSession sqlSession = null;
try {
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);
System.out.println(roles.size());
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}

case 5: 混合使用 bean 和注解

bean 2:

public class PageParams {
private int start;
private int limit; public int getStart() {
return start;
} public void setStart(int start) {
this.start = start;
} public int getLimit() {
return limit;
} public void setLimit(int limit) {
this.limit = limit;
}
} xml:
<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> mapper:
public List<Role> findByMix(@Param("params") RoleParams roleParams, @Param("page") PageParams PageParam); test:
public static void testFindByMix() {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
RoleParams roleParam = new RoleParams();
roleParam.setNote("1");
roleParam.setRoleName("1");
PageParams pageParams = new PageParams();
pageParams.setStart(0);
pageParams.setLimit(100);
List<Role> roles = roleMapper.findByMix(roleParam, pageParams);
System.out.println(roles.size());
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}

最新文章

  1. js中this的使用
  2. redis + 主从 + 持久化 + 分片 + 集群 + spring集成
  3. mysql字段冲突报错
  4. 对于这个函数const int func(const int&amp; a) const声明中,三个const分别是什么意思?
  5. iOS网络相关零散知识总结
  6. Servlet概念框架
  7. 纯CSS写三角形-border法
  8. R(八): R分词统计-老九门
  9. vijosP1092 全排列
  10. VS2015试验随手记
  11. jsp 之 解决 Mysql net start mysql启动,提示发生系统错误 5 拒绝访问的问题
  12. 一个高性能、轻量级的分布式内存队列系统--beanstalk
  13. 设计模式的征途—21.迭代器(Iterator)模式
  14. Javascript设计模式(1)
  15. 【深色模式】macOS Mojave+Visual Studio for Mac+FineUICore多图赏析!
  16. Map的深浅拷贝的探究
  17. 老男孩linux实战培训初级班第三次课课前考试题
  18. SpringMVC请求参数注解两个小问题
  19. 容器网络之 veth设备
  20. easyui window refresh 刷新两次解决办法

热门文章

  1. 手把手教你用java实现二分查找树及其相关操作
  2. How to Install KDE on Ubuntu 16.04
  3. 以太坊-Mac环境下remix环境搭建
  4. 【动态规划DP】[USACO16OPEN]248
  5. couchdb(5984)未授权访问
  6. netty系列之:netty中的Channel详解
  7. 洛谷P2504题解
  8. 面试官:你的App卡顿过吗?你是如何监控的?
  9. small-spring 代码贡献者3个月,敢说精通Spring了,分享我的总结!
  10. Windows API 进程相关笔记