MyBatis Plus-4-Conditional Constructor

Conditional constructor

1.1 EntityWrapper

  • Mybatis-Plus constructs query conditions through Entity Wrapper (EW, a query condition constructor encapsulated by MP) or Condition (similar to EW), which is simple and convenient!
  • Entity wrapper, mainly used to deal with sql splicing, sorting, entity parameter query, etc.
  • Entity Wrapper inherits Wrapper

Note: Use database fields, not Java attributes!

2.1 EW

2.1.1 query

public class TestMPEW01 {

    private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");

    private EmployeeMapper employeeMapper = ioc.getBean("employeeMapper", EmployeeMapper.class);

    /**
     * Conditional constructor query operation
     */
    @Test
    public void testEntityWrapperSelect() {
        //We need to paging to query all users in the tbl_employee table with the age of 18~50 and the gender is male and the name is Tom.

        List<Employee> emps = employeeMapper.selectPage(new Page<Employee>(1, 2),
                new EntityWrapper<Employee>()
                        .between("age", 18, 50)
                        .eq("gender", 1)
                        .eq("last_name", "Tom")
        );
        System.out.println(emps);


        // In the tbl_employee table, the gender is female and the name is "teacher" or "a" in the mailbox.

        List<Employee> emps3 = employeeMapper.selectList(new EntityWrapper<Employee>()
                .eq("gender", 0)
                .like("last_name", "Teacher")
                //.or()    // SQL: (gender = ? AND last_name LIKE ? OR email LIKE ?)
                .orNew()   // SQL: (gender = ? AND last_name LIKE ?) OR (email LIKE ?)
                .like("email", "a")
        );
        System.out.println(emps3);


        // Query gender for women, sort according to age (asc/desc), simple paging.

        List<Employee> emps4 = employeeMapper.selectList(new EntityWrapper<Employee>()
                .eq("gender", 0)
                .orderBy("age")
                //.orderDesc(Arrays.asList(new String [] {"age"}))
                .last("desc limit 1,3")
        );
        System.out.println(emps4);
    }
}

2.1.2 change

    @Test
    public void testEntityWrapperUpdate() {

        Employee employee = new Employee();
        employee.setLastName("Miss Zhang");
        employee.setEmail("cls@sina.com");
        employee.setGender(0);

        employeeMapper.update(employee, new EntityWrapper<Employee>()
                .eq("last_name", "Tom")
                .eq("age", 44)
        );
    }

2.1.3 delete

    @Test
    public void testEntityWrapperDelete() {

        employeeMapper.delete(new EntityWrapper<Employee>()
                .eq("last_name", "Tom")
                .eq("age", 45)
        );
    }

2.1.4 Condition

    List<Employee> emps2 = employeeMapper.selectPage(new Page<Employee>(1, 2),
            Condition.create()
                    .between("age", 18, 50)
                    .eq("gender", "1")
                    .eq("last_name", "Tom")

    );
    System.out.println(emps2);

3.1 Description of Conditional Parameters
Query mode | description ---|--- setSqlSelect | Sets the SELECT query field WHERE | WHERE statement, splicing + WHERE condition AND | AND statement, splicing + AND field = value andNew | AND statement, stitching + AND (field = value) OR | OR statement, splicing + OR field = value orNew | OR statement, stitching + OR (field = value) eq | equals= allEq | map-based content equals= ne | does not equal <> gt | greater than > ge | greater than or equal to >= lt | less than< le | less than or equal to<= LIKE | Fuzzy Query LIKE notLike | Fuzzy Query NOT LIKE IN | IN Query notIn | NOT IN query isNull | NULL value query isNotNull | IS NOT NULL groupBy | groupBy Have | HAVING keywords Order By | Sort ORDER BY orderAsc | ASC Sort ORDER BY Order Desc | DESC Sort ORDER BY EXISTS | EXISTS conditional statement notExists | NOT EXISTS conditional statement BETWEEN | BETWEEN conditional statement NOT Between | NOT BETWEEN conditional statement addFilter | Splicing SQL Freely Last | Stitching at the end, for example: last("LIMIT 1")

4.1 summary

    Integer selectCount(@Param("ew") Wrapper<T> var1);

    List<T> selectList(@Param("ew") Wrapper<T> var1);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1);

    List<Object> selectObjs(@Param("ew") Wrapper<T> var1);

    List<T> selectPage(RowBounds var1, @Param("ew") Wrapper<T> var2);

    List<Map<String, Object>> selectMapsPage(RowBounds var1, @Param("ew") Wrapper<T> var2);
    Integer update(@Param("et") T var1, @Param("ew") Wrapper<T> var2);

    Integer updateForSet(@Param("setStr") String var1, @Param("ew") Wrapper<T> var2);
    Integer delete(@Param("ew") Wrapper<T> var1);

Tags: Programming SQL less Mybatis Database

Posted on Sat, 05 Oct 2019 21:59:19 -0700 by Hokus