项目构建工具 用Leiningen
Clojure的数据库接口clojure.java.jdbc
使用Korma来进行数据库操作
首先要在project.clj
文件中添加依赖项:
:dependencies [[org.clojure/clojure "1.7.0"] [org.clojure/java.jdbc "0.3.6"] [c3p0/c3p0 "0.9.1.2"] [mysql/mysql-connector-java "5.1.25"] [korma "0.3.0"] ])
数据库表:
create table blog (
id varchar(32) not null primary key,
title varchar(50) not null,
content varchar(500) not null
);
其次引入 Korma:
(ns test.java.com.core (:use korma.db korma.core))
定义数据库连接的配置信息:
(defdb korma-db (mysql {:db "test", :host "127.0.0.1", :port "3307", :user "root", :characterEncoding "utf8", :zeroDateTimeBehavior "convertToNull", :password "root15"}))
然后定义一下要使用的entity,也就是表名:
(declare blog) (defentity blog)
插入一条数据:
(insert courses
(values { :id "2", :title "test", :connect "hello"})))
使用Clojure内置的map类型。
查询操作:
(println (select blog (where {:id 2}) (order :id :asc)))