还是这玩意,要求最底层service要复合findmany方法使用,不能使用mybatis的动态查询直接写sql语句对数据库进行查询,而是要在第二层process层中将最底层复合(粗略)查询的list拿出来,在对list的字段进行过滤查询!!!
代码:
<select id="findMany" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity" >
select * from StockInformation
where 1=1
<if test="stockcode != null and stockcode !=''" >
StockCode = #{stockcode}
</if>
<if test="shsc != null and shsc !=''">
and SHSC = #{shsc}
</if>
<if test="hsi != null and hsi !=''">
and HSI = #{hsi}
</if>
<if test="hscei != null and hscei !=''">
and HSCEI = #{hscei}
</if>
<if test="hs_tech != null and hs_tech !=''">
and HS_TECH = #{hs_tech}
</if>
order by StockCode asc
<if test="start >= 0 and end > 0">
limit #{start},#{end}
</if>
</select>
<select id="findOne" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity">
select * from StockInformation
where 1=1
<if test="stockcode != null and stockcode != ''" >
and StockCode = #{stockcode}
</if>
</select>
通过传入参数,替代了昨天写的六个查询:(重难点)
<!--查找最高收益率-->
<select id="findMaxEarnRate" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity" >
select * from StockInformation
where 1=1
<if test="indexComponent != null and indexComponent !=''">
and ${indexComponent} = 'Y'
</if>
order by Turnover desc
<if test="start >= 0 and end > 0">
limit #{start},#{end}
</if>
</select>
<!--查找最高市盈率 = 每股价格/每股利润-->
<select id="findMaxPER" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity" >
select * from StockInformation
where 1=1
<if test="indexComponent != null and indexComponent !=''">
and ${indexComponent} = 'Y'
</if>
order by (Open/EPS) desc
<if test="start >= 0 and end > 0">
limit #{start},#{end}
</if>
</select>
<!--查找最大市值-->
<select id="findMaxMarketValue" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity" >
select * from StockInformation
where 1=1
<if test="indexComponent != null and indexComponent !=''">
and ${indexComponent} = 'Y'
</if>
order by Turnover desc
<if test="start >= 0 and end > 0">
limit #{start},#{end}
</if>
</select>
<!--查找最大成交额-->
<select id="findMaxTurnover" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity" >
select * from StockInformation
where 1=1
<if test="indexComponent != null and indexComponent !=''">
and ${indexComponent} = 'Y'
</if>
order by Turnover desc
<if test="start >= 0 and end > 0">
limit #{start},#{end}
</if>
</select>
<!--查找最大%跌幅-->
<select id="findMaxDownChangedPercent" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity" >
select * from StockInformation
where ChangedPercent < 0
<if test="indexComponent != null and indexComponent !=''">
and ${indexComponent} = 'Y'
</if>
order by ChangedPercent desc
<if test="start >= 0 and end > 0">
limit #{start},#{end}
</if>
</select>
<!--查找最大%升幅-->
<select id="findMaxUpChangedPercent" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.StockInformationEntity" >
select * from StockInformation
where 1=1
<if test="indexComponent != null and indexComponent !=''">
and ${indexComponent} = 'Y'
</if>
order by ChangedPercent desc
<if test="start >= 0 and end > 0">
limit #{start},#{end}
</if>
</select>
在处理层:要将需要传的参数全部写到实体类中(难点,想了好久)
package com.simnectzbank.lbs.processlayer.stock.clientmodel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
public class StockModel {
@ApiModelProperty(notes="If you just want to check a specific stock market information with a stock code, you can just call this API dierctly. <br/> maxLength: 10",example="00112.HK")
private String stockcode;
@ApiModelProperty(notes = "沪港通/深港通.<br>maxLength: 18</br>",example = "Y")
private String shsc;
@ApiModelProperty(notes = "恒生指数.<br>maxLength: 18</br>",example = "Y")
private String hsi;
@ApiModelProperty(notes = "国企指数.<br>maxLength: 18</br>",example = "N")
private String hscei;
@ApiModelProperty(notes = "恒生科技指数.<br>maxLength: 18</br>",example = "N")
private String hs_tech;
@NotNull(message = "field.requried.index")
@ApiModelProperty(notes="an indicator to identify where your respone result will start to show.<br/>maxLength: 4",example="0")
@Digits(integer = 4,fraction = 0,message = "index.format" )
@Min(value = 0,message = "index.formate")
private BigDecimal index;
@NotNull(message = "field.requried.items")
@ApiModelProperty(notes="the number of items you'd like to get in the response result.<br/>maxLength: 4",example="3")
@Digits(integer = 4,fraction = 0,message = "items.format" )
@Min(value = 1,message = "items.format")
private BigDecimal items;
public BigDecimal getIndex() {
return index;
}
public void setIndex(BigDecimal index) {
this.index = index;
}
public BigDecimal getItems() {
return items;
}
public void setItems(BigDecimal items) {
this.items = items;
}
public String getShsc() {
return shsc;
}
public void setShsc(String shsc) {
this.shsc = shsc;
}
public String getHsi() {
return hsi;
}
public void setHsi(String hsi) {
this.hsi = hsi;
}
public String getHscei() {
return hscei;
}
public void setHscei(String hscei) {
this.hscei = hscei;
}
public String getHs_tech() {
return hs_tech;
}
public void setHs_tech(String hs_tech) {
this.hs_tech = hs_tech;
}
public String getStockcode() {
return stockcode;
}
public void setStockcode(String stockcode) {
this.stockcode = stockcode;
}
}
然后关键部分:以查询最大市值为例
public ResultUtil execute(BusinessModel model) throws RecordNotFoundException {
String method = "stockMaxMarketValueList-execute";
long threadId = Thread.currentThread().getId();
logUtil.sendDebug(new Date().getTime() + "|" + threadId + "|" + classname + "|" + method + SysConstant.SEND_DEBUG_METHOD_START);
ResultUtil result = null;
StockModel stockModel = (StockModel) model.getTransctionModel();
GetStockModel getStockModel = new GetStockModel();
getStockModel.setStart(stockModel.getIndex().intValue());
getStockModel.setEnd(stockModel.getItems().intValue());
getStockModel.setShsc(stockModel.getShsc());
getStockModel.setHsi(stockModel.getHsi());
getStockModel.setHscei(stockModel.getHscei());
getStockModel.setHs_tech(stockModel.getHs_tech());
ResultUtil stockResult = stockInfoService.findMaxMarketValue(getStockModel);
List<StockInformationModel> list = JSON.parseArray((String) stockResult.getData(), StockInformationModel.class);
if(list == null || list.size() == 0){
throw new RecordNotFoundException(localeMessage.getMessage(ResultConstant.RECORD_NOTFOUND), ExceptionConstant.ERROR_CODE404010);
}else{
SortUtil.sortByMarketCap(list);
result = ResponseUtil.success(SuccessConstant.SUCCESS_CODE200, list , localeMessage.getMessage(ResultConstant.SUCCESS_MSG));
}
logUtil.sendDebug(new Date().getTime() +"|" + threadId + "|" + classname + "|" + method + SysConstant.SEND_ERROR_LOG_METHOD_END );
return result; }
先设置传入参数的值,再将其方法getStockModel中,然后进行排序SortUtil.sortByMarketCap(list);
新建一个排序个工具类,方便对不同字段的查询进行不同的排序。
package com.simnectzbank.lbs.processlayer.stock.util;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.simnectzbank.lbs.processlayer.stock.clientmodel.StockInformationModel;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.commons.collections.comparators.FixedOrderComparator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SortUtil {
private static final Log Logger = LogFactory.getLog(SortUtil.class);
@SuppressWarnings("unchecked")
public static void sortByChangedPercentUp(List<StockInformationModel> list){
try{
Collections.sort(list, new Comparator<StockInformationModel>() {
@Override
public int compare(StockInformationModel o1, StockInformationModel o2) {
double d=Double.parseDouble(o1.getChangedpercent())-Double.parseDouble(o2.getChangedpercent());
return d>0?-1:1;
}
});
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static void sortByChangedPercentDown(List<StockInformationModel> list){
try{
Collections.sort(list, new Comparator<StockInformationModel>() {
@Override
public int compare(StockInformationModel o1, StockInformationModel o2) {
double d=Double.parseDouble(o1.getChangedpercent())-Double.parseDouble(o2.getChangedpercent());
return d>0?1:-1;//升序
}
});
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static void sortByTurnover(List<StockInformationModel> list){
try{
Collections.sort(list, new Comparator<StockInformationModel>() {
@Override
public int compare(StockInformationModel o1, StockInformationModel o2) {
double d=Double.parseDouble(String.valueOf(o1.getTurnover()))-Double.parseDouble(String.valueOf(o2.getTurnover()));
return d>0?-1:1;
}
});
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static void sortByMarketCap(List<StockInformationModel> list){
try{
Collections.sort(list, new Comparator<StockInformationModel>() {
@Override
public int compare(StockInformationModel o1, StockInformationModel o2) {
String m1 = o1.getMarketcap();
String m2 = o2.getMarketcap();
double mm1= Double.parseDouble(m1.substring(0,m1.length()-1));
double mm2= Double.parseDouble(m2.substring(0,m2.length()-1));
//trillion万亿 billion 十亿 1T = 1000B
if(m1.charAt(m1.length()-1)=='T'){
mm1*=1000;
}
if(m2.charAt(m2.length()-1)=='T'){
mm2*=1000;
}
double d = mm1-mm2;
return d>0?-1:1;
}
});
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static void sortByPE(List<StockInformationModel> list){
try{
Collections.sort(list, new Comparator<StockInformationModel>() {
@Override
public int compare(StockInformationModel o1, StockInformationModel o2) {
if(o1.getPe() != null && o2.getPe()!= null){
int d = o1.getPe().compareTo(o2.getPe());
//double d=Double.parseDouble(String.valueOf(o1.getPe()))-Double.parseDouble(String.valueOf(o2.getPe()));
return d > 0 ? -1 : 1;
}else{
return o1.getPe() == null ? 1 : -1;
}
}
});
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static void sortByYield(List<StockInformationModel> list){
try{
Collections.sort(list, new Comparator<StockInformationModel>() {
@Override
public int compare(StockInformationModel o1, StockInformationModel o2) {
if(o1.getYield() != null && o2.getYield()!= null){
double d=Double.parseDouble(o1.getYield().substring(0,4))-Double.parseDouble(o2.getYield().substring(0,4));
return d>0 ? -1 : 1;
}else{
return o1.getYield() == null ? 1 : -1;
}
}
});
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
}
以上是重难点!!!