1. 程式人生 > >mybatis include choose when if

mybatis include choose when if

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hundsun.cloudtrade.match.dao.IDayHoldDao">

	<sql id="insertIntoHoldSql">
		INSERT INTO tb_day_hold (
			firm_account    ,
			seat_no         ,
			stock_account   ,
			exchange_type   ,
			stock_name      ,
			stock_code      ,
			amount          ,
			market_value    ,
			position_str    
		)VALUES (
			#{firm_account  } ,
			#{seat_no       } ,
			#{stock_account } ,
			#{exchange_type } ,
			#{stock_name    } ,
			#{stock_code    } ,
		<choose>
			<when test="occur_amount != null and occur_amount != '' ">
				#{occur_amount        } ,  
			</when>
			<when test="amount != null and amount != '' ">
				#{amount        } ,  
			</when>
			<otherwise>
				0,
			</otherwise>
		</choose>
			#{market_value  } ,
			#{position_str  } 
		)
	</sql>

	<insert id="addOrUpdateOne_addAmount" parameterType="com.hundsun.cloudtrade.match.domain.DayHoldDomain">
		<include refid="insertIntoHoldSql"></include>
		<![CDATA[
		ON DUPLICATE KEY UPDATE
		amount = amount + VALUES(amount)
		;
		]]>
	</insert>

	<insert id="addOrUpdateOne_amount" parameterType="com.hundsun.cloudtrade.match.domain.DayHoldDomain">
		<include refid="insertIntoHoldSql"></include>
		<![CDATA[
		ON DUPLICATE KEY UPDATE
		amount = VALUES(amount) , seat_no = VALUES(seat_no)
		;
		]]>
	</insert>
	
	<select id="qryMultiRecords"
		parameterType="com.hundsun.cloudtrade.match.dto.req.QryHoldReq"
		resultType="com.hundsun.cloudtrade.match.domain.DayHoldDomain">
		SELECT 
		    		firm_account    ,
					seat_no         ,
					stock_account   ,
					exchange_type   ,
					stock_name      ,
					stock_code      ,
					amount          ,
					market_value    ,
					position_str    
		<choose>
			<!-- 查詢歷史持倉表 -->
		    <when test="hold_date != null and hold_date != '' ">
		    		,hold_date FROM tb_history_hold
		    </when>
		    <!-- 查詢當日持倉 -->
		    <otherwise>
		    		,'' hold_date FROM tb_day_hold
		    </otherwise>
	    </choose>
	    WHERE firm_account = #{firm_account}
		    <if test="seat_no != null and seat_no != '' ">  
		        AND seat_no = #{seat_no}  
		    </if>   
			<if test="exchange_type != null and exchange_type != '' ">  
		        AND exchange_type = #{exchange_type}  
		    </if>   
			<if test="stock_account != null and stock_account != '' ">  
		        AND stock_account = #{stock_account}  
		    </if>   
			<if test="position_str != null and position_str != '' ">  
		        AND position_str = #{position_str}  
		    </if>   
			    ORDER BY stock_code DESC
			<if test="req_number != null and req_number != '' ">  
		        LIMIT 0,#{req_number}
		    </if>
		    ;
	</select>
	
	<delete id="delMultiRecords" parameterType="com.hundsun.cloudtrade.match.dto.req.DelHoldsReq">
		<![CDATA[
			DELETE FROM tb_history_hold WHERE firm_account = #{firm_account};
			DELETE FROM tb_day_hold WHERE firm_account = #{firm_account};
		]]>
	</delete>

</mapper>

資料來源的配置:

jdbc.url=jdbc:mysql://10.20.135.32:8066/TESTDB?autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver

注意 allowMultiQueries=true 說明允許了多個 sql 語句的同時操作

相關推薦

mybatis include choose when if

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/myba

MyBatischoose when正確寫法

<choose> <when test="scoreRange!=null and scoreRange eq 1"> AND sc.score <![CDATA[ < ]]> 60 </when> <when

MyBatis-動態SQL的ifchoosewhen、otherwise、trim、where、set、foreach標籤的使用

動態SQL是MyBatis最強大的特性之一。用於實現動態SQL的主要元素如下: 1、if 2、choose、when、otherwise 3、trim、where、set 4、foreach 程式碼示例: 1、if

MyBatis-動態SQL的ifchoosewhen、otherwise、trim、where、set、foreach使用

動態SQL是MyBatis最強大的特性之一。用於實現動態SQL的主要元素如下: 1、if 2、choose、when、otherwise 3、trim、where、set 4、foreach 程式碼示例: 1、if EmpMapper.xml配置 <select

動態SQL( if, choose, when, otherwise, trim, where, set, foreach標籤講解)

