gaarason / database-all

Eloquent ORM for Java 8, 11, 17, 21 and Spring boot 2.x , 3.x
MIT License
819 stars 25 forks source link
database eloquent eloquent-orm java laravel model mysql orm orm-framework orm-library spring-boot starter

database

[]() []() []() []()

[]() []()

Eloquent ORM for Java

简介 Introduction

目录

// select * from student where id = 4 limit 1
Student student = studentModel.find(4).toObject();

// select * from student where id = 4 limit 1
Student student = studentModel.newQuery().query("select * from student where id= ? limit ? ", 4, 1).toObject();

// select name,age from student where id in (1, 2, 3)
List<Student> students = studentModel.newQuery()
        .select(Student::getName).select(Student::getAge)
        .whereIn(Student::getId, 1, 2, 3)
        .get().toObjectList();

// select id,name from student where id=3 or(age>11 and id=7 and(id between 4 and 10 and age>11))
List<Student> students = studentModel.newQuery().where("id", "3").orWhere(
                builder->builder.where("age", ">", "11").where("id", "7").andWhere(
            builder2->builder2.whereBetween("id", "4", "10").where("age", ">", "11")
        )
).select("id", "name").get().toObjectList();

// select * from student where id in (1, 2, 3)
// select * from teacher where id in (?, ?, ?)
// select * from father where id in (?, ?, ?)
// select * from house where owner_id in (?, ?, ?)
List<Student> students = studentModel.newQuery().whereIn("id", 1, 2, 3).get().with("teacher.father.house").toObjectList();

// select * from student where id = 8 limit 1
// select * from relation_student_teacher where student_id = 8 and teacher_id in (1, 2, 3)
// insert into relation_student_teacher set student_id = 8 and teacher_id = 3
studentModel.findOrFail(8).bind("teachers").attach( 1, 2, 3 );

Spring boot Quick start

1.引入仓库 pom.xml

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

2.引入依赖 pom.xml

latest-version

<dependency>
    <groupId>com.github.gaarason.database-all</groupId>
    <artifactId>database-spring-boot-starter</artifactId>
    <version>{latest-version}</version>
</dependency>

3.配置连接 application.properties

spring.datasource.url=jdbc:mysql://mysql.local/test_master_0?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=true&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 使用你喜欢的 datasource, 这边增加了 DruidDataSource 的支持, 使其符合 Spring 的指定风格
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# 雪花算法工作id, 默认是0
# gaarason.database.snow-flake.worker-id=1

# 包扫描路径, 默认是`@SpringBootApplication`所在的包
# 非 SpringBoot, 建议手动指定包扫描路径
# gaarason.database.scan.packages=you.package1,you.package2

4.快速开始 quick start

使用预置的 GeneralModel ,无需其他定义,即可进行查询。
Using the pre-built GeneralModel, no additional definitions are required to query.

@Resource
GeneralModel generalModel;

@Test
public void simpleQuery() {

    // select * from student where id = 3 limit 1
    Record<?, ?> record = generalModel.newQuery().from("student").where("id", 3).firstOrFail();

    // to map
    Map<String, Object> stringObjectMap = record.toMap();

    System.out.println(stringObjectMap);
}

完整体验 Complete experience