【问题标题】:Inserting multiple rows in mysql using korma in clojure在 clojure 中使用 korma 在 mysql 中插入多行
【发布时间】:2021-02-27 19:17:12
【问题描述】:

我正在开发一个安静的应用程序,它在后面使用 Clojure,在前面使用 Angular。我有以下表格:客户、订单、项目、订单项。我正在使用 korma 与 MySql 数据库进行通信。当我对所有实体进行 CRUD 操作时,一切正常。但我不知道如何在数据库中插入多行?我应该在 korma 中使用事务吗?

(declare customer)
(declare customerorder)
(declare item)
(declare orderitems)

(schema/defschema OrderItem
  {
   :OrderItemId schema/Int
   :OrderId schema/Int
   :ItemId schema/Int
   :Quantity schema/Int
   })

(schema/defschema Customer
  {
   :CustomerId schema/Int
   :Name schema/Str
   :Contact schema/Str
   })

(schema/defschema UpdateCustomer
  {
   :Name schema/Str
   :Contact schema/Str
   })

(schema/defschema NewCustomer
  {
   :Name schema/Str
   :Contact schema/Str
   })

(schema/defschema Order
  {
   :OrderId schema/Int
   :CustomerId schema/Int
   :PMethod schema/Str
   :GTotal schema/Num
   })

(schema/defschema Item
  {
   :ItemId schema/Int
   :Name schema/Str
   :Price schema/Num
   })


(defentity customer
           (pk :CustomerId)
           (has-many customerorder {:fk :CustomerId})
           (entity-fields :CustomerId :Name :Contact))

(defentity customerorder
           (pk :OrderId)
           (has-many orderitems {:fk :OrderId})
           (belongs-to customer {:fk :CustomerId})
           (entity-fields :PMethod :GTotal :OrderId)
           )
(defentity item
           (pk :ItemId)
           (entity-fields :ItemId :Name :Price)
           (has-many orderitems {:fk :ItemId}))

(defentity orderitems
           (pk :OrderItemId)
           (belongs-to item {:fk :ItemId})
           (belongs-to customerorder {:fk :OrderId})
           (entity-fields :OrderItemId  :ItemId :Quantity))

这是我的查询,用于选择使用现金支付订单和订单商品的客户:

(defn get-customersbypmethod [PMethod]
  (select customerorder (with customer (with orderitems)) (where {:PMethod PMethod})))

我的问题是如何插入带有订单商品的订单?

【问题讨论】:

  • 我无法回答您的问题,但是,作为 Korma 使用的 org.clojure/java.jdbc 的维护者,我想指出 a) Korma 已经好几年没有更新了( 0.4.3 于四年半前问世)b)Korma 的使用不是很广泛,因此获得对它的支持可能很困难,c)它使用了一个非常旧的 clojure.java.jdbc 版本,它也不再维护(鼓励用户迁移到 next.jdbc)。 ORM 风格的库在 Clojure 中并不常见,但如果你真的想要那种风格,请查看至少仍在维护的 Toucan。

标签: mysql clojure crud compojure korma


【解决方案1】:

Korma 文档说明如下:

插入查询使用函数(值)添加记录。它采用单个映射或映射集合并返回第一个插入记录的 id。

;; You can insert a single value:
(insert users
  (values {:first "john" :last "doe"}))

;; or a collection of several:
(insert users
  (values [{:first "john" :last "doe"}
           {:first "jane" :last "doe"}]))

;; You can also compose inserts:
(def base (-> (insert* users)
            (values {:first "john" :last "doe"})))

(-> base
  (values {:first "jane" :last "doe"})
  (insert))
;; Same thing as the collection insert

您可以在 Korma 中简单地使用 (transaction) 宏来执行事务,这可以确保在其中执行的所有查询都是单个事务的一部分。然后,您可以在必要时使用 (rollback) 函数强制事务回滚。

(transaction
  (insert users
    (values {:first "cool"}))
  (insert address
    (values {:address1 "cool"})))

(transaction
  (if-not (valid?)
    (rollback)
    (do-complicated-query))
  (when-not (is-rollback?)
    (println "success!")))

希望这会有所帮助。 Korma 是一个稳定且文档齐全的库。

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 2023-04-06
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多