提交 33ac78bf 编写于 作者: M MaxKey

add findByIds and Split TestRunner

上级 3e54608a
......@@ -32,7 +32,7 @@ public class CurdTestRunner {
public static StudentsService service;
@Test
public void insert() throws Exception{
void insert() throws Exception{
_logger.info("insert...");
Students student=new Students();
student.setStdNo("10024");
......@@ -48,7 +48,7 @@ public class CurdTestRunner {
}
@Test
public void merge() throws Exception{
void merge() throws Exception{
_logger.info("merge...");
Students student=new Students();
student.setStdNo("10024");
......@@ -65,7 +65,7 @@ public class CurdTestRunner {
}
@Test
public void get() throws Exception{
void get() throws Exception{
_logger.info("get...");
Students student=service.get("317d5eda-927c-4871-a916-472a8062df23");
System.out.println("Students "+student);
......@@ -90,13 +90,13 @@ public class CurdTestRunner {
}
@Test
public void remove() throws Exception{
void remove() throws Exception{
_logger.info("remove...");
service.remove("921d3377-937a-4578-b1e2-92fb23b5e512");
}
@Test
public void batchDelete() throws Exception{
void batchDelete() throws Exception{
_logger.info("batchDelete...");
List<String> idList=new ArrayList<String>();
idList.add("8584804d-b5ac-45d2-9f91-4dd8e7a090a7");
......@@ -106,33 +106,6 @@ public class CurdTestRunner {
service.deleteBatch(idList);
}
@Test
public void logicDelete() throws Exception{
_logger.info("logicDelete...");
List<String> idList=new ArrayList<String>();
idList.add("8584804d-b5ac-45d2-9f91-4dd8e7a090a7");
idList.add("ab7422e9-a91a-4840-9e59-9d911257c918");
idList.add("12b6ceb8-573b-4f01-ad85-cfb24cfa007c");
idList.add("dafd5ba4-d2e3-4656-bd42-178841e610fe");
service.logicDelete(idList);
}
@Test
public void batchDeleteByIds() throws Exception{
_logger.info("batchDeleteByIds...");
service.deleteBatch("2");
service.deleteBatch("2,639178432667713536");
}
@Test
public void findAll() throws Exception{
_logger.info("findAll...");
List<Students> allListStudents =service.findAll();
for (Students s : allListStudents) {
_logger.info("Students "+s);
}
}
@BeforeAll
public static void initSpringContext(){
if(InitContext.context!=null) return;
......
......@@ -19,9 +19,7 @@ package org.dromara.mybatis.jpa.test;
import java.util.List;
import org.dromara.mybatis.jpa.entity.JpaPage;
import org.dromara.mybatis.jpa.entity.JpaPageResults;
import org.dromara.mybatis.jpa.query.Query;
import org.dromara.mybatis.jpa.test.dao.service.StudentsService;
import org.dromara.mybatis.jpa.test.entity.Students;
import org.junit.jupiter.api.BeforeAll;
......@@ -34,7 +32,7 @@ public class FetchPageResultsTestRunner {
public static StudentsService service;
@Test
public void fetchPageResults() throws Exception{
void fetchPageResults() throws Exception{
_logger.info("fetchPageResults...");
Students student=new Students();
......@@ -54,43 +52,7 @@ public class FetchPageResultsTestRunner {
}
@Test
public void fetch() throws Exception{
_logger.info("fetch...");
JpaPage page = new JpaPage();
Students student = new Students();
student.setStdGender("M");
student.setStdAge(40);
page.setPageSize(20);
page.setPageable(true);
JpaPageResults<Students> results = service.fetch(page,student);
List<Students> rowsStudents = results.getRows();
_logger.info("records {} , totalPage {} , total {} , page {} ",
results.getRecords(),results.getTotalPage(),results.getTotal(),results.getPage());
for (Students s : rowsStudents) {
_logger.info("Students "+s);
}
}
@Test
public void fetchByCondition() throws Exception{
_logger.info("fetchByCondition...");
JpaPage page = new JpaPage();
Query condition = new Query().eq("stdMajor", "政治").and().gt("STDAGE", 30);
page.setPageSize(20);
page.setPageable(true);
JpaPageResults<Students> results = service.fetch(page,condition);
List<Students> rowsStudents = results.getRows();
_logger.info("records {} , totalPage {} , total {} , page {} ",
results.getRecords(),results.getTotalPage(),results.getTotal(),results.getPage());
for (Students s : rowsStudents) {
_logger.info("Students "+s);
}
}
@Test
public void fetchPageResultsByMapperId() throws Exception{
void fetchPageResultsByMapperId() throws Exception{
_logger.info("fetchPageResults by mapperId...");
Students student=new Students();
......
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mybatis.jpa.test;
import java.util.List;
import org.dromara.mybatis.jpa.entity.JpaPage;
import org.dromara.mybatis.jpa.entity.JpaPageResults;
import org.dromara.mybatis.jpa.query.Query;
import org.dromara.mybatis.jpa.test.dao.service.StudentsService;
import org.dromara.mybatis.jpa.test.entity.Students;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FetchTestRunner {
private static final Logger _logger = LoggerFactory.getLogger(FetchTestRunner.class);
public static StudentsService service;
@Test
void fetch() throws Exception{
_logger.info("fetch...");
JpaPage page = new JpaPage();
Students student = new Students();
student.setStdGender("M");
student.setStdAge(40);
page.setPageSize(20);
page.setPageable(true);
JpaPageResults<Students> results = service.fetch(page,student);
List<Students> rowsStudents = results.getRows();
_logger.info("records {} , totalPage {} , total {} , page {} ",
results.getRecords(),results.getTotalPage(),results.getTotal(),results.getPage());
for (Students s : rowsStudents) {
_logger.info("Students "+s);
}
}
@Test
void fetchByCondition() throws Exception{
_logger.info("fetchByCondition...");
JpaPage page = new JpaPage();
Query condition = new Query().eq("stdMajor", "政治").and().gt("STDAGE", 30);
page.setPageSize(20);
page.setPageable(true);
JpaPageResults<Students> results = service.fetch(page,condition);
List<Students> rowsStudents = results.getRows();
_logger.info("records {} , totalPage {} , total {} , page {} ",
results.getRecords(),results.getTotalPage(),results.getTotal(),results.getPage());
for (Students s : rowsStudents) {
_logger.info("Students "+s);
}
}
@BeforeAll
public static void initSpringContext(){
if(InitContext.context!=null) return;
service = new InitContext().init();
}
}
\ No newline at end of file
/*
* Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mybatis.jpa.test;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import org.dromara.mybatis.jpa.test.dao.service.StudentsService;
import org.dromara.mybatis.jpa.test.entity.Students;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FindTestRunner {
private static final Logger _logger = LoggerFactory.getLogger(FindTestRunner.class);
public static StudentsService service;
@Test
void findAll() throws Exception{
_logger.info("findAll...");
List<Students> allListStudents =service.findAll();
for (Students s : allListStudents) {
_logger.info("Students "+s);
}
}
@Test
void findByIds() throws Exception{
_logger.info("findByIds...");
List<String> idList=new ArrayList<String>();
idList.add("8c34448b-c65b-4a4e-a0da-83284d05f909");
idList.add("b9111f83-d338-461d-8d46-f331087d5a42");
idList.add("12b6ceb8-573b-4f01-ad85-cfb24cfa007c");
idList.add("dafd5ba4-d2e3-4656-bd42-178841e610fe");
service.findByIds(idList);
}
@Test
void find() throws Exception{
_logger.info("find by filter StdNo = '10024' or StdNo = '10004'");
List<Students> listStudents = service.find(
" StdNo = ? or StdNo = ? ",
new Object[]{"10024","10004"},
new int[]{Types.VARCHAR,Types.INTEGER}
);
for (Students s : listStudents) {
_logger.info("Students {}" , s);
}
}
@BeforeAll
public static void initSpringContext(){
if(InitContext.context!=null) return;
service = new InitContext().init();
}
}
\ No newline at end of file
/*
* Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mybatis.jpa.test;
import java.util.ArrayList;
import java.util.List;
import org.dromara.mybatis.jpa.test.dao.service.StudentsService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogicDeleteTestRunner {
private static final Logger _logger = LoggerFactory.getLogger(LogicDeleteTestRunner.class);
public static StudentsService service;
@Test
void logicDelete() throws Exception{
_logger.info("logicDelete...");
List<String> idList=new ArrayList<String>();
idList.add("8584804d-b5ac-45d2-9f91-4dd8e7a090a7");
idList.add("ab7422e9-a91a-4840-9e59-9d911257c918");
idList.add("12b6ceb8-573b-4f01-ad85-cfb24cfa007c");
idList.add("dafd5ba4-d2e3-4656-bd42-178841e610fe");
service.logicDelete(idList);
}
@Test
void batchDeleteByIds() throws Exception{
_logger.info("batchDeleteByIds...");
service.deleteBatch("2");
service.deleteBatch("2,639178432667713536");
}
@BeforeAll
public static void initSpringContext(){
if(InitContext.context!=null) return;
service = new InitContext().init();
}
}
\ No newline at end of file
......@@ -17,7 +17,6 @@
package org.dromara.mybatis.jpa.test;
import java.sql.Types;
import java.util.List;
import org.dromara.mybatis.jpa.query.Query;
......@@ -34,7 +33,7 @@ public class QueryTestRunner {
public static StudentsService service;
@Test
public void query() throws Exception{
void query() throws Exception{
_logger.info("find...");
List<Students> listStudents =service.query(new Students("10024"));
for (Students s : listStudents) {
......@@ -44,7 +43,7 @@ public class QueryTestRunner {
//WHERE (stdMajor = '政治' and STDAGE > 30 and stdMajor in ( '政治' , '化学' ) or ( stdname = '周瑜' or stdname = '吕蒙' ) )
@Test
public void queryByCondition() throws Exception{
void queryByCondition() throws Exception{
_logger.info("query by condition ...");
List<Students> listStudents =service.query(
new Query().eq("stdMajor", "政治").and().gt("STDAGE", 30).and().in("stdMajor", new Object[]{"政治","化学"})
......@@ -54,21 +53,6 @@ public class QueryTestRunner {
}
}
@Test
public void find() throws Exception{
_logger.info("find by filter StdNo = '10024' or StdNo = '10004'");
List<Students> listStudents = service.find(
" StdNo = ? or StdNo = ? ",
new Object[]{"10024","10004"},
new int[]{Types.VARCHAR,Types.INTEGER}
);
for (Students s : listStudents) {
_logger.info("Students {}" , s);
}
}
@BeforeAll
public static void initSpringContext(){
if(InitContext.context!=null) return;
......
......@@ -44,6 +44,12 @@ public interface IJpaMapper<T> {
@SelectProvider(type = MapperSqlProvider.class, method = "findAll")
public List<T> findAll(@Param (MapperMetadata.ENTITY_CLASS)Class<?> entityClass);
@SelectProvider(type = MapperSqlProvider.class, method = "findByIds")
public List<T> findByIds(
@Param (MapperMetadata.ENTITY_CLASS) Class<?> entityClass,
@Param (MapperMetadata.PARAMETER_ID_LIST) List<String> idList,
@Param (MapperMetadata.PARAMETER_PARTITION_KEY) String partitionKey);
@SelectProvider(type = MapperSqlProvider.class, method = "fetchCount")
public Integer fetchCount(JpaPage page);
......
......@@ -19,6 +19,7 @@ package org.dromara.mybatis.jpa;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import org.dromara.mybatis.jpa.entity.JpaEntity;
import org.dromara.mybatis.jpa.entity.JpaPage;
......@@ -329,6 +330,74 @@ public class JpaService <T extends JpaEntity> {
return findOne( filter ,null , null);
}
/**
* find entity by id List
* @param idList
* @return List<T>
*/
public List<T> findByIds(List<String> idList) {
try {
logger.trace("findByIds {}" , idList);
List<T> findList = getMapper().findByIds(this.entityClass,idList,null);
logger.trace("findByIds count : {}" , findList.size());
return findList;
} catch(Exception e) {
logger.error("findByIds Exception " , e);
}
return Collections.emptyList();
}
/**
* find entity by id List
* @param idList
* @param partitionKey
* @return List<T>
*/
public List<T> findByIds(List<String> idList,String partitionKey) {
try {
logger.trace("findByIds {} , partitionKey {}" , idList , partitionKey);
List<T> findList = getMapper().findByIds(this.entityClass , idList , partitionKey);
logger.debug("findByIds count : {}" , findList.size());
return findList;
} catch(Exception e) {
logger.error("findByIds Exception " , e);
}
return Collections.emptyList();
}
/**
* find entity by ids,split with ,
* @param ids
* @return List<T>
*/
public List<T> findByIds(String ids) {
List<String> idList = StringUtils.string2List(ids, ",");
return findByIds(idList);
}
/**
* find entity by ids,split with ,
* @param ids
* @param partitionKey
* @return
*/
public List<T> findByIds(String ids,String partitionKey) {
List<String> idList = StringUtils.string2List(ids, ",");
return findByIds(idList,partitionKey);
}
/**
* find entity by ids , split with ,
* @param ids
* @param split
* @return List<T>
*/
public List<T> findByIdsSplit(String ids , String split) {
List<String> idList = StringUtils.string2List(ids, StringUtils.isBlank(split)? "," : split );
return findByIds(idList);
}
/**
* query one entity by entity id
* @param id
......
......@@ -21,6 +21,7 @@
package org.dromara.mybatis.jpa.provider;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
......@@ -116,4 +117,41 @@ public class FindProvider <T extends JpaEntity>{
return findSql;
}
public String findByIds(Map<String, Object> parametersMap) {
Class<?> parameterEntityClass = (Class<?>)parametersMap.get(MapperMetadata.ENTITY_CLASS);
MapperMetadata.buildColumnList(parameterEntityClass);
ArrayList <String> parameterIds = (ArrayList<String>)parametersMap.get(MapperMetadata.PARAMETER_ID_LIST);
StringBuffer keyValue = new StringBuffer();
for(String value : parameterIds) {
if(value.trim().length() > 0) {
keyValue.append(",'").append(value).append("'");
logger.trace("find by id {}" , value);
}
}
String idsValues = keyValue.substring(1).replaceAll(";", "");//remove ;
String partitionKeyValue = (String) parametersMap.get(MapperMetadata.PARAMETER_PARTITION_KEY);
FieldColumnMapper partitionKeyColumnMapper = MapperMetadata.getPartitionKey((parameterEntityClass).getSimpleName());
FieldColumnMapper idFieldColumnMapper = MapperMetadata.getIdColumn(parameterEntityClass.getSimpleName());
SQL sql = MapperMetadata.buildSelect(parameterEntityClass);
if(partitionKeyColumnMapper != null && partitionKeyValue != null) {
sql.WHERE("%s = #{%s} and %s in ( %s )"
.formatted(
partitionKeyColumnMapper.getColumnName() ,
partitionKeyValue,
idFieldColumnMapper.getColumnName(),
idsValues)
);
}else {
sql.WHERE(" %s in ( %s )".formatted(idFieldColumnMapper.getColumnName(),idsValues));
}
String findByIdsSql = sql.toString();
logger.trace("Find by ids SQL \n{}" , findByIdsSql);
return findByIdsSql;
}
}
......@@ -48,6 +48,10 @@ public class MapperSqlProvider <T extends JpaEntity>{
return new FindProvider().find(parametersMap);
}
public String findByIds(Map<String, Object> parametersMap) {
return new FindProvider().findByIds(parametersMap);
}
public String findAll(Map<String, Object> parametersMap) {
return new FindProvider().findAll(parametersMap);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册