mybatis中常用标签
choose
-
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为 true,就会执行 if 标签中的条件。MyBatis 提供了 choose 元素。if标签是与(and)的关系,而 choose 是或(or)的关系。
-
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
-
例如下面例子,同样把所有可以限制的条件都写上,方面使用。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,我们使用where将choose包起来,放置关键字多于错误。
< !-- choose(判断参数) -参数garbageType 不为空并且为3 属性作为:where条件 --> < choose> <when test="garbageType != null and garbageType =='3'.toString"> (select b.garbage_type,a.net_weight,b.car_unit_name disposal_site_name,tare_date_time from xs_dispose_weight_${year} a,xs_car b where a.car_code=b.car_code and b.garbage_type=#{garbageType} ) b </when> <otherwise> xs_dispose_weight_${year} b </otherwise> </choose>
- choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中 的 choose 很类似。
< select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select>
-
when元素表示当 when 中的条件满足的时候就输出其中的内容,跟 JAVA 中的 switch 效果差不多的是按照条件的顺序,当 when 中有条件满足的时候,就会跳出 choose,即所有的 when 和 otherwise 条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出 otherwise 中的内容。所以上述语句的意思非常简单, 当 title!=null 的时候就输出 and titlte = #{title},不再往下判断条件,当title为空且 content!=null 的时候就输出 and content = #{content},当所有条件都不满足的时候就输出 otherwise 中的内容。
-
foreach循环使用方法
< if test="streetName != null and streetName.size > 0"> and d.streetName IN <foreach close=")" collection="streetName" item="streetName" open="(" separator=","> #{streetName} </foreach> </if>
-
一对多的方法
<resultMap id="BaseResultMap" type="com.gisquest.cloud.garbage.model.domain.XsInspectionRecord">
<result column="id" property="id" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="village_name" property="villageName" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="eid" property="eid" javaType="java.lang.Long" jdbcType="BIGINT" />
<result column="aid" property="aid" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="lng" property="lng" javaType="java.lang.Double" jdbcType="DOUBLE" />
<result column="lat" property="lat" javaType="java.lang.Double" jdbcType="DOUBLE" />
<result column="etype" property="etype" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="address" property="address" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="user_id" property="userId" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="user_name" property="userName" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="file_list" property="fileList" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="streetName" property="streetName" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="communityName" property="communityName" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="aa" property="aa" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="bb" property="bb" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="cc" property="cc" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="dd" property="dd" javaType="java.lang.String" jdbcType="VARCHAR" />
<collection property="problemReportList" ofType="com.gisquest.cloud.garbage.model.domain.XsProblemReport"
select="problemReport" column="id">
</collection>
</resultMap>
<resultMap id="BaseResultProblemReport" type="com.gisquest.cloud.garbage.model.domain.XsProblemReport">
<result column="id" property="id" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="eid" property="eid" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="num" property="num" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="type" property="type" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="inspection_time" property="inspectionTime" javaType="java.util.Date" jdbcType="TIMESTAMP" />
<result column="user_id" property="userId" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="file_list" property="fileList" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="aa" property="aa" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="bb" property="bb" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="cc" property="cc" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="dd" property="dd" javaType="java.lang.String" jdbcType="VARCHAR" />
</resultMap>
<select id="problemReport" parameterType="java.util.Map" resultMap="BaseResultProblemReport">
select * from xs_problem_report where left(id,36) = #{id}
</select>
不要忘记在对应的实体中加入属性