<choose>
<when test="totalFeeDesc == 1">
order by total_fee desc
</when>
<when test="totalFeeDesc == 2">
order by total_fee asc
</when>
<otherwise>
order by create_time desc
</otherwise>
</choose>
配合使用是等价于java中的
[java] view plain copy
- if(...){
- ....
- }else if(...){
- ...
- }else{
- ....
- }
两个when之间的顺序是有关系的。即,如果第一个when满足条件,第二个就不会影响查询结果
---------------------------------------------------------------
<if test="isExport == null">
limit #{page}, #{pageSize}
</if>
---------------------------------------------------------------
<![CDATA[ ]]>
xxxxlike CONCAT('%','${xxx}','%' )
----------------------------------------------------------------
<foreach collection="list" item="activityId" open="("
separator="," close=")">
#{activityId,jdbcType=BIGINT}
</foreach>
<foreach collection="users" item="u" separator=",">
(#{u.userName},#{u.sex})
</foreach>
<update id="updateUserInfoRelationIdByUserId" parameterType="java.util.List">
<foreach collection="list" item="item" open="" close="" separator=";">
update pt_user_info
<set>
relation_base_id = #{item.relationBaseId,jdbcType=VARCHAR}
</set>
where user_id = #{item.userId,jdbcType=BIGINT}
</foreach>
</update>
最终结果是因为 配置的 MySQL jdbc 链接字符串 默认不支持一次性执行多个sql 语句;
但是在我们的 update map中需要执行多个 update语句。
最后加上参数 "allowMultiQueries" 设置为true 如下:
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
<!--批量添加用户关联表-->
<insert id="insertBatchUserBase" parameterType="java.util.List" >
insert into pt_user_info_base ( relation_id)
values
<foreach collection ="list" item="infobase" separator =",">
( #{infobase.relationId,jdbcType=VARCHAR}
</foreach>
</insert>
------------------------------------------------------------------------
update pt_activity_info
<set>
<if test="activityName != null">
activity_name = #{activityName,jdbcType=VARCHAR},
</if>
</set>
=------------------------------------------------------------------------
insert into pt_activity_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="activityId != null">
activity_id,
</if>
<trim prefix="values (" suffix=")" suffixOverrides=",">
------------------------------------------------------------------
<where>
<if test="state != null and state != ''">
state = #{state}
</if>
</where>
--------------------------------------------------------------------------------
<resultMap id="queryForListMap" type="com.sica.domain.User">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<collection property="roles" javaType="java.util.List" ofType="com.sica.domain.Role">
<id column="r_id" property="id" jdbcType="VARCHAR" />
<result column="r_name" property="name" jdbcType="VARCHAR" />
<result column="r_jsms" property="jsms" jdbcType="VARCHAR" />
<result column="r_bz" property="bz" jdbcType="VARCHAR" />
<result column="r_jlzt" property="jlzt" jdbcType="INTEGER" />
<result column="r_glbm" property="glbm" jdbcType="VARCHAR" />
</collection>
</resultMap>
-------------------------------------------------------------------------------------
<resultMap id="OrderDetailResultMap" type="com.sf.shiva.oms.query.dto.OrderDetail">
<result property="totalGoodsCount" column="totalGoodsCount" jdbcType="INTEGER" />
<association property="order" javaType="com.sf.shiva.oms.query.model.Order" column="order_no" select="selectOrder">
<id property="orderNo" column="order_no" jdbcType="INTEGER"/>
<result property="customerNo" column="customer_no" jdbcType="INTEGER"/>
<result property="amount" column="amount" jdbcType="DOUBLE"/>
</association>
<collection property="goodsList" ofType="com.sf.shiva.oms.query.model.Goods"
select="selectGoods" column="order_no">
</collection>
</resultMap>
获取插入返回主键
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
insert into t_sys_user (su_username, su_password, su_name, su_sex)
values (#{username,jdbcType=NVARCHAR}, #{password,jdbcType=NVARCHAR}, #{name,jdbcType=NVARCHAR}, #{sex,jdbcType=NVARCHAR})
SELECT SCOPE_IDENTITY()
</selectKey>
1、增加了 useGeneratedKeys=”true” ,这一设置指定了 “id” 属性将会由数据库来自动生成,keyProperty ="id" 指定 News 类中的 id 属性,keyColumn="id" 则指定了news 表中的列名 id
2、使用<selectKey> 标签,就会在数据库自动生成 id 之后,将id 的值返回给 Java 程序中的对象,那么News类 实例中的id 值就会被正确设置。
SELECT LAST_INSERT_ID() 这一语法,根据使用数据库类型的不同,有可能不同,本例中的语句仅适用与MySQL
如果还有问题,请与我联系 http://lawrence-zxc.github.io/2014/10/01/aboutMe/
评论(0)
引用此答案
举报
zxc337
2年前
zxc337
0
mybatis 的insert 返回值是影响的行数。想要返回主键,可以通过传入的对象获得。比如主键是id,我先把id放在map里,map.put("id",0); 传入mybatis执行结束后,在通过map.get("id");获得。
<insert id="insertAgentAccount" parameterType="map">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select seq_t_sms_agent_account.nextval from dual
</selectKey>
insert into t_sms_agent_account(agent_acct_id,agent_id)
values(#{id},#{agent_id})
</insert>
in 获取后 并按照list排序
<select id="getUserInfoBaseListByRelationIdsSort" parameterType="cn.parteam.service.domain.po.user.UserInfoBasePo" resultType="list" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List"></include>
from pt_user_info_base
where relation_id in
<foreach collection="idList" item="idLists" open="(" separator="," close=")">
#{idLists,jdbcType=VARCHAR}
</foreach>
order by FIND_IN_SET( relation_id , #{idListStr,jdbcType=VARCHAR} )
</select>