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

Mybatis映射器(一)的更多相关文章

  1. MyBatis映射器(一)--多参数传递方式

    在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个.当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名 ...

  2. MyBatis映射器(转载)

    什么是MyBatis映射器? MyBatis框架包括两种类型的XML文件,一类是配置文件,即mybatis-config.xml,另外一类是映射文件,例如XXXMapper.xml等.在MyBatis ...

  3. MyBatis映射器元素

     映射器是MyBatis最强大的工具,也是我们使用MyBatis时用的最多的工具,映射器中主要有增删改查四大元素,来满足不同场景的需要: 下面是主要元素的介绍:         select:查询语句 ...

  4. mybatis映射器配置细则

    前面三篇博客我们已经多次涉及到映射器的使用了,增删查基本上都用过一遍了,但是之前我们只是介绍了基本用法,实际上mybatis中映射器可以配置的地方还是非常多,今天我们就先来看看映射器还有哪些需要配置的 ...

  5. mybatis 映射器(mappers) 配置说明 加载映射文件方式

    映射器(mappers) 既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了.但是首先我们需要告诉 MyBatis 到哪里去找到这些语句. Java 在自动查找这 ...

  6. mybatis 映射器

    1 映射器 Mapper 是由java接口和 XML 文件共同组成.它的作用如下 1)定义参数类型 2)描述缓存 3)描述 SQL 语句 4)定义查询结果和POJO的映射关系 2 SqlSession ...

  7. Mybatis映射器接口代理对象的方式 运行过程

    查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 1.pom.xml <?xml version="1.0" ...

  8. Mybatis 映射器接口实现类的方式 运行过程debug分析

    查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 <?xml version="1.0" encoding= ...

  9. 【长文】Spring学习笔记(七):Mybatis映射器+动态SQL

    1 概述 本文主要讲述了如何使用MyBatis中的映射器以及动态SQL的配置. 2 MyBatis配置文件概览 MyBatis配置文件主要属性如下: <settings>:相关设置,键值对 ...

随机推荐

  1. [刘阳Java]_SpringMVC文件上传第1季_第10讲

    今天来介绍一个关于SpringMVC框架的文件上传功能.首先我个人感觉SpringMVC框架的文件上传还是要比Struts2框架要好用一些,灵活性更强.因为SpringMVC框架的文件上传有几种不同的 ...

  2. Spring总结之事务

    Spring事务 1)定义 事务是指多个操作单元组成的集合,多个操作单元是整体不可分割的,要么都成功,要么都不成功.必须遵守四个原则(ACID) ●原子性(Atomicity):即事务是不可分割的最小 ...

  3. Requests方法 -- Http协议的短链接与长连接介绍

    转载于简书: 作者:熊师傅链接:https://www.jianshu.com/p/3fc3646fad80 1.以前的误解 很久之前就听说过长连接的说法,而且还知道HTTP1.0协议不支持长连接,从 ...

  4. Kubernetes全栈架构师(基本概念)--学习笔记

    目录 为什么要用Kubernetes? K8s控制节点-Master概念 K8s计算节点-Node概念 什么是Pod? 为什么要引入Pod? 创建一个Pod 零宕机发布应用必备知识:Pod三种探针 零 ...

  5. (JAVA3)MarkDown语法

    (三)MarkDown语法 分级标题(用 #数量 表示 级标题) #+空格+一级标题+回车 = 一级标题 ##+空格+二级标题+回车 = 二级标题 ###+空格+三级标题+回车 = 三级标题 加粗 选 ...

  6. (JAVA1)学习,复习的好帮手

    学习准备:博客 博客,英文名Blog,它的正式名称为网络日记. 为什么要写博客 我们学习任何东西都需要总结和思考.有时候我们一直在赶路,却忘了放慢脚步 提升文笔组织能力 提升学习总结能力 提升逻辑思维 ...

  7. 构建前端第6篇之---内嵌css样式 <el-button style="width:100%"> 登录 </el-button>

    张艳涛写于2021-1-20日 What: 如何让button的长度和input长度一致呢 最先想到的是给这个button加一个class ="buttonclass",然后在vu ...

  8. maven之---资源过滤 在java/main/resourse/*.xml ,*.properties引用maven属性${db.username}

    本文主要来源maven实战14.3 为了应对环境的变化,首先使用Maven属性将这个会发生变化的部分提取出来.在上一节的数据库配置中,连接数据库使用的驱动类,URL,用户名和密码都可能发生变化,因此使 ...

  9. solr(CVE-2019-0193)远程命令执行

    影响版本 Apache Solr < 8.2.0 并且开启了DataImportHandler模块(默认情况下该模块不被启用) 安装 重启daoker  更新配置文件 systemctl dae ...

  10. 数据库技术中的触发器(Trigger)——和ContentObserver功能类似

    刚总结过ContentObserver的作用和特点,顺便总结下数据库技术中的触发器(Trigger),触 发 器 分 为 表 触 发 器 . 行 触 发 器