初用mybatis,发现很好的一个操作数据库的框架,里面有一些小技巧,挺简单,很实用,记录一下:
mybatis的插入语句:
<insert id="insertAsset" parameterType="Criteria">
<selectKey resultType="java.lang.String" keyProperty="condition.Id" order="BEFORE"> //设置主键 keyproperty名字随便取 select sys_guid() from dual //从数据库中查询uuid作为主键 </selectKey> insert into asset_info <trim prefix="(" suffix=")" suffixOverrides=","> //prefix前缀 suffix后缀 suffixOverrides后缀最后去除多余的 ID,PROCESSINSTANCEID, <if test="condition.ASSET_NAME != null">ASSET_NAME,</if> <if test="condition.ASSET_SORT != null">ASSET_SORT,</if> <if test="condition.ASSET_MODEL != null">ASSET_MODEL,</if> <if test="condition.ADD_TYPE != null">ADD_TYPE,</if> </trim> values <trim prefix="(" suffix=")" suffixOverrides=","> #{condition.Id},#{condition.processinstanceId}, <if test="condition.ASSET_NAME != null">#{condition.assetName},</if> <if test="condition.ASSET_SORT != null">#{condition.assetSort},</if> <if test="condition.ASSET_MODEL != null">#{condition.assetModel},</if> <if test="condition.ADD_TYPE != null">#{condition.addType},</if> </trim> </insert>
如果插入类型是String类型,那么如下:
<select id="querySortId" resultType="ComboReturn" parameterType="String">
select t.SORT_ID as value, t.SORT_NAME as text from SMS_RD_SORT t <if test="_parameter!=null"> //判断是否为空只能用_parameter作为参数 <where> t.type_id =#{typeId,jdbcType=VARCHAR} </where> </if> </select>