关于java:Java-ORM-框架ObjectiveSQL-集成SpringBoot

27次阅读

共计 3059 个字符,预计需要花费 8 分钟才能阅读完成。

源码地址:https://github.com/braisdom/ObjectiveSql/tree/master/springboot_example

ObjectiveSQL(简称: objsql,中文译为: 对象化 SQL 或者面向对象 SQL)是一种 ORM 技术在 Java 中的利用,其次要思维为 ActiveRecord,使传统利用程序开发中的 VO,PO 等变为真正的 Java Class,不仅领有相干状态,同时也应有相干行为。ObjectiveSQL 基于 JSR 269,实现了数据库拜访的动静代码生成,并且在其中封装了关系型数据库的罕用逻辑,使得利用程序开发变得极其简略,因为动静代码生成,ObjectiveSQL 不依赖内部依赖注入型框架,能够在任何环境下独立运行。

SpringBoot 是一种疾速结构应用程序的框架,以动静代理 (Dynamic Proxy) 为主,基于 Java Annotation 进行利用零碎扩大点,从而实现对根底框架无感的模式进行零碎开发。

ObjectiveSQL 和 SpringBoot 能够无缝联合,相似于 Rails 中的 WEB 和 ActiveRecord 开发一样,具体代码如下:

增加 Maven 依赖:

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

Controller 定义:

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 主动生成.

正文完
 0