Spring Data JAP multiple query condition processing not required

Source: Internet
Author: User

A simple introduction to the use of the scene, the DAO layer with Spring Data implementation, DAO only interface, implementation class is the container startup dynamic bytecode generation, interface definition method, method @query write JPQL query statement.

Based on the above limitations, if you make a query to an entity, there are multiple conditions, and each condition is not required. Parameters must be passed, null or empty string must not be, this afternoon, the project team was confronted with this problem.

I've done it before, but the code didn't find it, I tried it again, and then I lost the code to pg.

This time the code is put out, save to lose again, the code is as follows:
DAO interface

?
123456 public interface ActivityDao extends JpaRepository<Activity, Integer> {    @Query("SELECT a FROM Activity a WHERE (a.code=:code OR :code = null) AND (a.name=:name OR :name = null)")    public List<Activity> findByCodeAndName(@Param("code") String code, @Param("name") String name);}

Junit

?
1234567891011121314 @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:META-INF\\appContext.xml")public class ActivityDaoTest extends AbstractJUnit4SpringContextTests {    @Resource    private ActivityDao dao;    @Test    public void test() {        // List<Activity> list = dao.findByCodeAndName("33", "3");        List<Activity> list = dao.findByCodeAndName("33", null);        System.out.println(list.size());    }}

Spring Data JAP multiple query condition processing not required

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.