-
Using ScalikeJDBCscala/playframework 2023. 2. 19. 20:30
http://scalikejdbc.org/documentation/playframework-support.html
https://github.com/scalikejdbc/scalikejdbc-play-support
build.sbt
libraryDependencies ++= Seq( jdbc, guice, "mysql" % "mysql-connector-java" % "8.0.32", "org.scalikejdbc" %% "scalikejdbc" % "3.5.0", "org.scalikejdbc" %% "scalikejdbc-config" % "3.5.0", "org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.8.0-scalikejdbc-3.5", "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test )
dependency 충돌로 scalikejdbc "4.0.0" 대신에 "3.5.0" 사용
conf/application.conf
play.modules { enabled += "scalikejdbc.PlayModule" disabled += "play.api.db.DBModule" } db { # You can turn on SQL logging for any datasource # https://www.playframework.com/documentation/latest/Highlights25#Logging-SQL-statements #default.logSql=true default.driver=com.mysql.jdbc.Driver default.url="jdbc:mysql://localhost:3306" default.username=root default.password=xxxxxx default.poolInitialSize=10 default.poolMaxSize=10 # default.poolValidationQuery= } scalikejdbc { global.loggingSQLAndTime.enabled=true global.loggingSQLAndTime.singleLineMode=false global.loggingSQLAndTime.logLevel=debug global.loggingSQLAndTime.warningEnabled=true global.loggingSQLAndTime.warningThresholdMillis=5 global.loggingSQLAndTime.warningLogLevel=warn }
Play Code
import play.api.Logging import play.api.mvc._ import scalikejdbc.{AutoSession, DB, DBSession, scalikejdbcSQLInterpolationImplicitDef} import javax.inject._ import scala.concurrent.{ExecutionContext, Future} @Singleton class ApiController @Inject() (cc: ControllerComponents)( implicit ec: ExecutionContext ) extends AbstractController(cc) { implicit val session: DBSession = AutoSession def api: Action[AnyContent] = Action { request => val entities: List[String] = DB.readOnly(implicit session => sql"select * from ndiff.evaluation limit 10").map(rs => rs.get[String]("answer")).list().apply() entities.foreach(println) Ok(s"allowed user") } }
'scala > playframework' 카테고리의 다른 글
CustomExecutionContext 설정 (0) 2023.03.01 ScalaTest (0) 2023.02.19 Logging MDC (0) 2023.02.19 Deploy (0) 2023.02.19 Filter (0) 2023.02.19