1. 程式人生 > 其它 >MyBatis:條件構造器QueryWrapper方法詳解 MyBatis:條件構造器QueryWrapper方法詳解 

MyBatis:條件構造器QueryWrapper方法詳解 MyBatis:條件構造器QueryWrapper方法詳解 

MyBatis:條件構造器QueryWrapper方法詳解 

目錄

 

正文

QueryWrapper

說明:
      繼承自 AbstractWrapper ,自身的內部屬性 entity 也用於生成 where 條件
及 LambdaQueryWrapper, 可以通過 new QueryWrapper().lambda() 方法獲取.

測試用表:

回到頂部

1. eq、ne

說明:

eq:等於,ne:不等於

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //eq() 等於
        wrapper.eq("last_name", "皮皮蝦");
        Employee one = employeeService.getOne(wrapper);
        System.out.println(one);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //ne() 不等於
        wrapper.ne("gender", 0);
        List<Employee> list = employeeService.list(wrapper);
        System.out.println(list);
}

回到頂部

2. gt、ge、lt、le

說明:

gt:大於,ge:大於等於,lt:小於,le:小於等於

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> gtWrapper = new QueryWrapper<>();
        //gt() 大於
        gtWrapper.gt("age", 27);
        List<Employee> gtList = employeeService.list(gtWrapper);
        System.out.println(gtList);

        //**********************

        QueryWrapper<Employee> geWrapper = new QueryWrapper<>();
        //ge() 大於等於
        geWrapper.ge("age", 26);
        List<Employee> geList = employeeService.list(geWrapper);
        System.out.println(geList);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> ltWrapper = new QueryWrapper<>();
        //lt() 小於
        ltWrapper.lt("age", 12);
        List<Employee> ltList = employeeService.list(ltWrapper);
        System.out.println(ltList);

        //**********************

        QueryWrapper<Employee> geWrapper = new QueryWrapper<>();
        //le() 小於等於
        leWrapper.le("age", 12);
        List<Employee> leList = employeeService.list(leWrapper);
        System.out.println(leList);
}

回到頂部

3. between、notBetween

說明:

between:在值1和值2之間,notBetween:不在值1和值2之間

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> betweenWrapper = new QueryWrapper<>();
        //between() 區間的值
        betweenWrapper.between("age", 10, 20);
        List<Employee> betweenList = employeeService.list(betweenWrapper);
        System.out.println(betweenList );

        //**********************

        QueryWrapper<Employee> notBetweenWrapper = new QueryWrapper<>();
        //notBetween() 不在區間的值
        notBetweenWrapper.notBetween("age", 10, 21);
        List<Employee> notBetweenList = employeeService.list(notBetweenWrapper );
        System.out.println(notBetweenList );
}

回到頂部

4. like、notLike、likeLeft、likeRight

說明:

like:’%值%’,notLike:’%值%’,likeLeft:’%值’,likeRight:'值%'

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> likeWrapper = new QueryWrapper<>();
        //like() 模糊查詢匹配值
        likeWrapper.like("last_name", "s");
        List<Employee> likeList = employeeService.list(likeWrapper);
        System.out.println(likeList );

        //**********************

        QueryWrapper<Employee> notLikeWrapper = new QueryWrapper<>();
        //notLike() 模糊查詢不匹配值
        notLikeWrapper.notLike("last_name", "s");
        List<Employee> notLikeList = employeeService.list(notLikeWrapper);
        System.out.println(notLikeList);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> likeLeftWrapper = new QueryWrapper<>();
        //likeLeft() 模糊查詢匹配最後一位值
        likeLeftWrapper.likeLeft("last_name", "s");
        List<Employee> likeLeftList = employeeService.list(likeLeftWrapper);
        System.out.println(likeLeftList );

        //**********************

        QueryWrapper<Employee> likeRightWrapper = new QueryWrapper<>();
        //likeRight() 模糊查詢匹配第一位值
        likeRightWrapper.likeRight("last_name", "s");
        List<Employee> likeRightList = employeeService.list(likeRightWrapper );
        System.out.println(likeRightList );
}

回到頂部

5. isNull、isNotNull

說明:

