Skip to content

Commit

Permalink
新增批量插入顶点非空属性insertSelectiveBatch()函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangjq-K committed May 15, 2024
1 parent 4058ee1 commit 82ca247
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
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,54 @@ 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
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

0 comments on commit 82ca247

Please sign in to comment.