此時java接收的對象便可以是單個對象,也能夠是List對象,具體以下:html
//dao文件 List<Integer> selectNotTest(); Double selMaxRetracement(@Param(value = "account") Integer account); //xml文件 <!--查詢非test帳號集合--> <select id="selectNotTest" resultType="Integer"> select distinct(account) from mt4list_rel </select> <!--查詢帳號得最大回撤值--> <select id="selMaxRetracement" resultType="Double"> select MaxRetracement from retracement_index where Account=#{account} </select>
此時mapper文件中設置的resultType爲hashmap,java中具體接收的是Map,同時返回的數據中,key爲select語句中設置的別名,value爲計算的數值,好比:下面的語句中,map中的一個key-value會是以下:key--allOrderNum,value--count(*)計算出來的數值java
//dao文件 Map<String,Object> selectAccountDeatail(@Param(value = "account")String table); //xml文件 <!--查詢帳戶的總訂單數,總手數,平倉總盈虧,平均手數--> <select id="selectAccountDeatail" resultType="hashmap"> select count(*) as allOrderNum,sum(Volume) as allVolumes,sum(profit) as historyProfit,sum(Volume)/count(*) as avgVolume from ${account} where cmd in ('buy','sell') </select>
resultType與resultMap只能存在一個,用resultMap將數據庫的字段與實體類進行匹配,而後返回的一個List,同事List中的中的元素爲數據庫表對應的實體類,具體以下:git
//dao文件 List<CloseOrder> queryCloseOrderByAccount(@Param(value = "list") String[] list); //xml文件 <resultMap id="CloseOrderResultMap" type="com.kflh.boxApi.apiNoCheckWestffs.entity.CloseOrder"> <id column="id" property="id"/> <result column="closeorder" property="closeOrder"/> <result column="account" property="account"/> <result column="symbol" property="symbol"/> <result column="cmd" property="cmd"/> <result column="Volume" property="volume"/> <result column="OpenTime" property="openTime"/> <result column="OpenPrice" property="openPrice"/> <result column="SL" property="sl"/> <result column="TP" property="tp"/> <result column="Magic" property="magic"/> <result column="Comment" property="comment"/> <result column="timestamp" property="timestamp"/> </resultMap> <select id="queryCloseOrderByAccount" resultMap="CloseOrderResultMap"> select * from closeorder where account in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
具體用法參照以下連接
https://www.cnblogs.com/eternityz/p/12284808.html數據庫
//dao文件 List<CloseOrderList> selectCloseOrderList(); <resultMap id="customResultMap" type="com.kflh.boxApi.chooseSignalSource.entity.CloseOrderList"> <id property="account" column="account"/> <collection property="closeOrderList" ofType="com.kflh.boxApi.chooseSignalSource.entity.CloseOrder"> <result column="id" jdbcType="INTEGER" property="id"/> <result column="closeorder" jdbcType="INTEGER" property="closeOrder"/> <result column="account" jdbcType="INTEGER" property="account"/> <result column="symbol" jdbcType="VARCHAR" property="symbol"/> <result column="cmd" jdbcType="TINYINT" property="cmd"/> <result column="Volume" jdbcType="DOUBLE" property="volume"/> <result column="OpenTime" jdbcType="INTEGER" property="openTime"/> <result column="OpenPrice" jdbcType="DECIMAL" property="openPrice"/> <result column="SL" jdbcType="DECIMAL" property="sl"/> <result column="TP" jdbcType="DECIMAL" property="tp"/> <result column="Magic" jdbcType="INTEGER" property="magic"/> <result column="Comment" jdbcType="VARCHAR" property="comment"/> <result column="timestamp" jdbcType="INTEGER" property="timestamp"/> <result column="Profit" jdbcType="DECIMAL" property="profit"/> <result column="ClosePrice" jdbcType="DECIMAL" property="closePrice"/> <result column="Digits" jdbcType="TINYINT" property="digits"/> <result column="Storage" jdbcType="VARCHAR" property="storage"/> </collection> </resultMap> <!--按照指定字段分組並將分組得數據進行返回--> <select id="selectCloseOrderList" resultMap="customResultMap"> select <include refid="Base_Column_List"/> from closeorder where account in(select account from mt4list_rel) </select>
controllerapi
@Autowired EaQuanXianService eaQuanXianService; @RequestMapping(value = "insertWestfieldAccountIndex",method = RequestMethod.GET) @ResponseBody public JsonResult insertWestfieldAccountIndex(String param) { String[] arr = param.split("@"); //帳號@本地時間@服務器時間@淨值@餘額 if (arr.length != 12) { throw new ServiceException(ResultEnum.LOSTPARAMS); } //初始淨值 String equity=eaQuanXianService.selectEquity(Integer.parseInt(arr[0])); Double cha=DoubleCalendar.subtract(arr[2],equity); Double profitability= DoubleCalendar.divideFloat(cha.toString(),equity); Map<String,Object> map=new HashedMap(); map.put("account",arr[0]); map.put("localtime",DateUtil.strToDate(arr[1])); map.put("equity",arr[2]); map.put("balance",arr[3]); map.put("floating",arr[4]); map.put("grossProfit",arr[5]); map.put("grossLoss",arr[6]); map.put("closeVolume",arr[7]); map.put("closeOrderNum",arr[8]); map.put("tradeVolume",arr[9]); map.put("tradeOrderNum",arr[10]); map.put("nightInterest",arr[11]); map.put("profitability",profitability); eaQuanXianService.insertWestfieldAccountIndex(map); return new JsonResult(); }
serviceImpl中的方法服務器
@Autowired EaQuanXianMapper eaQuanXianMapper; @Override public int insertWestfieldAccountIndex(Map<String, Object> map) { int num = eaQuanXianMapper.insertWestfieldAccountIndex(map); if (num <= 0) { throw new ServiceException(ResultEnum.INSERTWESTFIELDACCOUNTINDEXFAILED); } return num; }
dao文件mybatis
int insertWestfieldAccountIndex(Map<String ,Object> map);
xml文件app
<insert id="insertWestfieldAccountIndex"> insert into westfield_account_index values (null,#{account},#{localtime},#{equity},#{balance},#{floating},#{grossProfit}, #{grossLoss},#{closeVolume},#{closeOrderNum},#{tradeVolume},#{tradeOrderNum}, #{profitability},#{nightInterest}) </insert>
總結:
當從java傳map進入mybatis的時候,mybatis須要將map中的具體值取出來,此時就是把map中的key取出來,放入進去便可.也就是說在將參數放入map時得key與mybatis中的取值名稱一致
map.put("equity",arr[2])-->mybatis取出來得時候#{equity}ide
常規用法ui
//dao文件 void insertAccountFilterResultBatch(@Param(value = "list") List<Map<String,Object>> list); //xml文件 <insert id="insertAccountFilterResultBatch" parameterType="java.util.List"> insert into account_filter_results values <foreach collection="list" index="index" item="item" separator=","> (null,#{item.account},#{item.equity},#{item.inMoney},#{item.outMoney},#{item.createTime}, #{item.totalProfit},#{item.profitRate},#{item.allOrderNum},#{item.allVolumes}, #{item.historyProfit},#{item.avgVolume},#{item.maxRetracement},'1',now()) </foreach> </insert>
總結: 在mybatis的xml文件進行取值得時候,使用foreach標籤,籤內元素不要使用open及close屬性,不然會將全部循環的元素包括進open及close中的大標籤中.其中循環體item爲Map對象,此時取出其中具體的值得時候須要使用#{item.account},其中account爲map中的key