isNull:欄位 IS NULL,isNotNull:欄位 IS NOT NULL

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> isNullWrapper = new QueryWrapper<>();
        //isNull() 為空
        isNullWrapper.isNull("email");
        List<Employee> isNullList = employeeService.list(isNullWrapper);
        System.out.println(isNullList );

        //**********************

        QueryWrapper<Employee> isNotNullWrapper = new QueryWrapper<>();
        //isNotNull() 不為空
        isNotNullWrapper.isNotNull("last_name", "s");
        List<Employee> isNotNullList = employeeService.list(isNotNullWrapper );
        System.out.println(isNotNullList );
}

回到頂部

6. in、notIn

說明:

in:欄位 IN (v0, v1, …),notIn:欄位 NOT IN (value.get(0), value.get(1), …)

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> inWrapper = new QueryWrapper<>();
        //in() 符合多個條件的值
        inWrapper.in("age", 8, 16, 26);
        List<Employee> inList = employeeService.list(inWrapper);
        System.out.println(inList );

        //**********************

        QueryWrapper<Employee> notInWrapper= new QueryWrapper<>();
        //notIn() 不符合多個條件的值
        notInWrapper.notIn("age", 8, 16, 26);
        List<Employee> notInList = employeeService.list(notInWrapper);
        System.out.println(notInList );
}

回到頂部

7. or、and

說明:

or:或者 ,and:和

注意事項:
主動呼叫or表示緊接著下一個方法不是用and連線!(不呼叫or則預設為使用and連線)

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> orWrapper = new QueryWrapper<>();
        //or() 查詢age大於20 或者 gender等於1 的資料
        orWrapper.gt("age", 20).or().eq("gender", 1);
        List<Employee> orList = employeeService.list(orWrapper);
        System.out.println(orList );
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> andWrapper = new QueryWrapper<>();
        //and() 查詢last_name等於皮皮蝦b 和 gender等於1 的資料 (不使用and() 預設就是and())
        andWrapper.eq("last_name", "皮皮蝦b").eq("gender", 1);
        List<Employee> andList = employeeService.list(andWrapper );
        System.out.println(andList);
}

回到頂部

8. orderByAsc、orderByDesc

說明:

orderByAsc:升序:ORDER BY 欄位, … ASC,orderByDesc:降序:ORDER BY 欄位, … DESC

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> Wrapper = new QueryWrapper<>();
        //orderByAsc() 升序排序
        Wrapper.orderByAsc("id");
        List<Employee> list = employeeService.list(Wrapper);
        for(Employee e : list) {
            System.out.println(e);
        }
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> Wrapper = new QueryWrapper<>();
        //orderByDesc() 降序排序
        Wrapper.orderByDesc("id");
        List<Employee> list = employeeService.list(Wrapper);
        for(Employee e : list) {
            System.out.println(e);
        }
}

回到頂部

9. inSql、notInSql (不常用)

說明:

inSql:欄位 IN ( sql語句 ),notInSql:欄位 NOT IN ( sql語句 )

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> inSqlWrapper = new QueryWrapper<>();
        //inSql() 符合sql語句的值
        inSqlWrapper .inSql("select id from employee where id < 10");
        List<Employee> inSqlList = employeeService.list(inSqlWrapper );
        System.out.println(inSqlList );

        //**********************

        QueryWrapper<Employee> notInSqlWrapper= new QueryWrapper<>();
        //notInSql() 不符合sql語句的值
        notInSqlWrapper.notInSql("select id from employee where id < 10");
        List<Employee> notInSqlList = employeeService.list(notInSqlWrapper);
        System.out.println(notInSqlList );
}

回到頂部

10. exists、notExists (不常用)

說明:

exists:拼接 EXISTS ( sql語句 ),notExists:拼接 NOT EXISTS ( sql語句 )

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> existsWrapper = new QueryWrapper<>();
        //exists() 查詢符合sql語句的值
        existsWrapper.exists("select last_name,gender from employee where id = 1");
        List<Employee> existsList = employeeService.list(existsWrapper);
        System.out.println(existsList );

        //**********************

        QueryWrapper<Employee> notExistsWrapper = new QueryWrapper<>();
        //notExists() 查詢不符合sql語句的值
        notExistsWrapper.notExists("select last_name,gender from employee where id = 1");
        List<Employee> notExistsList = employeeService.list(notExistsWrapper);
        System.out.println(notExistsList );

}

 

