anorm中类似的dao方法还有很多不是吗?

发布于 2025-01-07 16:04:36 字数 1507 浏览 5 评论 0原文

我正在使用 Play2 和 anorm。我认为 anorm 的精神是编写简单的 sql,背后没有任何魔法。

但我很快发现我已经写了很多类似的 dao 方法。例如:

case class User(id:Pk[String], username:String, email:String, realname:String, city:String, website:String)

object User {
  val simple = get[Pk[String]]("id") ~ get[String]("username") ~ ... get[String]("website") map {
    case id ~ username ~ ... ~ website = User(id, username, ..., website)
  }
  def findByUsername(username:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where username={username}").on('username->username).as(simple.singleOpt)
  }
  def findByEmail(email:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where email={email}").on('email->email).as(simple.singleOpt)
  }
  def findById(id:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where id={id}").on('id->id).as(simple.singleOpt)
  }
  def findByRealname(keyword:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where realname like {keyword}").on('keyword->"%"+keyword+"%").as(simple *)
  }
  // more similar methods
}

方法几乎相同,除了 where 子句有很小的差异。

所以我创建了一个 findWhere() 方法:

def findWhere(conditon, values:Any*) = ...

我可以在操作中调用它:

User.findWhere("id=?", id)
User.findWhere("username=?", username)

它有效,但我不认为 anorm 推荐它。

解决这个问题的最佳方法是什么?

I'm using Play2 with anorm. I think the spirit of anorm is write plain sqls, no magic behind.

But I quickly found I have write a lot of similar dao methods. For example:

case class User(id:Pk[String], username:String, email:String, realname:String, city:String, website:String)

object User {
  val simple = get[Pk[String]]("id") ~ get[String]("username") ~ ... get[String]("website") map {
    case id ~ username ~ ... ~ website = User(id, username, ..., website)
  }
  def findByUsername(username:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where username={username}").on('username->username).as(simple.singleOpt)
  }
  def findByEmail(email:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where email={email}").on('email->email).as(simple.singleOpt)
  }
  def findById(id:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where id={id}").on('id->id).as(simple.singleOpt)
  }
  def findByRealname(keyword:String) = DB.withConnection { implicit connection =>
     SQL("select * from users where realname like {keyword}").on('keyword->"%"+keyword+"%").as(simple *)
  }
  // more similar methods
}

There methods are almost the same, exception the where clause has small difference.

So I created a findWhere() method as:

def findWhere(conditon, values:Any*) = ...

That I can call it in actions:

User.findWhere("id=?", id)
User.findWhere("username=?", username)

It works, but I don't think it's recommended by anorm.

What's the best way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2025-01-14 16:04:36

为什么您认为不推荐或可以?

Anorm 只关心接收 SQL 查询并将结果解析为案例类。如果由于您的约束/设计,您动态地生成该 SQL 请求,那没有什么区别。

我看到的唯一问题是他“?” char,这不符合 Anorm 的工作方式。我相信我会更喜欢:

User.findWhere("username", username)

def findWhere(field: String, value: String) = {
  SQL("select * from users where "+ field +"={"+ field +"}").on(Symbol(field)->value).as(simple.singleOpt)
}

这是一个简单的例子,根据需要扩展。

Why do you believe it is not recommended or ok?

Anorm only cares of receiving a SQL query and parsing the result into a case class. If due to your constraints/design you generate that SQL request dinamically, that makes no difference.

The only issue I see is witht he '?' char, which is nto the way Anorm works. I believe it would me more like:

User.findWhere("username", username)

def findWhere(field: String, value: String) = {
  SQL("select * from users where "+ field +"={"+ field +"}").on(Symbol(field)->value).as(simple.singleOpt)
}

This is a simple example, extend as required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文