Mybatis映射器(一)
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映射器(一)的更多相关文章
- MyBatis映射器(一)--多参数传递方式
在mybatis映射器的接口中,一般在查询时需要传递一些参数作为查询条件,有时候是一个,有时候是多个.当只有一个参数时,我们只要在sql中使用接口中的参数名称即可,但是如果是多个呢,就不能直接用参数名 ...
- MyBatis映射器(转载)
什么是MyBatis映射器? MyBatis框架包括两种类型的XML文件,一类是配置文件,即mybatis-config.xml,另外一类是映射文件,例如XXXMapper.xml等.在MyBatis ...
- MyBatis映射器元素
映射器是MyBatis最强大的工具,也是我们使用MyBatis时用的最多的工具,映射器中主要有增删改查四大元素,来满足不同场景的需要: 下面是主要元素的介绍: select:查询语句 ...
- mybatis映射器配置细则
前面三篇博客我们已经多次涉及到映射器的使用了,增删查基本上都用过一遍了,但是之前我们只是介绍了基本用法,实际上mybatis中映射器可以配置的地方还是非常多,今天我们就先来看看映射器还有哪些需要配置的 ...
- mybatis 映射器(mappers) 配置说明 加载映射文件方式
映射器(mappers) 既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了.但是首先我们需要告诉 MyBatis 到哪里去找到这些语句. Java 在自动查找这 ...
- mybatis 映射器
1 映射器 Mapper 是由java接口和 XML 文件共同组成.它的作用如下 1)定义参数类型 2)描述缓存 3)描述 SQL 语句 4)定义查询结果和POJO的映射关系 2 SqlSession ...
- Mybatis映射器接口代理对象的方式 运行过程
查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 1.pom.xml <?xml version="1.0" ...
- Mybatis 映射器接口实现类的方式 运行过程debug分析
查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 <?xml version="1.0" encoding= ...
- 【长文】Spring学习笔记(七):Mybatis映射器+动态SQL
1 概述 本文主要讲述了如何使用MyBatis中的映射器以及动态SQL的配置. 2 MyBatis配置文件概览 MyBatis配置文件主要属性如下: <settings>:相关设置,键值对 ...
随机推荐
- java集合(3)-Java8新增的Predicate操作集合
Java8起为Collection集合新增了一个removeIf(Predicate filter)方法,该方法将批量删除符合filter条件的所有元素.该方法需要一个Predicate(谓词)对象作 ...
- React 模块与组件
React 模块与组件 几个重要概念理解 1). 模块与组件 1. 模块: 理解: 向外提供特定功能的js程序, 一般就是一个js文件 为什么: js代码更多更复杂 作用: 复用js, 简化js的编写 ...
- P7473 [NOI Online 2021 入门组] 重力球
P7473 [NOI Online 2021 入门组] 重力球 题意 给你一个正方形平面,某些位置有障碍,对于平面上两个球,每次你可以改变重力方向使两个球下落到最底端,求使两个球位置重合的最小改变重力 ...
- IDEA如何导出war包
网上有很多关于IDEA导出war包的教程,然而很多照着一步步操作以后,war包并不能在对应目录中找到.参考网上一篇博文,发现其方法描述比较详细且经验证有效. 完整流程如下: 首先点击这里进入项目的配置 ...
- Win10强制程序高DPI缩放设置
起因 工作原因,需要在win10上安装数个古老vc版本(vc6,vc2008,vc2010),但是显示器是2K的,DPI缩放有问题 尝试 VC6比较好解决:右键,属性,兼容性,更改高DPI设置,勾选替 ...
- 关于ClassLoader 和Class的俩个记录
public class ZFBCheckAccountTask extends TaskThread { 断点 @CallerSensitive public ClassLoader getClas ...
- SQL Server常用的几个存储过程
1. sp_helptext 查看一些数据库对象的定义,比如存储过程.函数.试图等. 2. sp_who或者sp_who2 查看SQL Server数据库会话信息.比如是否被阻塞.
- java正则匹配字符串例子
import java.util.regex.Matcher;import java.util.regex.Pattern; public class sss { public static void ...
- time() 在thinkphp 3.2.3 模板格式化输出
{$ltime|date="Y-m-d",###}
- noip模拟测试31
终于有时间写博客了,前面一直咕咕咕都快变成一只公鸡了......这次考试,真的很意外,我在考场上觉得自己打出了T1的正解,样例一拍就过,还跑得嘎嘎快,然后T2,T3码了两个暴力,觉得自己应该能100p ...