目錄

 

正文

QueryWrapper

說明:
      繼承自 AbstractWrapper ,自身的內部屬性 entity 也用於生成 where 條件
及 LambdaQueryWrapper, 可以通過 new QueryWrapper().lambda() 方法獲取.

測試用表:

回到頂部

1. eq、ne

說明:

eq:等於,ne:不等於

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //eq() 等於
        wrapper.eq("last_name", "皮皮蝦");
        Employee one = employeeService.getOne(wrapper);
        System.out.println(one);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //ne() 不等於
        wrapper.ne("gender", 0);
        List<Employee> list = employeeService.list(wrapper);
        System.out.println(list);
}

回到頂部

2. gt、ge、lt、le

說明:

gt:大於,ge:大於等於,lt:小於,le:小於等於

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> gtWrapper = new QueryWrapper<>();
        //gt() 大於
        gtWrapper.gt("age", 27);
        List<Employee> gtList = employeeService.list(gtWrapper);
        System.out.println(gtList);

        //**********************

        QueryWrapper<Employee> geWrapper = new QueryWrapper<>();
        //ge() 大於等於
        geWrapper.ge("age", 26);
        List<Employee> geList = employeeService.list(geWrapper);
        System.out.println(geList);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> ltWrapper = new QueryWrapper<>();
        //lt() 小於
        ltWrapper.lt("age", 12);
        List<Employee> ltList = employeeService.list(ltWrapper);
        System.out.println(ltList);

        //**********************

        QueryWrapper<Employee> geWrapper = new QueryWrapper<>();
        //le() 小於等於
        leWrapper.le("age", 12);
        List<Employee> leList = employeeService.list(leWrapper);
        System.out.println(leList);
}

回到頂部

3. between、notBetween

說明:

between:在值1和值2之間,notBetween:不在值1和值2之間

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> betweenWrapper = new QueryWrapper<>();
        //between() 區間的值
        betweenWrapper.between("age", 10, 20);
        List<Employee> betweenList = employeeService.list(betweenWrapper);
        System.out.println(betweenList );

        //**********************

        QueryWrapper<Employee> notBetweenWrapper = new QueryWrapper<>();
        //notBetween() 不在區間的值
        notBetweenWrapper.notBetween("age", 10, 21);
        List<Employee> notBetweenList = employeeService.list(notBetweenWrapper );
        System.out.println(notBetweenList );
}

回到頂部

4. like、notLike、likeLeft、likeRight

說明:

like:’%值%’,notLike:’%值%’,likeLeft:’%值’,likeRight:'值%'

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> likeWrapper = new QueryWrapper<>();
        //like() 模糊查詢匹配值
        likeWrapper.like("last_name", "s");
        List<Employee> likeList = employeeService.list(likeWrapper);
        System.out.println(likeList );

        //**********************

        QueryWrapper<Employee> notLikeWrapper = new QueryWrapper<>();
        //notLike() 模糊查詢不匹配值
        notLikeWrapper.notLike("last_name", "s");
        List<Employee> notLikeList = employeeService.list(notLikeWrapper);
        System.out.println(notLikeList);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> likeLeftWrapper = new QueryWrapper<>();
        //likeLeft() 模糊查詢匹配最後一位值
        likeLeftWrapper.likeLeft("last_name", "s");
        List<Employee> likeLeftList = employeeService.list(likeLeftWrapper);
        System.out.println(likeLeftList );

        //**********************

        QueryWrapper<Employee> likeRightWrapper = new QueryWrapper<>();
        //likeRight() 模糊查詢匹配第一位值
        likeRightWrapper.likeRight("last_name", "s");
        List<Employee> likeRightList = employeeService.list(likeRightWrapper );
        System.out.println(likeRightList );
}

回到頂部

5. isNull、isNotNull

說明:

