前端 后端的基本 交互(后端 scala)(二)

时间:2022-03-28 21:08:56

最上层开始,一个 Boot.scala

~pathPrefix (“lttt”/”comm”){
服务的名称 ! _
}

定义服务的接口路径前缀,之后在服务里再定义一些一些接口,只写前缀后面的路径,但是传给前端的url 是 服务前缀的路径+服务里接口的路径

scala 后端返回处理的数据类型

List 和 Map 形式

List 形式 返回相当于数组 []

Map形式 返回相当于列表 {}

例如:前端需要 [
{“info”:[[20,30],[20,30],[20,30] ]},
{“info1”:[[20,30],[20,30],[20,30] ]},
{“info2”:[[20,30],[20,30],[20,30] ]}
]
后端返回处理数据

 val sql = new ProjDataInfoSql(cellKey).generateSql.query.value
if (sql.length != 0) {
val sqlResult = sql(0)
log.info("eResult length==" + sqlResult)
val info= List("age", "name", "address")
val info1= List("age1", "name1", "address1")
val info2= List("age2", "name2", "address2")
val info3= List("age3", "name3", "address3")
val infoend: List[List[String]] = (0 to info.length - 1).map(i => List(info(i), sqlResult(i))).toList
val info1end: List[List[String]] = (0 to info1.length - 1).map(i => List(info2(i), sqlResult(i + 10))).toList
val info2end: List[List[String]] = (0 to info2.length - 1).map(i => List(info2(i), sqlResult(i + 22))).toList
val info3end: List[List[String]] = (0 to info3.length - 1).map(i => List(info3(i), sqlResult(i + 24))).toList

List(Map("info" -> infoend), Map("info1" -> info1end), Map("info2" -> info2end), Map("info3" -> info3end))
} else {
List()
}

后端的基本流程:

* 1.定义接口 *

~
path("commom" / "pinjun") {
//页面进入趋势图-指标平均值,麦芒!
get {
//定义传参
parameters('provinceCode, 'cityCode, 'districtCode, 'timeUnit, 'time, 'algorithmName, 'modelName, 'frequency, 'language, 'inOrOut.?(""), 'qci.?("")) {
(provinceCode: String, cityCode: String, districtCode: String, timeUnit: String, time: String, algorithmName: String, modelName: String, frequency: String, language: String, inOrOut: String, qci: String) => detach() {
complete {
try {
val geo = GeoSegment(provinceCode, cityCode, districtCode)
//新的 服务
new AvgIndexHistoryTrendService().query(geo, timeUnit, time, algorithmName, modelName, frequency, language, inOrOut, qci)
} catch {
case e: Exception =>
val msg = "create avgindexhistory trendchart is not available!" + e.getMessage
log.error(msg, e)
HttpResponse(500, msg)
}
}
}
}
}
}

2.定义 服务

class AvgIndexHistoryTrendService extends XmlConfigUtils {
def query(geo: GeoSegment, timeUnit: String, time: String, algorithmName: String, modelName: String, frequency: String) ={


//服务里面定义调用sql 的方法

//以及查询数据库 对 返回的结果进行处理

//按前端所需要的格式进行返回


}

3.处理数据 查询数据库,编写sql语句

classSql(cellKey: String) extends SqlGenerator {
override def genSql: String = s"""select antenna,round(height,0) as height,round(azimuth,0) as azimuth,powerdivisiontype,powerdivisionid,bsid,
case when boardtype=1 then 'CC16' when boardtype=2 then 'CCE' when boardtype=3 then 'Other' else 'null' end as boardtype from lte_cm_projdata where reportcellkey='${cellKey}'"""


}