Java ORM 框架ObjectiveSQL 集成SpringBoot

源碼地址:https://github.com/braisdom/ObjectiveSql/tree/master/springboot_examplejava

ObjectiveSQL(簡稱: objsql,中文譯爲: 對象化SQL或者面向對象SQL)是一種ORM 技術在Java 中的應用,其主要思想爲ActiveRecord,使傳統應用程序開發中的VO,PO等變爲真正的Java Class,不只擁有相關狀態,同時也應有相關行爲。ObjectiveSQL 基於JSR 269,實現了數據庫訪問的動態代碼生成,而且在其中封裝了關係型數據庫的經常使用邏輯,使得應用程序開發變得極其簡單,因爲動態代碼生成,ObjectiveSQL 不依賴外部依賴注入型框架,能夠在任何環境下獨立運行。git

SpringBoot 是一種快速構造應用程序的框架,以動態代理(Dynamic Proxy)爲主,基於Java Annotation 進行應用系統擴展點,從而實現對基礎框架無感的形式進行系統開發。github

ObjectiveSQL 和SpringBoot 能夠無縫結合,相似於Rails 中的WEB 和ActiveRecord 開發同樣,具體代碼以下:web

添加Maven 依賴:spring

<dependency>
    <groupId>com.github.braisdom</groupId>
    <artifactId>objective-sql</artifactId>
    <version>1.3.4</version>
</dependency>

Controller 定義:sql

import com.github.braisdom.example.RequestObject;
import com.github.braisdom.example.ResponseObject;
import com.github.braisdom.example.model.Member;
import com.github.braisdom.example.model.Order;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RestController
public class MembersController {

    /**
     * The post body is in "resources/json/create_member.json"
     */
    @PostMapping("/members")
    public ResponseObject create(@RequestBody RequestObject rawMember) throws SQLException {
        Member dirtyMember = Member.newInstanceFrom(rawMember, false);
        Member member = Member.create(dirtyMember, true);
        return ResponseObject.createSuccessResponse(member);
    }

    @GetMapping("/members/{no}")
    public ResponseObject getMember(@PathVariable("no") String memberNo) throws SQLException {
        Member member = Member.queryByNo(memberNo);
        return ResponseObject.createSuccessResponse(member);
    }

    @GetMapping("/members")
    public ResponseObject getMembers() throws SQLException {
        List<Member> members = Member.queryAll();
        return ResponseObject.createSuccessResponse(members);
    }

    @GetMapping("/members/{no}/orders")
    public ResponseObject getMemberOrders(@PathVariable("no") String no) throws SQLException {
        Member member = Member.queryByNo(no, Member.HAS_MANY_ORDERS, Order.HAS_MANY_ORDER_LINES);
        return ResponseObject.createSuccessResponse(member);
    }

    @PutMapping("/members/{no}")
    public ResponseObject updateMember(@PathVariable("no") String memberNo,
                                       @RequestBody RequestObject rawMember) throws SQLException {
        Member member = Member.queryByNo(memberNo);
        Member.update(member.getId(), Member.newInstanceFrom(rawMember), true);
        return ResponseObject.createSuccessResponse();
    }

    @DeleteMapping("/members/{no}")
    public ResponseObject deleteMember(@PathVariable("no") String memberNo) throws SQLException {
        int deleteCount = Member.destroy("member_no = ?");
        return ResponseObject.createSuccessResponse(deleteCount);
    }
}
import com.github.braisdom.objsql.annotations.Column;
import com.github.braisdom.objsql.annotations.DomainModel;
import com.github.braisdom.objsql.annotations.Queryable;
import com.github.braisdom.objsql.annotations.Relation;
import com.github.braisdom.objsql.relation.RelationType;

import java.util.List;

@DomainModel
public class Member {
    @Queryable
    @Column(updatable = false)
    private String no;

    @Queryable
    private String name;
    private Integer gender;
    private String mobile;
    private String otherInfo;

    @Relation(relationType = RelationType.HAS_MANY)
    private List<Order> orders;

}

能夠看到,簡單的DomainModel 無需定義任務方法,就要以實現對數據庫的INSERT, UPDATE, DELETE, SELETE 等操做,這些方法均由ObjectiveSQL 自動生成.數據庫

相關文章
相關標籤/搜索