isNull:欄位 IS NULL,isNotNull:欄位 IS NOT NULL

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> isNullWrapper = new QueryWrapper<>();
        //isNull() 為空
        isNullWrapper.isNull("email");
        List<Employee> isNullList = employeeService.list(isNullWrapper);
        System.out.println(isNullList );

        //**********************

        QueryWrapper<Employee> isNotNullWrapper = new QueryWrapper<>();
        //isNotNull() 不為空
        isNotNullWrapper.isNotNull("last_name", "s");
        List<Employee> isNotNullList = employeeService.list(isNotNullWrapper );
        System.out.println(isNotNullList );
}

回到頂部

6. in、notIn

說明:

in:欄位 IN (v0, v1, …),notIn:欄位 NOT IN (value.get(0), value.get(1), …)

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> inWrapper = new QueryWrapper<>();
        //in() 符合多個條件的值
        inWrapper.in("age", 8, 16, 26);
        List<Employee> inList = employeeService.list(inWrapper);
        System.out.println(inList );

        //**********************

        QueryWrapper<Employee> notInWrapper= new QueryWrapper<>();
        //notIn() 不符合多個條件的值
        notInWrapper.notIn("age", 8, 16, 26);
        List<Employee> notInList = employeeService.list(notInWrapper);
        System.out.println(notInList );
}

回到頂部

7. or、and

說明:

or:或者 ,and:和

注意事項:
主動呼叫or表示緊接著下一個方法不是用and連線!(不呼叫or則預設為使用and連線)

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> orWrapper = new QueryWrapper<>();
        //or() 查詢age大於20 或者 gender等於1 的資料
        orWrapper.gt("age", 20).or().eq("gender", 1);
        List<Employee> orList = employeeService.list(orWrapper);
        System.out.println(orList );
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> andWrapper = new QueryWrapper<>();
        //and() 查詢last_name等於皮皮蝦b 和 gender等於1 的資料 (不使用and() 預設就是and())
        andWrapper.eq("last_name", "皮皮蝦b").eq("gender", 1);
        List<Employee> andList = employeeService.list(andWrapper );
        System.out.println(andList);
}

回到頂部

8. orderByAsc、orderByDesc

說明:

orderByAsc:升序:ORDER BY 欄位, … ASC,orderByDesc:降序:ORDER BY 欄位, … DESC

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> Wrapper = new QueryWrapper<>();
        //orderByAsc() 升序排序
        Wrapper.orderByAsc("id");
        List<Employee> list = employeeService.list(Wrapper);
        for(Employee e : list) {
            System.out.println(e);
        }
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> Wrapper = new QueryWrapper<>();
        //orderByDesc() 降序排序
        Wrapper.orderByDesc("id");
        List<Employee> list = employeeService.list(Wrapper);
        for(Employee e : list) {
            System.out.println(e);
        }
}

回到頂部

9. inSql、notInSql (不常用)

說明:

inSql:欄位 IN ( sql語句 ),notInSql:欄位 NOT IN ( sql語句 )

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> inSqlWrapper = new QueryWrapper<>();
        //inSql() 符合sql語句的值
        inSqlWrapper .inSql("select id from employee where id < 10");
        List<Employee> inSqlList = employeeService.list(inSqlWrapper );
        System.out.println(inSqlList );

        //**********************

        QueryWrapper<Employee> notInSqlWrapper= new QueryWrapper<>();
        //notInSql() 不符合sql語句的值
        notInSqlWrapper.notInSql("select id from employee where id < 10");
        List<Employee> notInSqlList = employeeService.list(notInSqlWrapper);
        System.out.println(notInSqlList );
}

回到頂部

10. exists、notExists (不常用)

說明:

exists:拼接 EXISTS ( sql語句 ),notExists:拼接 NOT EXISTS ( sql語句 )

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> existsWrapper = new QueryWrapper<>();
        //exists() 查詢符合sql語句的值
        existsWrapper.exists("select last_name,gender from employee where id = 1");
        List<Employee> existsList = employeeService.list(existsWrapper);
        System.out.println(existsList );

        //**********************

        QueryWrapper<Employee> notExistsWrapper = new QueryWrapper<>();
        //notExists() 查詢不符合sql語句的值
        notExistsWrapper.notExists("select last_name,gender from employee where id = 1");
        List<Employee> notExistsList = employeeService.list(notExistsWrapper);
        System.out.println(notExistsList );

}

 

