1 查询匹配类型支持:EQ, NEQ, LIKE, LLIKE, RLIKE, LT, GT, LE, GE, IN, NIN;2 类型转换支持:S(String.class), I(Integer.class), L(Long.class), N(Double.class), D(Date.class), B(Boolean.class), G(BigDecimal.class),AS(String[].class), AI(Integer[].class), AL(Long[].class), AN(Double[].class), AD(Date[].class), AB(Boolean[].class), AG(BigDecimal[].class);3 支持关联查询,使用“.”来分隔(支持正反向的关联查询);4 支持_OR_查询;5 支持IN查询;// 测试Demopackage com.ipan.myTest;import java.util.ArrayList;import java.util.List;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.data.jpa.domain.Specification;import com.ipan.core.persistence.DynamicSpecifications;import com.ipan.core.persistence.PropertyFilter;import com.ipan.system.dao.ATestDAO;import com.ipan.system.dao.BTestDAO;import com.ipan.system.dao.CommodityTestDAO;import com.ipan.system.dao.GroupTestDAO;import com.ipan.system.dao.PersonTestDAO;import com.ipan.system.entity.A;import com.ipan.system.entity.B;import com.ipan.system.entity.Commodity;import com.ipan.system.entity.Group;import com.ipan.system.entity.Person;public class FilterSearchTest {static AbstractApplicationContext ctx = null;static PersonTestDAO personTestDAO;static CommodityTestDAO commodityTestDAO;static GroupTestDAO groupTestDAO;static ATestDAO aTestDAO;static BTestDAO bTestDAO;@BeforeClasspublic static void setUpBeforeClass() throws Exception {ctx = new ClassPathXmlApplicationContext("classpath:test-beans.xml");personTestDAO = (PersonTestDAO) ctx.getBean(PersonTestDAO.class);commodityTestDAO = (CommodityTestDAO) ctx.getBean(CommodityTestDAO.class);groupTestDAO = (GroupTestDAO) ctx.getBean(GroupTestDAO.class);aTestDAO = (ATestDAO) ctx.getBean(ATestDAO.class);bTestDAO = (BTestDAO) ctx.getBean(BTestDAO.class);}@AfterClasspublic static void tearDownAfterClass() throws Exception {ctx.close();}// 简单属性查询测试// Hibernate: select person0_.id as id1_6_, person0_.age as age2_6_, person0_.birthday as birthday3_6_, person0_.name as name4_6_ // from t_person person0_ where (person0_.name like ?) and person0_.age=25 and person0_.birthday=?//@Testpublic void search_test_1() {List<PropertyFilter> param = new ArrayList<PropertyFilter>();param.add(new PropertyFilter("LIKES_name", "小张"));param.add(new PropertyFilter("EQI_age", "25"));param.add(new PropertyFilter("EQD_birthday", "1990-01-09"));Specification<Person> spec = DynamicSpecifications.byPropertyFilter(param, Person.class);List<Person> resultList = personTestDAO.findAll(spec);for (Person p : resultList) {System.out.println(p);}}// IN查询测试// Hibernate: select person0_.id as id1_6_, person0_.age as age2_6_, person0_.birthday as birthday3_6_, person0_.name as name4_6_ // from t_person person0_ where person0_.age in (25 , 26)//@Testpublic void search_test_2() {List<PropertyFilter> param = new ArrayList<PropertyFilter>();param.add(new PropertyFilter("INAI_age", new String[]{"25", "26"}));Specification<Person> spec = DynamicSpecifications.byPropertyFilter(param, Person.class);List<Person> resultList = personTestDAO.findAll(spec);for (Person p : resultList) {System.out.println(p);}}// OneToMany查询测试// Hibernate: select person0_.id as id1_6_, person0_.age as age2_6_, person0_.birthday as birthday3_6_, person0_.name as name4_6_ // from t_person person0_ inner join t_shop shoplist1_ on person0_.id=shoplist1_.per_id inner join t_commodity commodityl2_ // on shoplist1_.id=commodityl2_.shop_id where (person0_.age in (25 , 26)) and commodityl2_.name=?//@Testpublic void search_test_3() {List<PropertyFilter> param = new ArrayList<PropertyFilter>();param.add(new PropertyFilter("INAI_age", new String[]{"25", "26"}));param.add(new PropertyFilter("EQS_shopList.commodityList.name", "商品-1"));Specification<Person> spec = DynamicSpecifications.byPropertyFilter(param, Person.class);List<Person> resultList = personTestDAO.findAll(spec);for (Person p : resultList) {System.out.println(p);}}// ManyToOne查询测试// Hibernate: select commodity0_.id as id1_3_, commodity0_.name as name2_3_, commodity0_.price as price3_3_, // commodity0_.shop_id as shop_id4_3_ from t_commodity commodity0_ inner join t_shop shop1_ on commodity0_.shop_id=shop1_.id // inner join t_person person2_ on shop1_.per_id=person2_.id where person2_.name=?//@Testpublic void search_test_4() {List<PropertyFilter> param = new ArrayList<PropertyFilter>();param.add(new PropertyFilter("EQS_shop.person.name", "小张"));Specification<Commodity> spec = DynamicSpecifications.byPropertyFilter(param, Commodity.class);List<Commodity> resultList = commodityTestDAO.findAll(spec);for (Commodity p : resultList) {System.out.println(p);}}// ManyToMany(正向)查询测试// Hibernate: select group0_.id as id1_4_, group0_.name as name2_4_ from t_group group0_ inner join t_group_person personlist1_ // on group0_.id=personlist1_.group_id inner join t_person person2_ on personlist1_.per_id=person2_.id // where group0_.id=1 and person2_.name=?//@Testpublic void search_test_5() {List<PropertyFilter> param = new ArrayList<PropertyFilter>();param.add(new PropertyFilter("EQL_id", "1"));param.add(new PropertyFilter("EQS_personList.name", "小张"));Specification<Group> spec = DynamicSpecifications.byPropertyFilter(param, Group.class);List<Group> resultList = groupTestDAO.findAll(spec);for (Group p : resultList) {System.out.println(p);}}
2023-07-08 07:21:311