Table of contents
Query multiple results based on conditions
Query a single result based on a condition
Introduction to Mybatis-plus
Mybatis-plus can be said to be an enhanced version of Mybatis, which has the following enhancements:
1. Compared with Mybatis, it can generate code for service, serviceImpl, and controller layers, reducing the workload of code.
2. The service layer implements the general CURD (addition, deletion, modification and query function), which reduces the amount of code for users and avoids a large number of repeated codes.
Dependent environment integration
Precautions:
1. The versions of mybatis-plus and mybatis-plus-generator must be the same.
2. The versions of mybatis-plus and springboot should correspond best. You can search for mybatis-plus-boot-starter on https://mvnrepository.com/ , and then click on the corresponding version to view the corresponding springboot version.
Import dependencies:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
configuration file
mybatis-plus:
mapper-locations:
# xml文件的路径
- classpath*:mapper/*.xml
# 实体类的包路径
type-aliases-package: com.cw.bean
startup class
@MapperScan({"The package name where the mapper interface is located"}), there can be more than one.
Mybatis-plus usage syntax
insert operation
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestUserMapper {
@Autowired
private UserMapper userMapper;
@Test
public void testInsert(){
User user=new User();
user.setAge(12);
user.setName("曹操");
user.setPassword("123");
user.setMail("[email protected]");
user.setUserName("曹操");
user.setAddress("北京");
//result数据库受影响的行数
int result = userMapper.insert(user);
System.out.println("result=>"+result);
//获取自增长后的id值
System.out.println(user.getId());//自增后的id会回填到对象中
}
}
delete operation
delete by id
@Test
public void testDeleteById() {
//执行删除操作
int result = this.userMapper.deleteById(6L);
System.out.println("result = " + result);
}
delete by field
@Test
public void testDeleteByMap() {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("age",20); columnMap.put("name","张三");
//将columnMap中的元素设置为删除的条件,多个之间为and关系
int result = this.userMapper.deleteByMap(columnMap);
System.out.println("result = " + result);
}
delete by object
@Test public void testDeleteByMap() {
User user = new User();
user.setAge(20);
user.setName("张三");
//将实体对象进行包装,包装为操作条件
QueryWrapper<User> wrapper = new QueryWrapper<>(user);
int result = this.userMapper.delete(wrapper);
System.out.println("result = " + result);
}
update operation
Update according to id
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired private UserMapper userMapper;
@Test
public void testUpdateById() {
User user = new User(); user.setId(6L); //主键
user.setAge(21); //更新的字段
//根据id更新,更新不为null的字段
this.userMapper.updateById(user);
}
}
update by condition
@Test public void testUpdate() {
User user = new User(); user.setAge(22); //更新的字段
//更新的条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("id", 6);
//执行更新操作
int result = this.userMapper.update(user, wrapper);
System.out.println("result = " + result);
}
query operation
query by id
@Test
public void testSelectById() {
//根据id查询数据
User user = this.userService.getById(2L);
System.out.println("result = " + user);
}
batch id query
@Test
public void testSelectBatchIds() {
//根据id集合批量查询
List<User> users = this.userService.listByIds(Arrays.asList(2L, 3L, 10L));
for (User user : users) {
System.out.println(user);
}
}
Query multiple results based on conditions
@Test public void testList() {
QueryWrapper<User> wrapper = new QueryWrapper<User>();
wrapper.eq("name", "李四");
List<User> user = this.userMapper.list(wrapper);
}
Query a single result based on a condition
@Test public void testSelectOne() {
QueryWrapper<User> wrapper = new QueryWrapper<User>();
wrapper.eq("name", "李四");
User user = this.userService.getOne(wrapper);
}