1.
首先导包
<dependency>
<groupId>com.typesafe.slick</groupId>
<artifactId>slick_2.10</artifactId>
<version>3.1.1</version>
</dependency>
2.配置mysql application.conf
mip_common = {
url = "jdbc:mysql://${DB.MI.HOST}:${DB.MI.PORT}/${DB.MI.DB.COMMON}"
driver = com.mysql.jdbc.Driver
user = "${DB.MI.USER}"
password = "${DB.MI.PSWD}"
connectionPool = disabled
keepAliveConnection = true
}
3.创建class与表绑定
package com.you.model.mysql
import slick.driver.MySQLDriver.api._
object Campaign {
//表字段对应模版
case class CampaignModel(id: Long, version: Long, creation: String, lastModified: String, companyId: Option[Long], brand: Option[String], brandId: Option[Long]
, campaign: Option[String], beginDate: Option[String], endDate: Option[String], url: Option[String], removed: Int,
targetType: Option[String], adslotType: Option[String], deviceType: Option[String])
//表结构: 定义字段类型, * 代表结果集字段
class CampaignTable(tag: Tag) extends Table[CampaignModel](tag, "campaign") {
def id = column[Long]("id")
def version = column[Long]("version")
def creation = column[String]("creation")
def lastModified = column[String]("last_modified")
def companyId = column[Option[Long]]("company_id")
def brand = column[Option[String]]("brand")
def brandId = column[Option[Long]]("brand_id")
def campaign = column[Option[String]]("campaign_name")
def beginDate = column[Option[String]]("begin_date")
def endDate = column[Option[String]]("end_date")
def url = column[Option[String]]("url")
def removed = column[Int]("removed")
def targetType = column[Option[String]]("target_type")
def adslotType = column[Option[String]]("adslot_type")
def deviceType = column[Option[String]]("device_type")
def * = (id, version, creation, lastModified, companyId, brand, brandId,
campaign, beginDate, endDate, url, removed,
targetType, adslotType, deviceType) <> (CampaignModel.tupled, CampaignModel.unapply)
}
//库表实例
val q = TableQuery[CampaignTable]
}
4.编写dao接口(必须使用Await.result否则查不出数据)
object CampaignDao {
def selectCampaignAndTracking(advertiseid: Long): Map[Long, CampaignAndTracking] = {
val db = Database.forConfig("mip_common")
val tuples: Seq[(Campaign.CampaignModel, Tracking.TrackingModel)] = Await.result(db.run(Campaign.q.join(Tracking.q)
.on(_.id === _.campaignId)
.filter { case (campaign, tracking) => campaign.companyId === advertiseid && campaign.removed === 0 && tracking.removed === false }
.result), Duration.Inf)
val value: Seq[(Long, CampaignAndTracking)] = tuples.map { case (campaign, tracking) => campaign.id -> CampaignAndTracking(campaign.id, campaign.brand, campaign.beginDate, campaign.endDate
, campaign.campaign, campaign.url, campaign.adslotType, campaign.targetType, tracking.landingpage, tracking.id, tracking.publisher, tracking.adslot,
tracking.creative, tracking.kpiType, tracking.kpiValue, campaign.deviceType, tracking.channel, tracking.adType, tracking.region, tracking.vertical)
}
value.toMap
}
}
当初看了几篇比较好的博客:
https://blog.csdn.net/u012234115/article/details/78658625
https://www.cnblogs.com/tiger-xc/p/5898585.html
http://wiki.jikexueyuan.com/project/slick-guide/
还可以去官网看