mybatis系列(二)--mybatis的动态sql

本文详细解析MyBatis动态SQL的使用,包括parameterType的不同处理方式,以及if、where、choose(when, otherwise)、trim和set等标签的用法。通过示例代码展示如何在查询和更新操作中灵活运用这些动态SQL元素,以实现更智能的条件判断和避免常见错误。" 101744280,6304656,DBMS:优势与劣势解析,"['数据库', '数据库理论']
摘要由CSDN通过智能技术生成

首先看mybatis的查询语句

parameterType是传入的参数类型,当传入是基本数据类型时候:

<select id="findStudentById" parameterType="int" resultMap="studentList">
		select * from t_student where id =#{_parameter}
</select>

当传入是集合时候:

java代码:

public List<Student> findStudentById(int[] i)
对应得xml:

<select id="findStudentById" parameterType="int[]" resultMap="studentList">
	select * from t_student where id in
	<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>

当传入是list的时候:

java代码:

public List<Student> findStudentById(List<Integer> i);
对应得xml:

<select id="findStudentById" parameterType="list" resultMap="studentList">
	select * from t_student where id in
	<span style="white-space:pre">	</span><foreach collection="list" index="index" item="item" open="(" separator="," close=")">
		<span style="white-space:pre">	</span>#{item}
	</foreach>
</select>

当传入是map的时候:

public List<Student> findStudentById(Map<String, List<Integer>> map);</span>
对应得xml: 

<select id="findStudentById" parameterType="map" resultMap="studentList">
	select * from t_student where id in
		<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
</select>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值