依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.11</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency>
application.yml
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mpdb?serverTimezone=UTC username: root password: 1234 mybatis-plus: configuration: # 开启mp的日志(输出到控制台) log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: #全局设置实体类主键设置方式 id-type: assign_id #全局设置表名 table-prefix: tb_
分页
package com.itheima.mp.config; @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor interceptor(){ //mybatisplus的拦截器 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //增加分页拦截器 interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } }
@SpringBootTest class UserMapperTest { @Autowired private UserMapper userMapper; @Test public void pageTest(){ IPage<User> ipage = new Page<>(2, 3); userMapper.selectPage(ipage,null); System.out.println(ipage); } } package com.itheima.mp.mapper; @Mapper public interface UserMapper extends BaseMapper<User> { } @Builder @Data @AllArgsConstructor @NoArgsConstructor //@TableName("tb_user") public class User { //@TableId(type= IdType.AUTO) @TableId(type = IdType.ASSIGN_ID) private Long id; private String name; private String password; private Integer age; private String tel; @TableField(fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; @TableField(fill = FieldFill.INSERT) private LocalDateTime createTime; }
测试一
@SpringBootTest
class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void pageTest(){
IPage<User> ipage = new Page<>(2, 3);
userMapper.selectPage(ipage,null);
System.out.println(ipage);
}
@Test
public void testSelect(){
User user = userMapper.selectById(1);
System.out.println(user);
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 5, 10));
users.forEach(System.out::println);
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName,"jack");
user = userMapper.selectOne(lambdaQueryWrapper);
System.out.println(user);
Integer count = userMapper.selectCount(lambdaQueryWrapper);
System.out.println(count);
users = userMapper.selectList(lambdaQueryWrapper);
System.out.println(users);
}
@Test
public void testUpdate(){
//User user = User.builder()
// .age(20)
// .id(7l)
// .name("update")
// .build();
//userMapper.updateById(user);
User user = User.builder()
//.age(20)
//.id(7l)
//.name("UPDATE")
.build();
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.set("tel","16880206014");
wrapper.eq("name","update");
int row = userMapper.update(user, wrapper);
System.out.println(row);
}
@Test
public void testDelete(){
int row = userMapper.deleteById(12);
System.out.println(row);
row = userMapper.deleteBatchIds(Arrays.asList(15,16));
System.out.println(row);
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("name","test");
row = userMapper.delete(wrapper);
System.out.println(row);
}
@Test
public void testInsert(){
for (int i = 0; i < 20; i++) {
User user = User.builder()
.age(20)
.name("test"+i)
.password("1234")
.build();
int row = userMapper.insert(user);
System.out.println("row:"+row);
System.out.println(user.getId());
}
}
@Test
public void testFind(){
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
测试二
@SpringBootTest
public class DQLTest {
@Autowired
private UserMapper userMapper;
@Test
public void groupByAndSortTest(){
//LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//lambdaQueryWrapper.select(User::getPassword); //select password
//lambdaQueryWrapper.groupBy(User::getPassword);//group by password
//List<User> users = userMapper.selectList(lambdaQueryWrapper);
//users.forEach(System.out::println);
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
String name = null;
lambdaQueryWrapper.orderBy(name!=null,true,User::getAge);
lambdaQueryWrapper.orderByDesc(User::getAge,User::getName);
List<User> users = userMapper.selectList(lambdaQueryWrapper);
users.forEach(System.out::println);
}
@Test
public void orTest() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
String name = "Test";
Integer age = 20;
lambdaQueryWrapper.between(age != null, User::getAge, 10, 100)
.like(name != null, User::getName, name)
.or()
.like(User::getPassword, "123");
//select 全字段 from user where age between 10 and 20 and name like '%Test%' or password like '%123%';
List<User> users = userMapper.selectList(lambdaQueryWrapper);
users.forEach(System.out::println);
}
@Test
public void andTest() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.ge(User::getAge, 10);//where age >= 10
lambdaQueryWrapper.lt(User::getAge, 100);//age < 100
//链式编程
lambdaQueryWrapper.like(User::getName, "Test")//name like '%Test%'
.likeRight(User::getTel, "13");//tel like '13%'
List<User> users = userMapper.selectList(lambdaQueryWrapper);
users.forEach(System.out::println);
}
}