MyBitis的xml文件语法

1、# {参数, jdbcType=字段类型}

   指定参数对应数据库字段类型;如#{userName, jdbcType=varchar}

2、foreach元素

<foreach> #{参数} </foreach>

     foreach元素的主要用在构建in条件中。
     foreach元素的属性主要有 item,index,collection,open,separator,close
     item表示集合中每一个元素进行迭代时的别名,
     index表示在迭代过程中,每次迭代到的位置,一般很少使用这个,
     open表示该语句以什么开始,
     separator表示在每次进行迭代之间以什么符号作为分隔 符,
     close表示以什么结束,
     collection:表示如何来得到这个集合, 必须指定

   (1)如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

	<select id="dymamicForeachListGetUser" resultType="com.happyheng.entity.User">
	    select * from users where id in
	      <foreach collection="list" index = "index" item = "item" open="(" separator="," close=")">
	            #{item} 	--需与item属性相同
	      </foreach>
	</select>
	List<Integer> idList = new ArrayList<>();
	idList.add(7);
	idList.add(8);
	idList.add(10);
	idList.add(11);
	List<User> users = session.selectList("dymamicForeachListGetUser",idList); 		//单参数

   (2)如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

	<select id="dymamicForeachArrayGetUser" resultType="com.happyheng.entity.User">
	  select * from users where id in
	      <foreach collection="array" index = "index" item = "idArray" open="(" separator="," close=")">
	       		#{idArray}  --与item属性相同
	      </foreach>
	</select>
	int[] ids = new int[]{7,8,10,11};
	List<User> users = session.selectList("dymamicForeachArrayGetUser", ids); 		//单参数

   (3)如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了

	<select id="dynamicForeach3Test" resultType="Blog">
	    select *
	    from t_blog
	    where title like "%"#{title}"%"
	       <if test="null != ids and ids.size()>0">  --特别注意,此处的判断是map.size()>0
	    	  and id in
	            <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
	            					--collection属性与Map中对应字段的key值相同
	                    #{item}  	--与item属性相同
	            </foreach>
	    	</if>
	</select>
	List idList = new ArrayList();
	idList.add(1);
	idList.add(2);
	idList.add(3);
	idList.add(6);
	idList.add(7);
	idList.add(9);
	Map params = new HashMap();
	params.put("ids", idList);  	// “ids”是foreach元素中collection属性对应字段的key值
	params.put("title", "中国");
	List blogs = blogMapper.dynamicForeach3Test(params);

3、choose元素

   包含两个子元素when与otherwise

​   (1)when元素(相当于java中的if语句)

     最常用的是test属性(相当于java中if语句中的判断条件)

   (2)otherwise元素(相当于java中的else语句)

4、if元素

   相当于java中的if语句

     包含test属性(相当于java中if语句中的判断条件)

5、sql公共片段的使用

   (1)定义sql公共片段:

	<sql id=""></sql>

   (2)片段的调用:

	<include refid="" />  	--refid使用定义时的id

6、resultMap标签的使用:

   如:

	<resultMap id="IncomeMainIndexMap" type="com.ylz.dto.IncomeMainIndexDto" >
	    <result property="wcsrkh" column="WSCLBZ" jdbcType="VARCHAR" />
	    --在Java类中调用wcsrkh,既是调用了数据库查询中的WSCLBZ
	    
	    <association property="segment" resultMap="SegmentMap" columnPrefix="segment_"/>
	    --在association中,property是对应一个实体类,resultMap是下面id为SegmentMap的resultMap
	    --特别注意:columnPrefix是指在对应SementMap中的column属性前加上前缀segment_
	</resultMap>
	
	<resultMap id="SegmentMap" type="com.nykko.hongta.vo.SegmentVo">
	        <id property="id" column="id"/>
	        <result property="warehouseId" column="warehouse_id"/>
	        <result property="name" column="name"/>
	        <result property="number" column="number"/>
	        <result property="length" column="length"/>
	        <result property="width" column="width"/>
	        <result property="height" column="height"/>
	        <result property="status" column="status"/>
	        <result property="distributeInfo" column="distribute_info"/>
	        <result property="userId" column="user_id"/>
	        <result property="condition" column="condition"/>
	        <result property="createBy" column="create_by"/>
	        <result property="updateBy" column="update_by"/>
	        <result property="createTime" column="create_time"/>
	        <result property="updateTime" column="update_time"/>
	    </resultMap>

   (1)两个属性:id与type
     id:在之后的调用中使用;
     type:映射对应的对象类

   (2)两个子标签:id与result
     id:指定数据表中的主键字段;
     result:指定数据表中的非主键字段
     两个子标签的属性:column、property、jdbcType
     1)column:表示sql语句中查询出来的字段名称。
      例:

	select userId   		--userId就是column
		from sysuser
		
		-- 或者
		
	select t.userId as Id   --Id就是column
		from sysuser

     2)property:表示对应对象类中的属性

     3)jdbcType:指定对应数据表字段的数据类型

     4)javaType:指定对象类中属性的类型

7、标签CDATA

   <![CDATA[]]>标签:在CDATA内部的所有内容都会被解析器忽略。

     具体例子如:

	<![CDATA[and orgid<>0]]>  	--原本的<>会被解析成&lt;&gt; 但是放到<![CDATA[]]>后就不会

8、#{}与${}:(使用#{},添加双引号或者单引号。由数据库类型决定)

   MyBatis中使用parameterType向SQL语句传参,parameterType后的类型可以是基本类型int,String,HashMap和java自定义类型。
     在SQL中引用这些参数的时候,可以使用两种方式#{parameterName}或者${parameterName}。

   (1) #{}
     #将传入的数据都当成一个字符串,会对自动传入的数据加一个单引号。
     例如:order by #{parameterName} //或取Map中的value#{Key}也是一样操作。
     假设传入参数是‘Smith’
      会解析成:order by ‘Smith’

   (2)${}
     $将传入的数据直接显示生成在sql中。
     例如:order by ${parameterName} // 与取Map中的value${Key}也是一样操作
     假设传入参数是“Smith”
       会解析成:order by Smith

   (3) 概念
     1)#方式能够很大程度防止sql注入,$方式无法防止Sql注入

     2)$方式一般用于传入数据库对象,例如传入表名。

     3)从安全性上考虑,能使用#尽量使用#来传参,因为这样可以有效防止SQL注入的问题。

  注: # 将值加双引号 / 单引号,$ 直接传入不做处理

9、#{0}或${0}

  注: 在 MyBatis 的 xml 文件中,如果 只传一个 参数,可以使用 #{ 0 } 或者 ${ 0 } 表示该参数
     并 不是 取0值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值