首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用clojure中的korma在mysql中插入多行

使用clojure中的korma在mysql中插入多行
EN

Stack Overflow用户
提问于 2020-11-16 23:15:40
回答 1查看 99关注 0票数 0

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

代码语言:javascript
复制
(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))

以下是我的查询,以选择使用现金支付的客户及其订单和订单项目:

代码语言:javascript
复制
(defn get-customersbypmethod [PMethod]
  (select customerorder (with customer (with orderitems)) (where {:PMethod PMethod})))

我的问题是如何将订单与订单项一起插入?

EN

回答 1

Stack Overflow用户

发布于 2020-11-22 23:15:03

Korma文档中这样写道:

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

代码语言:javascript
复制
;; 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

您只需使用( transaction )宏就可以在Korma中进行事务处理,这可以确保在其中执行的所有查询都是单个事务的一部分。然后,如果需要,可以使用( rollback )函数强制事务回滚。

代码语言:javascript
复制
(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是一个稳定且文档齐全的库。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64860593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档