0

我想使用 sql 命令从缓存中读取。由于 sql 命令每次都是动态的,我需要将其作为变量输入,但不支持在 sql 命令中使用变量。这是我的代码:

import com.typesafe.config.{Config, ConfigFactory}
import com.zaxxer.hikari.{HikariConfig, HikariDataSource}
import javax.sql.DataSource
import scalikejdbc._

  //load config from application.conf
  lazy val config: Config = ConfigFactory.load("application.conf")
  val md: DataSource = {
val hikariConfig = new HikariConfig()
hikariConfig.setJdbcUrl(config.getString("jdbc.ignite.url"))
hikariConfig.setUsername(config.getString("jdbc.ignite.username"))
hikariConfig.setPassword(config.getString("jdbc.ignite.password"))
hikariConfig.setDriverClassName(config.getString("jdbc.ignite.driver"))

val ds = new HikariDataSource(hikariConfig)
ds
}

ConnectionPool.singleton(new 
DataSourceConnectionPool(inMemoryDataSource))
val inMemoryDbName: String = config.getString("jdbc.ignite.name")
ConnectionPool.add(inMemoryDbName, new 
DataSourceConnectionPool(inMemoryDataSource))

lazy val inMemorySession: DBSession = AutoSession
lazy val inMemoryDatabaseExecutorContext: ExecutionContext =
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(1))

def getAggResult(query: String) = DB readOnly {
  implicit session =>
      sql"$query".map(_.toMap()).list().apply()   
}

问题是我使用的'$',错误说:

  java.sql.SQLException: Failed to parse query. Syntax error in SQL 
  statement "?[*]"; expected "[, ::, ="; SQL statement:

query是一个选择命令,如select field1, field2 from table name where condition1 like value1

如何解决?

4

1 回答 1

1

对不起,那是我的错误,我改变了

 sql"$query".map(_.toMap()).list().apply()  

SQL(s"${query}").map(_.toMap()).list().apply()

它奏效了

于 2018-11-17T10:03:36.800 回答