com.baomidou mybatis-plus-boot-starter 3.5.1
@TableName("leave_request") @Data @AllArgsConstructor @NoArgsConstructor public class LeaveRequest { @TableId private int id; private String requestUuid; private String title; private Integer leaveDay; private LocalDate leaveStartTime; private LocalDate leaveEndTime; private String requestorName; private String teamLeader; private String sourcingManager; private String DbsBoss; private String NCSTeamLeader; private String status; }
需要注意的是@TableName("leave_request"),这个东西对应你数据库里面的表
@SpringBootApplication(exclude= SecurityAutoConfiguration.class) @MapperScan("com.example.demo.Mappers") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@MapperScan("com.example.demo.Mappers")里面的value对应你第四步的文件路径
@Repository public interface LeaveRequestMapper extends BaseMapper{ LeaveRequest findByRequestUuid(String requestUuid); }
可以通过命名的方式不用写sql语句即可实现查询。下面是基本的查询方式:
/**
* 普通查询
*/
@Test
public void selectById() {
UserInfo userInfo = userInfoMapper.selectById(123);
System.out.println(userInfo);
}
/**
* 批量查询
*/
@Test
public void selectByIds() {
Listids = Arrays.asList(123L,124L,125L);
ListuserInfo = userInfoMapper.selectBatchIds(ids);
System.out.println(userInfo);
}
/**
* 名字包含娟并且年龄小于30
*/
@Test
public void selectByWrapper() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.like("username","娟").lt("age",30);
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 名字包含娟并且年龄大于等于20且小于等于40并且email不为空
*/
@Test
public void selectByWrapper2() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.like("username","娟").between("age",20,30).isNotNull("email");
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 名字姓肖或者年量大于等于20,按照年龄降序排列,年龄相同按照id生序排列
*/
@Test
public void selectByWrapper3() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.likeRight("username","肖")
.or().ge("age",20).orderByDesc("age").orderByAsc("id");
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 创建日期为2024年1月2日并且直属上级名字为王姓
*/
@Test
public void selectByWrapper4() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2024-10-02")
.inSql("parent_id","select id from user where username like '王%'");
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
//上面的日期查询使用的是占位符的形式进行查询,目的就是为了防止SQL注入的风险
/**
* 名字为王姓并且(年龄小于40或邮箱不为空)
*/
@Test
public void selectByWrapper5() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.likeRight("username","王")
.and(wq->wq.lt("age",40))
.or().isNotNull("email");
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 名字为王姓并且(年龄小于40并且大与20或邮箱不为空)
*/
@Test
public void selectByWrapper6() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.likeRight("username","王")
.and(wq->wq.lt("age",40).gt("age",20))
.or().isNotNull("email");
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* (年龄小于40并且大与20或邮箱不为空)并且名字为王姓
*/
@Test
public void selectByWrapper7() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.nested(wq->wq.lt("age",40).gt("age",20))
.or().isNotNull("email")
.likeRight("username","王");
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 年龄23,30,40
*/
@Test
public void selectByWrapper8() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.in("age",Arrays.asList(20,30,40));
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 只返回满足条件的其中一条语句即可
*/
@Test
public void selectByWrapper9() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.in("age",Arrays.asList(20,30,40)).last("limit 1");
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 名字中包含雨并且年龄小于40(只取id,username)
*/
@Test
public void selectByWrapper10() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.select("id","username").like("username","雨").lt("age",40);
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 名字中包含雨并且年龄小于40(不取create_time,parent_id两个字段,即不列出全部字段)
*/
@Test
public void selectByWrapper11() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.like("username","雨").lt("age",40)
.select(UserInfo.class,info->!info.getColumn().equals("create_time")&&
!info.getColumn().equals("parent_id"));
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 姓名和邮箱不为空
*/
public void testCondition() {
String username = "王";
String email = "";
condition(username,email);
}
private void condition(String username,String email){
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.like(StringUtils.isNullOrEmpty(username),"name",username)
.like(StringUtils.isNullOrEmpty(email),"email",email);
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* 实体作为条件构造器方法的参数
*/
@Test
public void selectByWrapperEntity() {
UserInfo whereUser = new UserInfo();
whereUser.setUsername("xiaojuan");
whereUser.setAge(22);
QueryWrapperqueryWrapper = new QueryWrapper (whereUser);
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* AllEq用法
*/
@Test
public void selectByWrapperAllEq() {
QueryWrapperqueryWrapper = new QueryWrapper ();
Mapparams = new HashMap ();
params.put("nuserame","xiaojuan");
params.put("age",null);
queryWrapper.allEq(params);
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* AllEq用法(排除不是条件的字段)
*/
@Test
public void selectByWrapperAllEq2() {
QueryWrapperqueryWrapper = new QueryWrapper ();
Mapparams = new HashMap ();
params.put("nuserame","xiaojuan");
params.put("age",null);
queryWrapper.allEq((k,v)->!k.equals("name"),params);
ListuserInfoList = userInfoMapper.selectList(queryWrapper);
userInfoList.forEach(System.out::println);
}
/**
* selectMaps
*/
@Test
public void selectByWrapperMaps() {
QueryWrapperqueryWrapper = new QueryWrapper ();
queryWrapper.like("name","肖").lt("age",40);
List