mybatis基础 sql写法

1.以List<Map<String, Object>> list 查询结果集的写法

<select id="select" resultType="java.util.HashMap">
		SELECT * FROM 表名
</select>

<select id="select" parameterType="java.util.HashMap" resultType="Map">
	SELECT * FROM 表名
</select>

<select id="select" parameterType="long" resultType="Map">
    SELECT * FROM 表名
<select>

<select id="select" resultType="java.util.HashMap">
    SELECT * FROM 表名
<select>

2.以返回Double为查询结果

<select id="select" resultType="java.lang.Double">
		SELECT
		TRUNCATE(sum(amount)/100,2) as AMOUNT FROM 表名 WHERE `PROJECT_ID` = #{proId} 
</select>

3.以返回Long为查询结果

<select id="select" resultType="long">
		SELECT
		count(*)
		FROM  表名
</select>

4.以实体返回为查询结果

	<select id="select" resultMap="ResultMap">
		SELECT * from 表名
	</select>

5.新增写法

<insert id="insert" insert="com.实体"
		useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
		INSERT INTO
		<include refid="table"></include>
		(
		`USER_ID`,`DISPLAY_NAME`,`USER_IMAGE`,`ORG_ID`,`PROJECT_ID`,`MODE`,`INVOICE_ID`,`AMOUNT`,`STATUS`,
		`GOODS_NAME`,`ORG_NAME`,
		`ALI_TRADE_NO`,`ALI_USER_ID`,`ALI_USER_NAME`,`ALI_CUSTOM_ID`,`BFB_CUSTOM_ID`,`BFB_TRADE_NO`,`ANONYMOUS`,`PAY_DATE`,`WEIXIN_TRADE_NO`,
		`CREATE_DATE`,`UPDATE_DATE`,`IS_RANDOM`,`PROJECT_MAIN_IMG`,`PROJECT_TARGET`,`SHARE_ORDER_ID`,`ORDER_TYPE`,`CLIENT_IP`,`DON_TOGETHER_ID`,`MONTH_DONATE_ID`
		)
		VALUES
		(
		#{userId},#{displayName},#{userImage},#{orgId},#{projectId},#{mode},#{invoiceId},#{amount},#{status},
		#{goodsName},#{orgName},
		#{aliTradeNo},#{aliUserId},#{aliUserName},#{aliCustomId},#{bfbCustomId},#{bfbTradeNo},#{anonymous},#{weixinTradeNo},#{payDate},
		#{createDate},
		#{updateDate},
		#{isRandom},
		#{projectMainImg},
		#{projectTarget},
		#{shareOrderId},
		#{orderType},
		#{clientIp},
		#{donateTogetherId},
		#{monthDonateId}
		)
	</insert>

6.修改写法

<update id="update" parameterType="com.实体">
		UPDATE
		<include refid="table"></include>
		SET
		`USER_ID` = #{userId},
		`DISPLAY_NAME` = #{displayName},
		`USER_IMAGE`
		= #{userImage},
		`ORG_ID` =#{orgId},
		`PROJECT_ID`
		=#{projectId},
		`MODE`
		=#{mode},
		`INVOICE_ID`=#{invoiceId},
		`AMOUNT`=#{amount},
		`STATUS`=#{status},
		`GOODS_NAME`=#{goodsName},
		`ORG_NAME`=#{orgName},
		`ALI_TRADE_NO`=#{aliTradeNo},
		`ALI_USER_ID`=#{aliUserId},
		`ALI_USER_NAME`=#{aliUserName},
		`ALI_CUSTOM_ID`=#{aliCustomId},
		`BFB_CUSTOM_ID`=#{bfbCustomId},
		`BFB_TRADE_NO` =#{bfbTradeNo},
		`ANONYMOUS`=#{anonymous},
		`PAY_DATE` =#{payDate},
		`WEIXIN_TRADE_NO`=#{weixinTradeNo},
		`CREATE_DATE`=#{createDate},
		`UPDATE_DATE`=#{updateDate},
		`IS_RANDOM`=#{isRandom},
		`PROJECT_MAIN_IMG`=#{projectMainImg},
		`PROJECT_TARGET`=#{projectTarget},
		`SHARE_ORDER_ID`=#{shareOrderId},
		`ORDER_TYPE`=#{orderType},
		`CLIENT_IP`=#{clientIp}
		WHERE
		`ID` = #{id};
	</update>

7.删除写法

	<delete id="delect" parameterType="java.lang.Long">
		delete from
		表名
		where id = #{id,jdbcType=BIGINT}
	</delete>

8.多条件排序

<select id="select" resultType="java.util.HashMap">
		SELECT
		d.id,
		d.company_name AS companyName,
		d.company_idea AS
		companyIdea,
		DATE_FORMAT(d.create_date, '%Y-%m-%d') AS entryDate,
		COUNT(c.pro_id) AS proNumber,
		IFNULL(c.donate_amount, 0) AS
		donateAmount,
		IFNULL(c.donate_material_amount, 0) AS
		donateMaterialAmount,
		d.update_date AS updateDate
		FROM
		gongyi_donation_company d
		LEFT JOIN gongyi_company_donate c ON d.id =
		c.donate_company_id
		WHERE
		1 = 1
		<if test="companyName != null and companyName != ''">
			AND
			d.company_name like CONCAT('%',#{companyName},'%')
		</if>
		AND (d.is_del = 0 OR c.is_del = 0)
		GROUP BY
		d.company_name
		ORDER BY
		<if test="sort == 1">
			entryDate ASC,
		</if>
		<if test="sort == 2">
			entryDate DESC,
		</if>
		<if test="sort == 3">
			proNumber ASC,
		</if>
		<if test="sort == 4">
			proNumber DESC,
		</if>
		<if test="sort == 5">
			c.donate_amount ASC,
		</if>
		<if test="sort == 6">
			c.donate_amount DESC,
		</if>
		<if test="sort == 7">
			c.donate_material_amount ASC,
		</if>
		<if test="sort == 8">
			c.donate_material_amount DESC,
		</if>
		<if test="sort == 7">
			d.update_date ASC,
		</if>
		<if test="sort == 8">
			d.update_date DESC,
		</if>
		d.update_date DESC
	</select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭优秀的笔记

你的支持就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值