目錄

 

正文

QueryWrapper

說明:
      繼承自 AbstractWrapper ,自身的內部屬性 entity 也用於生成 where 條件
及 LambdaQueryWrapper, 可以通過 new QueryWrapper().lambda() 方法獲取.

測試用表:

回到頂部

1. eq、ne

說明:

eq:等於,ne:不等於

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //eq() 等於
        wrapper.eq("last_name", "皮皮蝦");
        Employee one = employeeService.getOne(wrapper);
        System.out.println(one);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        //ne() 不等於
        wrapper.ne("gender", 0);
        List<Employee> list = employeeService.list(wrapper);
        System.out.println(list);
}

回到頂部

2. gt、ge、lt、le

說明:

gt:大於,ge:大於等於,lt:小於,le:小於等於

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> gtWrapper = new QueryWrapper<>();
        //gt() 大於
        gtWrapper.gt("age", 27);
        List<Employee> gtList = employeeService.list(gtWrapper);
        System.out.println(gtList);

        //**********************

        QueryWrapper<Employee> geWrapper = new QueryWrapper<>();
        //ge() 大於等於
        geWrapper.ge("age", 26);
        List<Employee> geList = employeeService.list(geWrapper);
        System.out.println(geList);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> ltWrapper = new QueryWrapper<>();
        //lt() 小於
        ltWrapper.lt("age", 12);
        List<Employee> ltList = employeeService.list(ltWrapper);
        System.out.println(ltList);

        //**********************

        QueryWrapper<Employee> geWrapper = new QueryWrapper<>();
        //le() 小於等於
        leWrapper.le("age", 12);
        List<Employee> leList = employeeService.list(leWrapper);
        System.out.println(leList);
}

回到頂部

3. between、notBetween

說明:

between:在值1和值2之間,notBetween:不在值1和值2之間

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> betweenWrapper = new QueryWrapper<>();
        //between() 區間的值
        betweenWrapper.between("age", 10, 20);
        List<Employee> betweenList = employeeService.list(betweenWrapper);
        System.out.println(betweenList );

        //**********************

        QueryWrapper<Employee> notBetweenWrapper = new QueryWrapper<>();
        //notBetween() 不在區間的值
        notBetweenWrapper.notBetween("age", 10, 21);
        List<Employee> notBetweenList = employeeService.list(notBetweenWrapper );
        System.out.println(notBetweenList );
}

回到頂部

4. like、notLike、likeLeft、likeRight

說明:

like:’%值%’,notLike:’%值%’,likeLeft:’%值’,likeRight:'值%'

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> likeWrapper = new QueryWrapper<>();
        //like() 模糊查詢匹配值
        likeWrapper.like("last_name", "s");
        List<Employee> likeList = employeeService.list(likeWrapper);
        System.out.println(likeList );

        //**********************

        QueryWrapper<Employee> notLikeWrapper = new QueryWrapper<>();
        //notLike() 模糊查詢不匹配值
        notLikeWrapper.notLike("last_name", "s");
        List<Employee> notLikeList = employeeService.list(notLikeWrapper);
        System.out.println(notLikeList);
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> likeLeftWrapper = new QueryWrapper<>();
        //likeLeft() 模糊查詢匹配最後一位值
        likeLeftWrapper.likeLeft("last_name", "s");
        List<Employee> likeLeftList = employeeService.list(likeLeftWrapper);
        System.out.println(likeLeftList );

        //**********************

        QueryWrapper<Employee> likeRightWrapper = new QueryWrapper<>();
        //likeRight() 模糊查詢匹配第一位值
        likeRightWrapper.likeRight("last_name", "s");
        List<Employee> likeRightList = employeeService.list(likeRightWrapper );
        System.out.println(likeRightList );
}

回到頂部

5. isNull、isNotNull

說明:

isNull:欄位 IS NULL,isNotNull:欄位 IS NOT NULL

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> isNullWrapper = new QueryWrapper<>();
        //isNull() 為空
        isNullWrapper.isNull("email");
        List<Employee> isNullList = employeeService.list(isNullWrapper);
        System.out.println(isNullList );

        //**********************

        QueryWrapper<Employee> isNotNullWrapper = new QueryWrapper<>();
        //isNotNull() 不為空
        isNotNullWrapper.isNotNull("last_name", "s");
        List<Employee> isNotNullList = employeeService.list(isNotNullWrapper );
        System.out.println(isNotNullList );
}

