MyBatis(四)多参数处理问题
这里总结了关于多参数传递时,MyBatis接收的三种方式。
(1)接口中编写方法
public Emp getEmpByParams(Integer id,String lastNmae);
public Emp getEmpByParam(@Param("id")Integer id,@Param("lastName")String lastNmae);
public Emp getEmpByParam(Map<String, Object> map);
(2)编写Mapper文件
<select id="getEmpByParams" resultType="com.eu.bean.Emp">
select id,last_name lastName,gender geder,email from Emp where id = #{param1} and last_name= #{param2}
</select>
<select id="getEmpByParam" resultType="com.eu.bean.Emp">
select id,last_name lastName,gender geder,email from Emp where id = #{id} and last_name= #{lastName}
</select>
(3)编写测试
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "conf/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testMapperPar() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1.获取到sqlsession 不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
EmpDao mapper = openSession.getMapper(EmpDao.class);
Emp emp = mapper.getEmpByParam(1, "张三");
System.out.println(emp);
//手动提交数据
openSession.commit();
openSession.close();
}
@Test
public void testMapperMap() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1.获取到sqlsession 不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
EmpDao mapper = openSession.getMapper(EmpDao.class);
Map<String, Object> map=new HashMap<String, Object>();
map.put("id", 1);
map.put("lastName", "张三");
Emp emp = mapper.getEmpByParam(map);
System.out.println(emp);
//手动提交数据
openSession.commit();
openSession.close();
}
MyBatis(四)多参数处理问题的更多相关文章
- 源码解析之 Mybatis 对 Integer 参数做了什么手脚?
title: 源码解析之 Mybatis 对 Integer 参数做了什么手脚? date: 2021-03-11 updated: 2021-03-11 categories: Mybatis 源码 ...
- canvas绘制二次贝塞尔曲线----演示二次贝塞尔四个参数的作用
canvas中绘制二次贝塞尔曲线的方法为ctx.quadraticCurveTo(x1,y1,x2,y2); 四个参数分别为两个控制点的坐标.开始点即当前canvas中目前的点,如果想从指定的点开始, ...
- margin标记可以带一个、二个、三个、四个参数,各有不同的含义。
margin标记可以带一个.二个.三个.四个参数,各有不同的含义. margin: 20px;(上.下.左.右各20px.) margin: 20px 40px;(上.下20px:左.右40px.) ...
- MyBatis的返回参数类型和查询结果遍历
MyBatis的返回参数类型分两种 1. 对应的分类为: 1.1.resultMap: 1.2.resultType: 2 .对应返回值类型: 2.1.resultMap:结果集 2.2.result ...
- Subscribe的第四个参数用法
看别人的代码真的是很好的学习过程啊 之前用Subscribe订阅的时候都是简单的用法形如: ros::Subscriber sub = node.subscribe<uhf_rfid_api:: ...
- hibernate篇章四-- Hibernate配置文件中hiberante.hbm2ddl.auto四个参数的配置
我们在搭建环境的时候,在配置文件中有一个属性标签为: <property name="hibernate.hbm2ddl.auto"> </propert ...
- Hibernate配置文件中hiberante.hbm2ddl.auto四个参数的配置
我们在搭建环境的时候,在配置文件中有一个属性标签为: <property name="hibernate.hbm2ddl.auto"> </propert ...
- 轨迹系列5——验证轨迹GPS坐标转换为本地坐标的四/七参数是否准确的一种方案
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 目前对多个项目轨迹不准确的情况做了排查,发现导致轨迹偏移百分 ...
- MyBatis 传List参数 nested exception is org.apache.ibatis.binding.BindingException: Parameter 'idList' not found.
在MyBatis传入List参数时,MyBatis报错:nested exception is org.apache.ibatis.binding.BindingException: Paramete ...
- mybatis中String参数的传递
mybatis中String参数的传递 Keywords selectKeywords(@Param("key") String key); 可以在mapper方法的参数钱添加 @ ...
随机推荐
- postgresql设置主键
replace(((uuid_generate_v4())::character varying)::text, '-'::text, ''::text)
- Pytorch划分数据集的方法
之前用过sklearn提供的划分数据集的函数,觉得超级方便.但是在使用TensorFlow和Pytorch的时候一直找不到类似的功能,之前搜索的关键字都是"pytorch split dat ...
- 【Android手机测试】OOM
当大多数应用程序都消耗完自己的内存的时候,因为这些应用程序的内存需求加起来超出了物理内存(包括swap)的容量,内核(OOM killer)必须杀掉一些进程才能腾出空间保障系统正常运行 Linux内核 ...
- SpringSecurity在Springboot下使用的初步体验
SpringSecurity曾经在十年前非常火热,只要是做权限系统,当时几乎非用它不可,记得是在XML文件里一堆的配置.曾几何时,Shiro冒了出来,以其简洁和轻量的风格慢慢地捕获了众多码农的心,从此 ...
- Spring框架中的org.springframework.context.annotation.Import注解类
@Import注解的作用和在使用spring的xml配置时用到的<import/>类似.但应注意是@Import在使用时必须要保证能被IOC容器扫描到,所以通常它会和@Configurat ...
- 题解-GXOI/GZOI2019 特技飞行
Problem loj3085 bzoj不放题面差评 题意概要:给出两条竖直直线,再给出 \(n\) 架飞机的初始航线:一条接通这两条直线的线段,保证航线交点不在两条直线上.现要求安排所有飞机在航线相 ...
- mybatis调用oracle存储过程的几个参考例子
首先写一个存储过程: create or replace procedure p_syn_equipment_20161205 is sqlstr ); begin --清空表 sqlstr := ' ...
- Haproxy_haproxy.cfg
global # 全局参数的设置 log 127.0.0.1 local2 # log语法:log <address_1>[max_level_1] # 全局的日志配置,使用log关键字, ...
- SQL解析
private static String getCountSql(String sql) { return "select count(*) from "+cutOrderByO ...
- idea中classpath的问题
新创建的项目,在web.xml中配置 Spring MVC 的 DispatcherServlet,,指定springmvc.xml,结果这部分出错,请求的时候报错 结果查找,发现在Idea中,cla ...