Mybatis的sql语法

1.if和where的使用。

<select id="findUserList" parameterType="user" resultType="user">
		select * from user 
		<where>
		<if test="id!=null and id!=''">
		and id=#{id}
		</if>
		<if test="username!=null and username!=''">
		and username like '%${username}%'
		</if>
		</where>
</select>

2.foreach的使用

<if test="ids!=null and ids.size>0">
	 <foreach collection="ids" open=" and id in(" close=")" item="id" separator="," >
	    #{id}
	 </foreach>
</if>

3.sql片段

<sql id="query_user_where">
	<if test="id!=null and id!=''">
		and id=#{id}
	</if>
	<if test="username!=null and username!=''">
		and username like '%${username}%'
	</if>
</sql>
<select id="findUserList" parameterType="user" resultType="user">
	 select * from user 
	 <where>
	 <include refid="query_user_where"/>
	 </where>
</select>

如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:

<include refid="namespace.sql片段”/>

4.resultMap做关联查询(一对一,用户订单表)

<resultMap type="Orders" id="orderUserResultMap">
		<id column="id" property="id"/>
		<result column="user_id" property="userId"/>
		<result column="number" property="number"/>
		<result column="createtime" property="createtime"/>
		<result column="note" property="note"/>
		<!-- 一对一关联映射 -->
		<!-- property:Orders对象的user属性,javaType:user属性对应 的类型 -->
		<association property="user" javaType="cn.itcast.po.User">
			<!-- column:user表的主键对应的列  property:user对象中id属性-->
			<id column="user_id" property="id"/>
			<result column="username" property="username"/>
			<result column="address" property="address"/>
		</association>
</resultMap>

5,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis SQL语法包括动态SQL技术和XML解析。动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它解决了拼接SQL语句字符串时的痛点问题。在Mybatis中,SQL语句通常写在mapper.xml文件中,但是XML解析时会遇到特殊字符需要进行转义处理,例如使用<代替<,>代替>,&代替&,&apos;代替',"代替"等。另外,#{}在Mybatis中用于向prepareStatement中的预处理语句中设计参数值,可以理解为一个占位符即?。所以,Mybatis SQL语法是通过动态SQL技术拼装SQL语句,并在mapper.xml中进行XML解析,并且使用#{}作为参数的占位符。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [MyBatis基础语法详解,真的全面](https://blog.csdn.net/qq_42176665/article/details/127873388)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Mybatis常用语法汇总](https://blog.csdn.net/qw463800202/article/details/103221651)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值