MyBatis 常用操作

1.where  or | and

  <sql id="BaseColumn">
    ID, NodeName, PID, Level, Creator, CreateTime, LastAccessTime, FirstParentId, nodeUrl
  </sql>

<select id="queryTreeListById" resultMap="BaseResultMap">
  	SELECT <include refid="BaseColumn" />
	FROM BC_DirTree
	<trim prefix="WHERE" prefixOverrides="OR">
		<if test="id != null and id != ''">
		   OR  id = #{id}
		</if>
		<if test="firstParentId != null and firstParentId != ''">
		   OR  firstParentId = #{firstParentId}
		</if>
	</trim>
  </select>

2.批量插入:
 

<!-- 批量插入 -->
<insert id = "insertCategoryAttr" parameterType = "java.util.List" >
	INSERT INTO 
		BC_SampleCategoryAttribute(ID,CategoryID,AttrName,Unit,AttrType,DispStyle,DispDetail,Sort,AttrValue,Creator,CreateTime,LastAccessTime)
	VALUES
	 <foreach collection="list" item="item" separator = ",">
	  	(
	  	  #{item.id}, 
	  	  #{item.categoryID},
	  	  #{item.attrName},
	  	  #{item.unit},
	  	  #{item.attrType},
	  	  #{item.dispStyle},
	  	  #{item.dispDetail},
	  	  #{item.sort},
	  	  #{item.attrValue},
	  	  #{item.creator},
	  	  #{item.createTime},
	  	  #{item.lastAccessTime}
	  	)
	  </foreach>
	
</insert>

3.IN操作:

 <!--删除样本分类属性的id -->
<delete id="deleteAttributesByAttributeId" parameterType ="java.util.List">
     DELETE FROM BC_SampleCategoryAttribute WHERE ID IN
      <foreach collection="list" item="scaid" open="(" separator="," close=")">
          #{scaid}
     </foreach>
</delete> 

4.带条件的批量更改

  <!-- 批量启用或者停用 -->
  <update id="batchStartAndStop" parameterType="java.util.Map">
  	 update channel_sync set status = #{status}  where id in
  	 <foreach collection="ids" index="index" item="id" open="(" separator="," close=")">
          #{id}
     </foreach>
  </update>


dao接口
public Integer batchStartAndStop(Map<String, Object> params) throws SQLException;

service调用方式

 Map<String, Object> params = new HashMap<String, Object>();
 params.put("status", channelSync.getStatus());
 params.put("ids", channelSync.getIds());
 channelSyncDAO.batchStartAndStop(params);

5.返回主键,主键在对象中

 <!-- 新增同步渠道信息 -->
  <insert id="createChannelSync" parameterType="com.mysteel.cms.article.dao.entity.ChannelSync" 
  useGeneratedKeys="true" keyProperty="id">
    insert into channel_sync
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null"> id, </if>
      <if test="channelId != null"> channel_id, </if>
      <if test="websiteId != null">  website_id, </if>
      <if test="masterChannelId != null"> master_channel_id, </if>
      <if test="masterChannelName != null"> master_channel_name,  </if>
      <if test="masterBreeds != null"> master_breeds,</if>
      <if test="masterCitys != null"> master_citys,</if>
      <if test="masterFactorys != null"> master_factorys, </if>
      <if test="masterPorts != null"> master_ports, </if>
      <if test="creatorId != null"> creator_id, </if>
      <if test="createTime != null"> create_time, </if>
      <if test="lastEditorId != null"> last_editor_id, </if>
      <if test="lastEditTime != null"> last_edit_time, </if>
      <if test="status != null"> status, </if>
      <if test="masterBreedsIds != null"> master_breeds_ids , </if>
      <if test="masterCitysIds != null"> master_citys_ids, </if>
      <if test="masterFactorysIds != null"> master_factorys_ids, </if>
      <if test="masterPortsIds != null"> master_ports_ids, </if>
      version,
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null"> #{id,jdbcType=BIGINT}, </if>
      <if test="channelId != null">  #{channelId,jdbcType=BIGINT}, </if>
      <if test="websiteId != null">  #{websiteId,jdbcType=BIGINT}, </if>
      <if test="masterChannelId != null"> #{masterChannelId,jdbcType=BIGINT}, </if>
      <if test="masterChannelName != null"> #{masterChannelName,jdbcType=VARCHAR}, </if>
      <if test="masterBreeds != null"> #{masterBreeds,jdbcType=VARCHAR}, </if>
      <if test="masterCitys != null"> #{masterCitys,jdbcType=VARCHAR}, </if>
      <if test="masterFactorys != null"> #{masterFactorys,jdbcType=VARCHAR}, </if>
      <if test="masterPorts != null"> #{masterPorts,jdbcType=VARCHAR}, </if>
      <if test="creatorId != null"> #{creatorId,jdbcType=BIGINT}, </if>
      <if test="createTime != null"> #{createTime,jdbcType=BIGINT}, </if>
      <if test="lastEditorId != null"> #{lastEditorId,jdbcType=BIGINT}, </if>
      <if test="lastEditTime != null">  #{lastEditTime,jdbcType=BIGINT}, </if>
      <if test="status != null"> #{status,jdbcType=BIT}, </if>
      <if test="masterBreedsIds != null"> #{masterBreedsIds,jdbcType=VARCHAR} , </if>
      <if test="masterCitysIds != null"> #{masterCitysIds,jdbcType=VARCHAR}, </if>
      <if test="masterFactorysIds != null"> #{masterFactorysIds,jdbcType=VARCHAR}, </if>
      <if test="masterPortsIds != null"> #{masterPortsIds,jdbcType=VARCHAR}, </if>
      0,
    </trim>
  </insert>
扫描二维码关注公众号,回复: 1467052 查看本文章

猜你喜欢

转载自my.oschina.net/u/2253438/blog/817700