1 動態SQL# 那麼,問題來了: 什麼是動態SQL? 動態SQL有什麼作用? 傳統的使用JDBC的方法,相信大家在組合複雜的的SQL語句的時候,需要去拼接,稍不注意哪怕少了個空格,都會導致錯誤。Mybatis的動態SQL功能正是為了解決這種問題, 其通過 if, choose, when,

Mybatis動態SQL--choose when

參考MyBatis choose(when, otherwise)標籤 前言: 使用mybatis操作資料庫肯定是需要自己書寫SQL語句的,這在帶來方便(進行SQL優化/定製)的同時也要求我們對mybatis的動態SQL有一定了解. 例如在where子句中進行判斷,有時候我們並不

mybatis學習之路----動態sql之choose when otherwise

點滴記載,點滴進步,願自己更上一層樓。 choose節點,用法跟java中的switch 語法相似(官方文件這麼說,事實也是這樣)。 節點用法。 <choose>

mybatis where,foreach,choose when ,set

sql語句where條件中,需要一些安全判斷,例如按性別檢索,如果傳入的引數是空的,此時查詢出的結果很可能是空的,也許我們需要引數為空 時,是查出全部的資訊。這是我們可以使用動態sql,增加一個判斷,當引數不符合要求的時候,我們可以不去判斷此查詢條件。  下文均採用mys

Mybatis學習筆記12 - 動態sql之choose(when otherwise)標簽

oos lose conf 測試 gets lec class 動態 != choose (when, otherwise):分支選擇;帶了break的swtich-case 示例代碼: 接口定義: package com.mybatis.dao; import com

Mybatis的WHERE和IF動態

-- image rod cat return app div 如果 pri mapper.xml: <!--查詢套餐產品 --> <select id="queryComboProducts" resultType="com.runmi

mybatismybatis中的<if test=“”>test中多條件

ID span lun tco sel myba cloud ise uid mybatis中的<if test=“”>test中多條件 代碼展示: 其中 accountCode和apiName都是ApiAllRespBean的屬性 <select

Mybatis中case when的使用

在寫資料庫語句的時候如果需要根據判斷來確定結果的時候可以使用CASE ....when 上例子: 我需要根據查詢的姓名為‘zhangsan’給他一個true狀態,其他的為false SELECT *,CASE tb.username WHEN ('zhangsan') THEN T

case...when...then if 用法 select case when if 的一些用法

select case when if 的一些用法 概述:sql語句中的case語句與高階語言中的switch語句,是標準sql的語法,適用於一個條件判斷有多種值的情況下分別執行不同的操作。 首先,讓我們看一下CASE的語法。在一般的SELECT中,其語法格式如下:  CASE&nb

mybatis 使用xml中if判斷時出現的問題

在用mybatis 時 用if判斷總是遇到各種的坑,時間長了就忘了,在這裡記錄一下 if在判斷 用數值型別和空字串進行比較 如 integer != “”; 這裡就會出現 當integer =0 的時候,這個判斷就不是成立的,導致欄位等於0的時候無法插入 如圖: 這裡的amountPay

mybatis 註解 動態sql<if test="">if>所遇之坑

         相信很多同學很少遇見這種,也很少用到這種,因為這種寫法有點噁心。但是遇到了就要解決啊,網上搜了一堆,都是int,不知道是有意還是無意的,例子如下:  @Select("<script>"             + "select a.* fr

mybitis下choose..when. otherwise條件不起作用

我的程式碼如下:       <select id="findList" resultType="TyArticle"> SELECT <include refid="tyArticleColumns"/> FROM ty_article a <inc

sql控制流程語句case when/if/ifnull/null if

select *,case t.name when 1 then '男' when 2 then '女' end as sexfrom tt t 結果:   一.            控制流程函式 1.CASE value WHEN [compare-value]THEN result [WHEN [c

Mybatis動態語句,if test字串不用進行null判斷

開發十年,就只剩下這套架構體系了! >>>   

MyBatis從入門到精通(六):MyBatis動態Sql之if標籤的用法

最近在讀劉增輝老師所著的《MyBatis從入門到精通》一書,很有收穫,於是將自己學習的過程以部落格形式輸出,如有錯誤,歡迎指正,如幫助到你,不勝榮幸! 本篇部落格主要講解如何使用if標籤生成動態的Sql,主要包含以下3個場景: 根據查詢條件實現動態查詢 根據引數值實現動態更新某些列 根據引數值實現動態插入某

Mybatis】動態SQL之choosewhen、otherwise

Mybatis中沒有if-else的寫法,取而代之的是choose-when-otherwise。choose在最外面,when相當於if,otherwise則相當於else。 <choose> <wh