MyBatis学习笔记之参数处理

这个参数处理的参数指的是接口里面方法传的参数

单个简单类型参数

简单类型包括(七种基本数据类型加七种包装类再加String和两种Date)

  • byte、short、int、long、float、double、char
  • Byte、Short、Integer、Long、Float、Double、Character
  • String
  • java.util.Date
  • java.sql.Date

Long类型

<select id="selectById" resultType="Student" parameterType="java.lang.Long">
	select * from t_student where id = #{id}
</select>

parameterType属性的作用:
告诉mybatis框架,我这个方法的参数类型是什么类型
mybatis框架自身带有类型自动判断机制,所有大部分情况下parameterType属性是可以省略不写的


SQL语句最终是这样子的
select * form t_student where id = ?
JDBC代码是一定要给?传值的
怎么传值呢?
ps.seyXxx
ps.setLong(1,1L);
ps.setString(1,“zhangsan”)

mybatis底层到底调用setXxx的哪个方法,取决于parameterType属性的值

注意:mybatis底层实际上内置了很多别名,可以参考开发手册。https://mybatis.net.cn/configuration.html(这里的类型别名那个tag里)

Map参数

<insert id="insertStudentByMap" parameterType="map">
	insert into t_student(id,name,age,sex,birth,height) values (null,#{姓名},#{年龄},#{性别},#{生日},#{身高})
</insert>
@Test
public void testInsertStudentByMap(){
	SqlSession sqlSession = sqlSessionUtil.openSession();
	StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
	Map<String, Object> map = new HashMap<>();
	map.put("年龄","赵六");
	map.put("年龄",20);
	map.put("身高",1.81);
	map.put("性别",'男');
	map.put("生日",new Date());
	
	mapper.insertStudentByMap(map);
	sqlSession.commit();
	sqlSession.close();
}

这种方式是手动封装Map集合,将每个条件以key和value的形式存放到集合中。然后在使用的时候通过#{map集合的key}来取值

这个好像在刚讲CRUD的时候有演示

POJO类

<insert id="insertStudentByPojo" parameterType="studet">
	insert into t_student(id,name,age,sex,birth,height) values (null,#{name},#{age},#{sex},#{birth},#{height})
</insert>
public void testInsertStudentByPojo(){
	SqlSession sqlSession = sqlSessionUtil.openSession();
	StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
	Student student = new Student();
	student.setName("张飞"):
	student.setAge(50):
	student.setSex('女'):
	student.setBirth(new Date()):
	student.setHeight(10.0):
		
	mapper.insertStudentByMap(student);
	sqlSession.commit();
	sqlSession.close();
}

多参数

如果是多个参数,mybatis框架底层是怎么做的呢?
mybatis框架会自动创建一个Map集合,并且Map集合是以这种方式存储参数的:
map.put(“arg0”,name);
map.put(“arg1”,sex);
map.put(“param1”,name);
map.put(“param2”,sex)

List<Student> selectByNameAndSex(String name, Charater sex);
<select id="selectByNameAndSex" resultType="Student">
	select * from t_student where name = #{name} and sex = #{sex}
</select>
@Test
public void testSelectByNameAndSex(){
	SqlSession sqlsession = SqlSessionUtil.openSession();
	StudentMapper mapper = sqlsession.getMapper(StudentMapper.class);
	List<Student> students = mapper.selectByNameAndSex("张三","男");
	students.forEach(student -> System.out.println(student));
	sqlsession.close();
}

代码会报错

Error querying database. Cause:org,apache.ibatis.binding,BindingException: Parameter 'name' not found. Available parameters are [arg1,arg0,param1,param2]

就是说这个name参数找不到,有效的参数是arg1、arg0、param1、param2


所以要改成:

<select id="selectByNameAndSex" resultType="Student">
	select * from t_student where name = #{arg0} and sex = #{arg1}
</select>

或者

<select id="selectByNameAndSex" resultType="Student">
	select * from t_student where name = #{param1} and sex = #{param2}
</select>

Param注解

接上回的arg几和param几的,可读性太差,就可以使用@Param注解来指定名字

List<Student> selectByNameAndSex2(@Param("name") String name, @Param("sex") Charaster sex);
<select id="selectByNameAndSex2" resultType="Student">
	select * from t_student where name = #{name} and sex = #{sex}
</select>

但是还是可以用param几的,只是将arg的替换掉了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优降宁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值