Exposed เป็นไลบรารี SQL ที่มีน้ำหนักเบาด้านบนของไดรเวอร์ JDBC สำหรับภาษา Kotlin Exposed มีสองรสชาติของการเข้าถึงฐานข้อมูล: Typesafe SQL Wrapping DSL และวัตถุการเข้าถึงข้อมูลที่มีน้ำหนักเบา (DAO)
ด้วยการเปิดเผยคุณมีสองตัวเลือกสำหรับการเข้าถึงฐานข้อมูล: ห่อ DSL และ DAO ที่มีน้ำหนักเบา มาสคอตอย่างเป็นทางการของเราคือปลาหมึกซึ่งเป็นที่รู้จักกันดีในเรื่องความสามารถในการล้อเลียนที่โดดเด่นซึ่งช่วยให้สามารถผสมผสานเข้ากับสภาพแวดล้อมใด ๆ ได้อย่างราบรื่น เช่นเดียวกับมาสคอตของเราการเปิดเผยสามารถใช้ในการเลียนแบบเอ็นจิ้นฐานข้อมูลที่หลากหลายซึ่งช่วยให้คุณสร้างแอปพลิเคชันโดยไม่ต้องพึ่งพาเครื่องยนต์ฐานข้อมูลเฉพาะและสลับระหว่างพวกเขาด้วยการเปลี่ยนแปลงน้อยมากหรือไม่มีเลย
H2 (เวอร์ชัน 2.x; 1.x เวอร์ชันเลิกใช้แล้วและจะถูกลบออกในรุ่นอนาคต)
(นอกจากนี้ PostgreSQL โดยใช้ไดรเวอร์ PGJDBC-NG JDBC)
การเปิดตัวของการเปิดเผยมีอยู่ในที่เก็บ Maven Central คุณสามารถประกาศพื้นที่เก็บข้อมูลนี้ในสคริปต์บิลด์ของคุณดังนี้:
คำเตือน: คุณอาจต้องตั้งค่าเป้าหมาย Kotlin JVM เป็น 8 และเมื่อใช้ฤดูใบไม้ผลิถึง 17 เพื่อให้สิ่งนี้ทำงานได้อย่างถูกต้อง:
ที่เก็บ {// เวอร์ชันหลังจาก 0.30.1 // เวอร์ชันก่อน 0.30.1 ไม่สามารถใช้งานได้สำหรับ nowmavencentral () -
พื้นที่เก็บข้อมูลกลาง Maven เปิดใช้งานโดยค่าเริ่มต้นสำหรับผู้ใช้ Maven
Exposed
ประกอบด้วยโมดูลต่อไปนี้:
Exposed -Core - โมดูลฐานซึ่งมีทั้ง DSL API พร้อมกับการแมป
Exposed-Crypt-จัดเตรียมประเภทคอลัมน์เพิ่มเติมเพื่อจัดเก็บข้อมูลที่เข้ารหัสใน DB และ ENCODE/DECODE
Dao -dao - dao api
Exposed-Java-Time-ส่วนขยายเวลาวันที่ตาม Java8 Time API
Exposed -JDBC - การใช้งานระดับการขนส่งตาม Java JDBC API
Exposed-Jodatime-ส่วนขยายเวลาวันที่ขึ้นอยู่กับห้องสมุด Jodatime
Exposed -JSON - ส่วนขยายประเภทข้อมูล JSON และ JSONB
Exposed-Kotlin-Datetime-ส่วนขยายเวลาวันที่ขึ้นอยู่กับ Kotlinx-Datetime
การเปิดเผยเงิน-ส่วนขยายเพื่อสนับสนุนเงินจำนวนมากจาก "javax.money:money-api"
Exposed-Spring-Boot-Starter-ผู้เริ่มต้นสำหรับการบูตฤดูใบไม้ผลิเพื่อใช้ประโยชน์จาก ORM แทน Hibernate
<การพึ่งพา> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> Exposed-core </artifactId> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> Exposed-Crypt </artifactId> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> Exposed-DAO </artifactId> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> เปิดเผย-java-time </artifactid> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> Exposed-jdbc </artifactid> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> enposed-jodatime </artifactid> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> enposed-json </artifactid> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> Exposed-Kotlin-Datetime </artifactid> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> การเปิดเผยเงิน </artifactid> <Sersion> 0.56.0 </Sersion> </dermentency> <การพึ่งพา> <RoupID> org.jetbrains.exposed </groupId> <ArtIfactId> เปิดเผย-สปริง-สตาร์เทอร์ </artifactid> <Sersion> 0.56.0 </Sersion> </dermentency> </dependencies>
การพึ่งพา { การใช้งาน 'org.jetbrains.exposed:exposed-core:0.56.0'implementation' org.jetbrains.exposed:exposed-crypt:0.56.0'implementation 'org.jetbrains.exposed:exposed-dao:0.56.0 'org.jetbrains.exposed:exposed-jdbc:0.56.0' การใช้งาน 'org.jetbrains.exposed:exposed-jodatime:0.56.0'/// orimplementation' org.jetbrains.exposed:exposed-java-time:0.56.0'/// orimplementation 'org.jetbrains DateTime: 0.56.0 ' การใช้งาน 'org.jetbrains.exposed:exposed-json:0.56.0'implementation' org.jetbrains.exposed:exposed-money:0.56.0'implementation 'org.jetbrains.exposed:exposed-pring- 0 '}
ใน build.gradle.kts
:
Val Exposedversion: String by Project การพึ่งพา { การใช้งาน ("org.jetbrains.exposed:exposed-core:$exposedversion") การใช้งาน ("org.jetbrains.exposed:exposed-crypt:$exposedversion") การใช้งาน ("org.jetbrains.exposed:exposed-dao:$exposedversion") การใช้งาน ("org.jetbrains.exposed:exposed-jdbc:$exposedversion") การดำเนินการ ("org.jetbrains.exposed:exposed-jodatime:$exposedversion") // orimplementation ("org.jetbrains.exposed:exposed-java-time:$exposedversion") // orimplementation ("org.jetbrains.exposed -kotlin-datetime: $ ensosedversion ") การใช้งาน ("org.jetbrains.exposed:exposed-json:$exposedversion") การดำเนินการ ("org.jetbrains.exposed:exposed-oy:$exposedversion") การใช้งาน ("org.jetbrains.exposed:exposed-pring-boot-starter:$exposedversion") -
และใน gradle.properties
exposedVersion=0.56.0
ตรวจสอบตัวอย่างเพื่อเริ่มต้นอย่างรวดเร็ว
ปัจจุบันมีการเปิดเผยสำหรับ การสร้าง Maven/Gradle ตรวจสอบ Maven Central และอ่านการเริ่มต้นเพื่อรับข้อมูลเชิงลึกเกี่ยวกับการตั้งค่าการเปิดเผย
สำหรับข้อมูลเพิ่มเติมโปรดเยี่ยมชมลิงค์ด้านล่าง:
เอกสารพร้อมตัวอย่างและเอกสาร
มีส่วนร่วมในการเปิดเผย
คู่มือการโยกย้าย
ทำลายการเปลี่ยนแปลงและรายละเอียดการย้ายถิ่นใด ๆ
ช่องหย่อน
ตัวติดตามออก
โปรดทราบว่าเรากำลังย้ายออกจากปัญหา GitHub สำหรับการรายงานข้อบกพร่องและคุณสมบัติ โปรดบันทึกคำขอใหม่ใด ๆ ใน YouTrack คุณต้องลงชื่อเข้าใช้เพื่อดูและบันทึกปัญหามิฉะนั้นคุณจะได้พบกับ 404
คุณมีคำถามหรือไม่? อย่าลังเลที่จะขอคำเชิญสำหรับ Kotlinlang Slack และเข้าร่วมการสนทนาโครงการที่ช่อง #EXPosed ของเรา
เรายินดีต้อนรับคำขอดึงของคุณอย่างแข็งขัน อย่างไรก็ตามการเชื่อมโยงงานของคุณกับปัญหาที่มีอยู่เป็นที่ต้องการ
แยก repo และสร้างสาขาของคุณจากหลัก
ตั้งชื่อสาขาของคุณสิ่งที่อธิบายถึงงานที่คุณกำลังทำอยู่ คือเพิ่มสิ่งใหม่
หากคุณเพิ่มรหัสที่ควรทดสอบให้เพิ่มการทดสอบและตรวจสอบให้แน่ใจว่าผ่านการทดสอบ
ตรวจสอบให้แน่ใจว่าคุณได้ทำการเตือนคำเตือนใด ๆ
หากคุณทำให้รหัสที่มีอยู่ดีขึ้นโปรดแจ้งให้เราทราบในคำอธิบายการประชาสัมพันธ์ของคุณ
ดูแนวทางการบริจาคสำหรับรายละเอียดเพิ่มเติม
นำเข้า org.jetbrains.exposed.sql.*นำเข้า org.jetbrains.exposed.sql.sqlexpressionbuilder.likeimport org.jetbrains.exposed.sql.transactions.transactionObject ผู้ใช้: ตาราง () id ", 10) ชื่อวาล: คอลัมน์ <string> = varchar (" ชื่อ ", length = 50) val CityID: คอลัมน์ <int?> = (จำนวนเต็ม ("city_id") อ้างอิง Cities.id). nullable () แทนที่วัล PrimaryKey = PrimaryKey (id, name = "pk_user_id") // ชื่อเป็นทางเลือกที่นี่} เมืองวัตถุ: ตาราง (ตาราง (ตาราง ) {val id: คอลัมน์ <int> = จำนวนเต็ม ("id"). autoincrement () ชื่อวาล: คอลัมน์ <string> = varchar ("ชื่อ", 50) แทนที่ Val PrimaryKey = PrimaryKey (id, name = "pk_cities_id") } fun main () {database.connect ("jdbc: h2: mem: test", driver = "org.h2.driver", user = "root", รหัสผ่าน = "") ธุรกรรม { addlogger (stdoutsqllogger) schemautils.create (เมืองผู้ใช้) Val Saintpetersburgid = Cities.Insert { มัน [ชื่อ] = "เซนต์ปีเตอร์สเบิร์ก"} รับ Cities.idval Munichid = Cities.Insert { มัน [ชื่อ] = "Munich"} รับ Cities.idval Pragueid = Cities.Insert { it.update (ชื่อ, StringLiteral ("Prague") .Trim (). Substring (1, 2)) } [cities.id] val praguename = cities.selectall (). โดยที่ {cities.id eq pragueid} .single () [cities.name] println ("praguename = $ praguename") ผู้ใช้ มัน [id] = "Andrey" it [name] = "Andrey" มัน [users.cityid] = saintpetersburgid } users.Insert { มัน [id] = "sergey" มัน [name] = "sergey" มัน [users.cityid] = มิวนิชิด } users.Insert { มัน [id] = "Eugene" มัน [name] = "Eugene" มัน [users.cityId] = Munichid } users.Insert { มัน [id] = "Alex" it [name] = "Alex" มัน [users.cityId] = null} users.Insert { มัน [id] = "smth" มัน [ชื่อ] = "บางสิ่ง" มัน [users.cityId] = null} users.update ({users.id eq "Alex"}) { มัน [ชื่อ] = "Alexey"} users.deletewhere {users.name เช่น "%สิ่ง"} println ("เมืองทั้งหมด:") สำหรับ (City in Cities.selectall ()) {println ("$ {เมือง [เมือง id]}: $ {city [cities.name]} ") } println ("คู่มือเข้าร่วม:") (ผู้ใช้ InnerJoin Cities) .Select (users.Name, Cities.Name) .ที่ไหน { (users.id.eq ("Andrey") หรือ users.name.eq ("sergey")) Andusers.id.eq ("sergey") และ users.cityid.eq (Cities.id) } .Foreach {println ("$ {it [users.name]} อาศัยอยู่ใน $ {it [cities.name]}")) } println ("เข้าร่วมกับคีย์ต่างประเทศ:") (ผู้ใช้ InnerJoin Cities) .Select (users.name, users.cityId, Cities.name) .where {cities.name.eq ("St. Petersburg") หรือ users.cityid.isnull ()} .Foreach {ถ้า (it [users.cityId]! = null) {println ("$ {it [users.name]} อาศัยอยู่ใน $ {it [cities.name]}")) } else {println ("$ {it [users.name]} มีชีวิตอยู่ที่ไหนเลย") - } println ("ฟังก์ชั่นและกลุ่มโดย:") ((เมืองในเมืองเข้าร่วม) ผู้ใช้) .Select (Cities.name, users.id.count ()) .groupby (Cities.Name) ). foreach {val cityname = it [cities.name] val usercount = it [users.id.count ()] ถ้า (usercount> 0) {println ("$ usercount ผู้ใช้อาศัยอยู่ใน $ cityname" ) } else {println ("ไม่มีใครอยู่ใน $ cityname") - } schemautils.drop (ผู้ใช้เมือง) - -
สร้าง SQL:
SQL: สร้างตารางหากไม่มีเมือง (ID int auto_increment, ชื่อ varchar (50) ไม่ใช่ NULL, ข้อ จำกัด PK_CITIES_ID คีย์หลัก (ID)) SQL: สร้างตารางหากไม่มีผู้ใช้ (id varchar (10), ชื่อ varchar (50) ไม่ใช่ null, city_id int null, ข้อ จำกัด pk_user_id คีย์หลัก (ID), ข้อ จำกัด FK_USERS_CITY_ID__ID คีย์ต่างประเทศ (CITY_ID) ในการ จำกัด การอัปเดต) SQL: แทรกเข้าไปในเมือง (ชื่อ) ค่า ('เซนต์ปีเตอร์สเบิร์ก') SQL: แทรกลงในเมือง (ชื่อ) ค่า ('มิวนิค') SQL: แทรกลงในเมือง (ชื่อ) ค่า (substring (trim ('prague'), 1, 2)) SQL: เลือก Cities.id, Cities.name จากเมืองที่ Cities.id = 3praguename = PR SQL: แทรกลงในผู้ใช้ (id, ชื่อ, city_id) ค่า ('Andrey', 'Andrey', 1) SQL: แทรกลงในผู้ใช้ (id, ชื่อ, city_id) ค่า ('sergey', 'sergey', 2) SQL: แทรกลงในผู้ใช้ (id, ชื่อ, city_id) ค่า ('Eugene', 'Eugene', 2) SQL: แทรกลงในผู้ใช้ (ID, ชื่อ, city_id) ค่า ('Alex', 'Alex', Null) SQL: แทรกลงในผู้ใช้ (id, ชื่อ, city_id) ค่า ('smth', 'บางสิ่ง', null) SQL: อัปเดตผู้ใช้ SET NAME = 'Alexey' โดยที่ users.id = 'Alex'Sql: ลบจากผู้ใช้ที่ผู้ใช้ชื่อเช่น'%สิ่งที่ทุกเมือง: SQL: เลือก Cities.id, Cities.Name จาก Cities1: St. Petersburg2: Munich3: PR การเข้าร่วมด้วยตนเอง: SQL: เลือก users.name, Cities.Name จากผู้ใช้ในเมืองเข้าร่วมในเมืองใน Cities.id = users.city_id โดยที่ (users.id = 'Andrey') หรือ (users.name = 'sergey')) และ (users.id = 'sergey') และ (users.city_id = cities.id) Sergey อาศัยอยู่ใน Munichjoin กับ Key ต่างประเทศ: SQL: เลือก users.name, users.city_id, Cities.name จากผู้ใช้ในเมืองเข้าร่วมในเมืองใน Cities.id = users.city_id ที่ (Cities.name = 'St. Petersburg') หรือ (users.city_id เป็นโมฆะ) Andrey อาศัยอยู่ในเซนต์ปีเตอร์สเบิร์ก ฟังก์ชั่นและกลุ่มโดย: SQL: เลือก Cities.name, Count (users.id) จากเมืองเข้าร่วมในเมืองผู้ใช้ใน Cities.id = users.city_id Group โดย Cities.name1 ผู้ใช้ Live (S) ใน St. Petersburg2 ผู้ใช้ Live (S) ) ในมิวนิค SQL: Drop Table หากมีผู้ใช้ SQL: Drop Table หากมีเมือง
นำเข้า org.jetbrains.exposed.dao.*นำเข้า org.jetbrains.exposed.dao.id.entityidimport org.jetbrains.exposed.dao.id.intidtableimport org.jetbrains.exposed.sql. .Transactions.TransactionObject ผู้ใช้: intidTable () {val name = varchar ("ชื่อ", 50) .index () Val City = การอ้างอิง ("เมือง", เมือง) Val Age = Integer ("อายุ") } Object Cities: IntidTable () {val name = varchar ("ชื่อ", 50) } ผู้ใช้คลาส (ID: EntityId <int>): ความตั้งใจ (ID) {วัตถุสหาย: IntentityClass <ผู้ใช้> (ผู้ใช้) ชื่อ VAR โดยผู้ใช้. } class city (id: entityId <int>): ความตั้งใจ (id) {วัตถุสหาย: intentityclass <yity> (เมือง) ชื่อ VAR โดย Cities.nameval ผู้ใช้โดยผู้ใช้ผู้ใช้ผู้ใช้ } fun main () {database.connect ("jdbc: h2: mem: test", driver = "org.h2.driver", user = "root", รหัสผ่าน = "") ธุรกรรม { addlogger (stdoutsqllogger) schemautils.create (เมืองผู้ใช้) val stpete = city.new { name = "St. Petersburg"} val Munich = city.new { name = "Munich"} user.new { name = "a" city = stpete อายุ = 5} user.new { name = "b" เมือง = stpete อายุ = 27} user.new { name = "c" เมือง = มิวนิค อายุ = 42} println ("เมือง: $ {city.all (). Jointostring {it.name}}") println ("ผู้ใช้ใน $ {stpete.name}: $ {stpete.users.jointoString {it.name}} } ") println (" ผู้ใหญ่: $ {user.find {users.age Greatereq 18} .JoinToString {it.name}} ") - -
สร้าง SQL:
SQL: สร้างตารางหากไม่มีเมือง (ID int auto_increment คีย์หลักชื่อ varchar (50) ไม่ใช่ null) SQL: สร้างตารางหากไม่มีผู้ใช้ (ID int auto_increment คีย์หลักชื่อ varchar (50) ไม่ใช่ Null, City int ไม่เป็นโมฆะอายุ int ไม่ว่าง, ข้อ จำกัด fk_users_city__id คีย์ต่างประเทศ (เมือง) อ้างอิงเมือง (ID) จำกัด) SQL: สร้างดัชนี users_name บนผู้ใช้ (ชื่อ) SQL: แทรกเข้าไปในเมือง (ชื่อ) ค่า ('เซนต์ปีเตอร์สเบิร์ก') SQL: แทรกลงในเมือง (ชื่อ) ค่า ('มิวนิค') SQL: เลือก Cities.id, Cities.Name จากเมืองต่างๆ เมือง: เซนต์ปีเตอร์สเบิร์กมิวนิค SQL: แทรกลงในผู้ใช้ (ชื่อเมืองอายุ) ค่า ('a', 1, 5) SQL: แทรกลงในผู้ใช้ (ชื่อเมืองอายุ) ค่า ('B', 1, 27) SQL: แทรกลงในผู้ใช้ (ชื่อเมืองอายุ) ค่า ('C', 2, 42) SQL: เลือก users.id, users.name, users.city, users.age จากผู้ใช้ที่ users.city = 1users ใน St. Petersburg: A, B SQL: เลือก users.id, users.name, users.city, users.age จากผู้ใช้ที่ users.age> = 18adults: b, c
โปรดดูคู่มือการบริจาคก่อนที่จะมีส่วนร่วม
โดยการมีส่วนร่วมในโครงการที่เปิดเผยคุณยอมรับว่าการบริจาคของคุณจะได้รับใบอนุญาตภายใต้ใบอนุญาต Apache เวอร์ชัน 2.0