Mybatis操作mysql数据库(条件查询,表连接)

Where 条件

SELECT id,username,age from tablename
<trim prefix="WHERE" prefixOverrides="AND"> 
	<if test="id != null and id != ''">   
	   AND id = #{id}
	</if>
	<if test="username!= null and username!= ''">   
	   AND username= #{username}
	</if>
	<if test="age != null">   
	   AND age = #{age}
	</if>
</trim>

update 

<update id="updateById">
	UPDATE tablename
	<set>
	  <if test="username != null">
		username = #{username,jdbcType=VARCHAR},
	  </if>
	</set>
	WHERE id = #{id,jdbcType=VARCHAR}
</update>

新增(单条)

<insert id="insertTable">
	INSERT INTO tablename
	<trim prefix="(" suffix=")" suffixOverrides=",">
	  <if test="id != null">
		id,
	  </if>
	  <if test="username != null">
		username,
	  </if>
	  <if test="createTime != null">
		create_time,
	  </if>
	</trim>
	<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
	  <if test="id != null">
		#{id,jdbcType=VARCHAR},
	  </if>
	  <if test="username != null">
		#{username,jdbcType=VARCHAR},
	  </if>
	  <if test="createTime != null">
		now(),
	  </if>
	</trim>
</insert>

新增多条(批量新增)

<insert id="insertBatch" parameterType="java.util.List">
	INSERT INTO tablename (id, username, create_time)
	VALUES
	<foreach collection="list" item="item" index="index" separator=",">
		(
		#{item.id,jdbcType=VARCHAR}, 
		#{item.username,jdbcType=VARCHAR},
		now()
		)
	</foreach>
</insert>

In 条件

SELECT id,username,age from tablename
<trim prefix="WHERE" prefixOverrides="AND"> 
	<if test="idList != null">   
	   AND id IN 
		<foreach item="list" index="index" collection="idList"
			open="(" separator="," close=")"> #{list}
		</foreach>
	</if>
</trim>

sql片段

<sql id="Base_Column_List">
	id, username,age,create_time
</sql>
<!-- 引用 -->
SELECT 
<include refid="Base_Column_List" />
FROM tablename

嵌套查询子查询column传多个参数,举个例子:

<!-- 根据商家查询优惠券总量实例 -->
<resultMap id="entity" type="Entity1" >
	<result property="businessId"      			column="BUSI_ID"/>
	<result property="businessName"      		column="BUSI_NAME"/>
	<association property="couponCount1" 
		column="{BRAND_ID=BUSI_ID,VOUCHER_ID=VOUCHER_ID5,OFF_TIME=createTime}" 
		javaType="int" select="getCountByCondition"/>
</resultMap>
<select id="getList" resultMap="entity">
	SELECT
		ID AS BUSI_ID,
		NAME AS BUSI_NAME,
		4 AS VOUCHER_ID4,
		5 AS VOUCHER_ID5,
		#{lastTime} AS lastTime,
		#{createTime} AS createTime
	FROM
		T_ENTITY
	WHERE
		IS_DELETE = 0
		<if test="businessId != null and businessId != ''">
			AND ID = #{businessId}
		</if>
</select>
<select id="getCountByCondition" resultType="int">
	SELECT
		COUNT(1)
	FROM
		T_COUPON
	<trim prefix="WHERE" prefixOverrides="AND"> 
		<if test="CREATE_TIME != null and CREATE_TIME != ''">   
		   AND TO_DAYS(CREATE_TIME) = TO_DAYS(#{CREATE_TIME})
		</if>
		<if test="VOUCHER_ID != null and VOUCHER_ID != ''">   
		   AND VOUCHER_ID = #{VOUCHER_ID}
		</if>
		<if test="BRAND_ID != null and BRAND_ID != ''">   
		   AND BRAND_ID = #{BRAND_ID}
		</if>
		<if test="OFF_TIME != null">   
		   AND TO_DAYS(CREATE_TIME) <![CDATA[<=]]> TO_DAYS(#{OFF_TIME})
		</if>
		AND IS_DELETE = 0
	</trim>
</select>

一对多查询

<resultMap id="entity2" type="Entity2" >
 <id     property="id"                       column="id" />
 <id     property="username"                 column="username" />
 <collection property="carPhotos" ofType="CarPhoto" column="id"
	select="mapper.standard.CarPhotoMapper.getCarPhotoCollectionByEntityId">
 </collection>
</resultMap>

有待更新哦

转载于:https://my.oschina.net/u/3902753/blog/1929173

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值