mybatis常用语法

namespace

namespace对应的是mapper.xml所属接口类的位置。

<mapper namespace="com.icbc.support.mapper.OperateLogMapper"> </mapper>
crud

crud 对应的是select、insert、update、delete标签。

<select></select>
<insert></insert>
<update></update>
<delete></delete>
sql重用
<sql id="userColumns">
 ${alias}.id,${alias}.username,${alias}.password
<sql>
<select id="selectUsers" resultType="map">
	select
	<include refid="(com.this.mapper xml的位置一般不写默认本xml).userColumns"><property name="alias" value="t1"></include>,
	<include refid="userColumns”><property name="alias" value="t2"></include>
	from some_table t1
	join some_table_t2 
<select>
trim
	<trim prefix="" suffix="" suffixOverrides="" PrefixOverrides="">
	<!--
    prefix:在trim标签内sql语句加上前缀。
    suffix:在trim标签内sql语句加上后缀。
    suffixOverrides:指定去处多余的后缀内容,如suffixOverrides="," 去处多余的,
    prefixOverrides:指定去除多余的前缀内容。
     -->
	<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">  
        insert into cart  
        <trim prefix="(" suffix=")" suffixOverrides=",">  
            <if test="id != null">  
                id,  
            </if>  
            <if test="userId != null">  
                user_id,  
            </if>  
            <if test="dealId != null">  
                deal_id,  
            </if>  
            <if test="dealSkuId != null">  
                deal_sku_id,  
            </if>  
            <if test="count != null">  
                count,  
            </if>  
            <if test="createTime != null">  
                create_time,  
            </if>  
            <if test="updateTime != null">  
                update_time,  
            </if>  
        </trim>  
其他
<update id="methodName" parameterType="String/hashmap/int/Long" resultType="hashmap/com.xxxpackage.common.xxxEntity">
	use ${value}
	<if test="xxxFiled!=null and xxx!=null"> xxxFiled=#{xxxFiled}</if>
    <!-- 转义字符 -->
	<![CDATA[<=]]>	
</update>
<!--批量更新接口执行状态 -->
	<update id="xxx" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" separator=";">
			update xxxTable
			  <set>
			  	 xxx=#{item.xxx}
			  	 <if test="item.executeTime!=null and item.executeTime!=''">
			  	 	,executeTime=#{item.executeTime}
			  	 </if>	
			  </set>
			  where merid=#{item.merid} and interfaceId=#{item.transCode}
		</foreach>
    </update>

    <!--批量对接口配置加锁 -->
    <select id="queryInfoByMeridAndTransCode" parameterType="java.util.List" resultType="hashmap">
    	<foreach collection="list" item="item" index="index" separator=";">
    		select * from gc_transfer_sse_tbl where merid=#{item.merid} and interfaceId=#{item.transCode} FOR UPDATE
    	</foreach>
    </select> 	

    <resultMap type="com.xxxpackage.xxxEntity" id="xxx" autoMapping="true">
    	<id property="id" column="id">
        <result property="field1" column="filed1">		
        <result property="field2" column="filed2">		
        <result property="field3" column="filed3">
        <association property="repos" column="repositoryid" select="com.xxx.pass.ReposMapper.selectById"/>		
        <collection property="belongs" javaType="ArrayList" column="id" ofType="BelongToEntity" select="getUserBelong"/>
        <collection property="functions"  column="id" ofType="com.xxxpackage.entity" javaType="list" >
        	<result column="funcId" property="id"/>
        	<result column="functionCode" property="functionCode"/>
        </collection>	
        <collection property="roles" ofType="RoleEntity" column="{roleId=roleId,appId=selectAppId}" select="com.xxx.union.mapper.RoleMapper.getRoleWithFunctions" />
    </resultMap>


    <select id="getApplicationList" parameterType="hashmap" resultMap="xxx">
    </select>


    <insert id="insertAccount">
    	insert into scm_account_tbl
    	(
    	<foreach collection="namelist" item="item" index="index" separator=",">
           ${item}
    	</foreach>
    	)
    	values(
		<foreach collection="valueList" item="item" index="index" separator=",">
			#{item}
		</foreach>
    	)
    </insert>

    <insert id="updateAccount">
    	update scm_account_tbl set lastUpdateTime=sysdate()
    	<foreach collection="updateList" item="item" index="index" separator="">
    		,${item.filedName}=#{item.fieldValue}
    	</foreach>
    	where customerId=#{customerId}
    </insert>

    
    <select>
    	select 
    	<include refid="Base_Column_List">
    		from uisp_wechat2.scm_event_log
    		where id=#{id,jdbcType=INTEGER}
    </select>

    <insert id="insertSelective" parameterType="xxx.entity">
    	 <trim prefix="(" suffix=")" suffixOverrides=",">
    	 	<if test="content!-null">
    	 		#{content,jdbcType=VARCHAR}
    	 	</if>
    	 </trim>
    </insert> 

    <select id="queryAccount" resultType="com.espresso.commons.AccountEntity">
    	select id ,xxx,
    	<choose>
    		<when test="LongType!=null and LongType=='mobile'" >
    			xxx as xx,
    		</when>	
    		<otherwise>
    			xxx as xxxx,
    		</otherwise>
        </choose>
        aaa from n_user_tbl where 1=1
        <choose>
        	<when test="mobile !=null and mobile !=''">
        		AND mobile=#{mobile}
        	<when>
        	<otherwise>
        		AND userid=#{userid}
        	</otherwise>	
		</choose>        	

    </select>	

    <insert id="insertAccount" parameterType="com.espresso.commons.AccountEntity">
    	<selectKey resultType="long" keyProperty="userId">
    		SELECT LAST_INSERT_ID()
    	</selectKey>
    	...sql	
    </insert>	


  • 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、付费专栏及课程。

余额充值