Skip to content

Commit

Permalink
- 支持多态关联关系
Browse files Browse the repository at this point in the history
  • Loading branch information
xutengx committed Dec 30, 2022
1 parent 7f5817f commit 6be9fc8
Show file tree
Hide file tree
Showing 27 changed files with 1,482 additions and 201 deletions.
58 changes: 35 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,55 @@ Eloquent ORM for Java
* [GraalVM](/document/graalvm.md)
* [版本信息](/document/version.md)


- 以如下的方式在程序中查询数据
- 查询 model.newQuery().select().where().get().toObject();
- 更新 model.newQuery().data().where().update();
- 删除 model.newQuery().where().delete();
- 新增 model.newQuery().column().value().insert();

```java
// 查询id为4的一条数据 select * from student where id = 4 limit 1
// 查询id为4的一条数据
// select * from student where id = 4 limit 1
Student student = studentModel.find(4).toObject();

// 查询id为4的一条数据 select * from student where id = 4 limit 1
// 查询id为4的一条数据
// 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)
// 表达式列名风格
// 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)
.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 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();


// 增加关联 给id为8的学生增加3名老师(id分别为1,2,3)
studentModel.findOrFail(8).bind("teachers").attach( teacherModel.findMany(1, 2, 3) );
// 增加关联 给id为8的学生增加3名老师(id分别为1,2,3), 已存在的不重复添加
// 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 快速开始

1.引入仓库 pom.xml

```$xslt
```xml
<repositories>
<repository>
<id>jitpack.io</id>
Expand All @@ -79,17 +87,19 @@ studentModel.findOrFail(8).bind("teachers").attach( teacherModel.findMany(1, 2,

2.引入依赖 pom.xml

```$xslt
**latest-version**![](https://jitpack.io/v/gaarason/database-all.svg)

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

3.配置连接 application.properties

```$xslt
```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
Expand All @@ -107,20 +117,22 @@ spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

4.快速开始

使用预置的 `GeneralModel` ,无需其他定义,即可进行查询。

```java
@Resource
GeneralModel generalModel;

@Test
public void 简单查询(){
public void 简单查询() {

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

// 结果转化到map
Map<String, Object> stringObjectMap = record.toMap();
// 结果转化到map
Map<String, Object> stringObjectMap = record.toMap();

System.out.println(stringObjectMap);
System.out.println(stringObjectMap);
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,24 @@
String localModelForeignKey();

/**
* `父表`中的`关联键`, 默认值为`父表`的主键(`@Primary()`修饰的键)
* `父表`中的`关联键`
* 默认值为`父表`的主键(`@Primary()`修饰的键)
* @return `父表`中的`关联键`
*/
String parentModelLocalKey() default "";

/**
* `本表`中的`多态类型键`
* 默认空, 表示不启用多态
* @return `本表`中的`多态类型键`
*/
String localModelMorphKey() default "";

/**
* `本表`中的`多态类型键`的值
* 默认值为`父表`的表名
* localModelMorphKey 为空时, 表示不启用多态
* @return `本表`中的`多态类型键`的值
*/
String localModelMorphValue() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
Class<? extends Model<?, ?>> relationModel();

/**
* `本表`中的`关联键`, 默认值为`本表`的主键(`@Primary()`修饰的键)
* `本表`中的`关联键`
* 默认值为`本表`的主键(`@Primary()`修饰的键)
* @return `本表`中的`关联键`
*/
String localModelLocalKey() default "";
Expand All @@ -39,8 +40,39 @@
String foreignKeyForTargetModel();

/**
* `目标表`中的`关联键`, 默认值为`目标表`的主键(`@Primary()`修饰的键)
* `目标表`中的`关联键`
* 默认值为`目标表`的主键(`@Primary()`修饰的键)
* @return `目标表`中的`关联键`
*/
String targetModelLocalKey() default "";

/**
* `关系表`中的`本表`的`多态类型键`
* 默认空, 表示对于`本表`不启用多态
* @return `关系表`中的`本表`的`多态类型键`
*/
String morphKeyForLocalModel() default "";

/**
* `关系表`中的`本表`的`多态类型键`的值
* 默认值为`本表`的表名
* morphKeyForLocalModel 为空时, 表示对于`本表`不启用多态
* @return `关系表`中的`本表`的`多态类型键`的值
*/
String morphValueForLocalModel() default "";

/**
* `关系表`中的`目标表`的`多态类型键`
* 默认空, 表示对于`目标表`不启用多态
* @return `关系表`中的`目标表`的`多态类型键`
*/
String morphKeyForTargetModel() default "";

/**
* `关系表`中的`目标表`的`多态类型键`的值
* 默认值为`目标表`的表名
* morphKeyForTargetModel 为空时, 表示对于`目标表`不启用多态
* @return `关系表`中的`目标表`的`多态类型键`的值
*/
String morphValueForTargetModel() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,25 @@
String sonModelForeignKey();

/**
* `本表`中的`关联键`, 默认值为`本表`的主键(`@Primary()`修饰的键)
* `本表`中的`关联键`
* 默认值为`本表`的主键(`@Primary()`修饰的键)
* @return `本表`中的`关联键`
*/
String localModelLocalKey() default "";

/**
* `子表`中的`多态类型键`
* 默认空, 表示不启用多态
* @return `子表`中的`多态类型键`
*/
String sonModelMorphKey() default "";

/**
* `子表`中的`多态类型键`的值
* 默认值为`本表`的表名
* sonModelMorphKey 为空时, 表示不启用多态
* @return `子表`中的`多态类型键`的值
*/
String sonModelMorphValue() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gaarason.database.contract.eloquent.Record;
import gaarason.database.contract.eloquent.RecordList;
import gaarason.database.contract.function.GenerateSqlPartFunctionalInterface;
import gaarason.database.lang.Nullable;

import java.util.Collection;
import java.util.List;
Expand All @@ -29,15 +30,15 @@ public interface RelationSubQuery {
* @param builderForRelation 中间表查询构造器
* @return 查询结果集
*/
RecordList<?, ?> dealBatchForRelation(Builder<?, ?> builderForRelation);
RecordList<?, ?> dealBatchForRelation(@Nullable Builder<?, ?> builderForRelation);

/**
* 批量关联查询 (目标表)
* @param builderForTarget 目标表查询构造器
* @param relationRecordList @BelongsToMany 中间表数据
* @return 查询结果集
*/
RecordList<?, ?> dealBatchForTarget(Builder<?, ?> builderForTarget, RecordList<?, ?> relationRecordList);
RecordList<?, ?> dealBatchForTarget(@Nullable Builder<?, ?> builderForTarget, RecordList<?, ?> relationRecordList);

/**
* 筛选批量关联查询结果对象
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public Record<T, K> with(String fieldName, GenerateSqlPartFunctionalInterface<?,
// 快捷类型
if (columnArr.length > 1) {
String lastLevelColumn = columnArr[columnArr.length - 1];
String otherLevelColumn = StringUtils.rtrim(fieldName, "." + lastLevelColumn);
String otherLevelColumn = fieldName.substring(0, fieldName.length() - (lastLevelColumn.length() + 1));
return with(otherLevelColumn, builder -> builder,
thrRecord -> thrRecord.with(lastLevelColumn, ObjectUtils.typeCast(builderClosure), recordClosure));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gaarason.database.contract.eloquent.relation.RelationSubQuery;
import gaarason.database.core.Container;
import gaarason.database.eloquent.RecordListBean;
import gaarason.database.lang.Nullable;
import gaarason.database.provider.ModelShadowProvider;
import gaarason.database.support.FieldMember;
import gaarason.database.support.ModelMember;
Expand Down Expand Up @@ -51,8 +52,27 @@ protected static Set<Object> getColumnInMapList(List<Map<String, Object>> column
return result;
}

/**
* 将数据源中的某一列,转化为可以使用 where in 查询的 set
* @param columnValueMapList 数据源
* @param column 目标列
* @param morphKey 多态key
* @param morphValue 多态value
* @return 目标列的集合
*/
protected static Set<Object> getColumnInMapList(List<Map<String, Object>> columnValueMapList, String column, String morphKey, String morphValue) {
Set<Object> result = new HashSet<>();
for (Map<String, Object> stringColumnMap : columnValueMapList) {
Object mKeyValue = stringColumnMap.get(morphKey);
if(mKeyValue != null && mKeyValue.equals(morphValue)){
result.add(stringColumnMap.get(column));
}
}
return result;
}

@Override
public RecordList<?, ?> dealBatchForRelation(Builder<?, ?> builderForRelation) {
public RecordList<?, ?> dealBatchForRelation(@Nullable Builder<?, ?> builderForRelation) {
return new RecordListBean<>(getContainer());
}

Expand Down
Loading

0 comments on commit 6be9fc8

Please sign in to comment.