Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: insertSelectiveBatch() to bulk insert of vertices with non-null properties #299

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/en/md/dev-example/dao-basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public class PersonServiceImpl {

@Autowired private PersonDao dao;

// Regardless of whether the attribute is empty or not,
// if there is a corresponding ID value in the database,
// other properties will be overwritten
//If there is already a corresponding ID value
//the insert operation will not be performed.
public void insert( Person person ) {
dao.insert( person );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.nebula.contrib.ngbatis.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ye.weicheng.ngbatis.demo.pojo.Employee;
import ye.weicheng.ngbatis.demo.pojo.Like;
import ye.weicheng.ngbatis.demo.pojo.LikeWithRank;
import ye.weicheng.ngbatis.demo.pojo.Person;
Expand Down Expand Up @@ -161,6 +162,7 @@ public void insertSelective() {
Person person = new Person();
person.setAge(20);
person.setName("王小冰");
person.setGender(null);
repository.insertSelective(person);
}

Expand Down Expand Up @@ -206,6 +208,56 @@ public void insertBatch() {

repository.insertBatch(people);
}

@Test
public void insertSelectiveBatch() {
Person person1 = new Person();
person1.setName("IB111");
person1.setGender("M");
person1.setAge(null);

Person person2 = new Person();
person2.setName("IB222");
person2.setAge(18);
person2.setBirthday(new Date());

Person person3 = new Person();
person3.setName("IB333");
person3.setGender("M");
person3.setBirthday(new Date());

List<Person> people = new ArrayList<>();
people.add(person1);
people.add(person2);
people.add(person3);

repository.insertSelectiveBatch(people);
}

@Test
public void insertSelectiveBatchMultiTag() {
Employee employee1 = new Employee();
employee1.setName("职员1");
employee1.setGender("F");
employee1.setPosition("后端");

Employee employee2 = new Employee();
employee2.setName("职员2");
employee2.setGender("M");
employee2.setPosition("前端");

Employee employee3 = new Employee();
employee3.setName("职员3");
employee3.setGender("F");
employee3.setPosition("测试");

List<Employee> employees = new ArrayList<>();
employees.add(employee1);
employees.add(employee2);
employees.add(employee3);

repository.insertSelectiveBatch(employees);
}
// endregion

// region update zoom
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<name>shbone</name>
<email>[email protected]</email>
</developer>
<developer>
<name>Ozjq</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ default void insertBatch(List<T> ts) {
ClassModel classModel = getClassModel(this.getClass());
MapperProxy.invoke(classModel, methodModel, ts);
}

/**
* 批量插入非空字段
* @param ts 当前Tag下多个顶点
*/
default void insertSelectiveBatch(List<? extends T> ts){
MethodModel methodModel = getMethodModel();
ClassModel classModel = getClassModel(this.getClass());
MapperProxy.invoke(classModel,methodModel,ts);
}
// endregion

// region update zoom
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/NebulaDaoBasic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@
${ id } : ( ${ ng.join( @kv.values ) } ) ${ rowLP.last ? '' : ', ' }
@}
</insert>

<insert id="insertSelectiveBatch">
@for ( row in ng_args[0] ) {
@var kv = ng.kv( row, '', true, true );
@var id = ng.id( row );
INSERT VERTEX IF NOT EXISTS
@for ( entry in @kv.multiTagColumns ) {
`${ entry.key }` (
${ ng.join( entry.value, ", ", "ng.schemaFmt" ) }
) ${ entryLP.last ? '' : ','}
@}
VALUES
${ id } : ( ${ ng.join( @kv.values ) } ) ;
@}
</insert>

<!--endregion-->

<!--region update zoom -->
Expand Down
Loading