回到頂部

6. in、notIn

說明:

in:欄位 IN (v0, v1, …),notIn:欄位 NOT IN (value.get(0), value.get(1), …)

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> inWrapper = new QueryWrapper<>();
        //in() 符合多個條件的值
        inWrapper.in("age", 8, 16, 26);
        List<Employee> inList = employeeService.list(inWrapper);
        System.out.println(inList );

        //**********************

        QueryWrapper<Employee> notInWrapper= new QueryWrapper<>();
        //notIn() 不符合多個條件的值
        notInWrapper.notIn("age", 8, 16, 26);
        List<Employee> notInList = employeeService.list(notInWrapper);
        System.out.println(notInList );
}

回到頂部

7. or、and

說明:

or:或者 ,and:和

注意事項:
主動呼叫or表示緊接著下一個方法不是用and連線!(不呼叫or則預設為使用and連線)

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> orWrapper = new QueryWrapper<>();
        //or() 查詢age大於20 或者 gender等於1 的資料
        orWrapper.gt("age", 20).or().eq("gender", 1);
        List<Employee> orList = employeeService.list(orWrapper);
        System.out.println(orList );
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> andWrapper = new QueryWrapper<>();
        //and() 查詢last_name等於皮皮蝦b 和 gender等於1 的資料 (不使用and() 預設就是and())
        andWrapper.eq("last_name", "皮皮蝦b").eq("gender", 1);
        List<Employee> andList = employeeService.list(andWrapper );
        System.out.println(andList);
}

回到頂部

8. orderByAsc、orderByDesc

說明:

orderByAsc:升序:ORDER BY 欄位, … ASC,orderByDesc:降序:ORDER BY 欄位, … DESC

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> Wrapper = new QueryWrapper<>();
        //orderByAsc() 升序排序
        Wrapper.orderByAsc("id");
        List<Employee> list = employeeService.list(Wrapper);
        for(Employee e : list) {
            System.out.println(e);
        }
}

@Test
public void contextLoads(){
        QueryWrapper<Employee> Wrapper = new QueryWrapper<>();
        //orderByDesc() 降序排序
        Wrapper.orderByDesc("id");
        List<Employee> list = employeeService.list(Wrapper);
        for(Employee e : list) {
            System.out.println(e);
        }
}

回到頂部

9. inSql、notInSql (不常用)

說明:

inSql:欄位 IN ( sql語句 ),notInSql:欄位 NOT IN ( sql語句 )

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> inSqlWrapper = new QueryWrapper<>();
        //inSql() 符合sql語句的值
        inSqlWrapper .inSql("select id from employee where id < 10");
        List<Employee> inSqlList = employeeService.list(inSqlWrapper );
        System.out.println(inSqlList );

        //**********************

        QueryWrapper<Employee> notInSqlWrapper= new QueryWrapper<>();
        //notInSql() 不符合sql語句的值
        notInSqlWrapper.notInSql("select id from employee where id < 10");
        List<Employee> notInSqlList = employeeService.list(notInSqlWrapper);
        System.out.println(notInSqlList );
}

回到頂部

10. exists、notExists (不常用)

說明:

exists:拼接 EXISTS ( sql語句 ),notExists:拼接 NOT EXISTS ( sql語句 )

測試:

@Test
public void contextLoads(){
        QueryWrapper<Employee> existsWrapper = new QueryWrapper<>();
        //exists() 查詢符合sql語句的值
        existsWrapper.exists("select last_name,gender from employee where id = 1");
        List<Employee> existsList = employeeService.list(existsWrapper);
        System.out.println(existsList );

        //**********************

        QueryWrapper<Employee> notExistsWrapper = new QueryWrapper<>();
        //notExists() 查詢不符合sql語句的值
        notExistsWrapper.notExists("select last_name,gender from employee where id = 1");
        List<Employee> notExistsList = employeeService.list(notExistsWrapper);
        System.out.println(notExistsList );

}