public interface EntityRepository<T, ID extends Serializable> {
.
.
public List<T> findAll(Object query);
public List<T> findAll(Object query, Sort sort);
public Page<T> findAll(Object query, Pageable pageable);
public long count(Object query);
}
package example.u2ware.springfield.part1;
import lombok.Getter;
import lombok.Setter;
public class FindByIdAndPasswordAndNameAndAddress {
public @Getter @Setter Integer id;
public @Getter @Setter String password;
public @Getter @Setter String address;
public @Getter @Setter String name;
}
package example.u2ware.springfield.part1;
import com.u2ware.springfield.repository.QueryMethod;
import lombok.Getter;
import lombok.Setter;
@QueryMethod("findByIdAndPasswordAndNameAndAddress")
public class SampleQuery {
public @Getter @Setter Integer id;
public @Getter @Setter String password;
public @Getter @Setter String address;
public @Getter @Setter String name;
}
SQLSESSION 전략에서 QueryMethod는 sqlsession.xml 에 미리정의된 mapping query statement 를 의미한다.
JPA 전략에서 QueryMethod는 "findBy~" 로 시작하는 spring-data JPA Query Method 을 따른다.
MONGODB 전략에서 QueryMethod는 "findBy~" 로 시작하는 spring-data mongodb Query Method 을 따른다.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations="../../application-context.xml")
public class JpaBeanRepositoryTest {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired @Qualifier("mybatisBeanRepository")
private EntityRepository<MyBatisBean,Integer> mybatisBeanRepository;
@Autowired @Qualifier("jpaBeanRepository")
private EntityRepository<JpaBean,Integer> jpaBeanRepository;
@Autowired @Qualifier("mongodbBeanRepository")
private EntityRepository<MongodbBean,Integer> mongodbBeanRepository;
public void testWhereByQueryObject1() throws Exception{
FindByIdAndPasswordAndNameAndAddress query = new FindByIdAndPasswordAndNameAndAddress();
query.setPassword("pwd7");
List<?> result = xxxxxxxxRepository.findAll(query);
Assert.assertEquals(1, result.size());
}
public void testWhereByQueryObject2() throws Exception{
FindByIdAndPasswordAndNameAndAddress query = new FindByIdAndPasswordAndNameAndAddress();
List<?> result = xxxxxxxxRepository.findAll(query);
Assert.assertEquals(9, result.size());
}
public void testWhereByQueryObject3() throws Exception{
MyQuery query = new MyQuery();
query.setAddress("addr-9");
List<?> result = xxxxxxxxRepository.findAll(query);
Assert.assertEquals(1, result.size());
}
public void testWhereByQueryObject4() throws Exception{
MyQuery query = new MyQuery();
List<?> result = xxxxxxxxRepository.findAll(query);
Assert.assertEquals(9, result.size());
}
}