创建实体类和数据库建立映射关系
package com.xbb.mybatisplus.tutorial.entity;
public class User {
private Long id;
private String name;
private Integer age;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", email=" + email + "]";
}
}
建立Mapper接口用于访问数据库
package com.xbb.mybatisplus.tutorial.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xbb.mybatisplus.tutorial.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
在src/test/java下创建com.xbb.mybatisplus.tutorial.MybatisPlusTest
package com.xbb.mybatisplus.tutorial;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbb.mybatisplus.tutorial.entity.User;
import com.xbb.mybatisplus.tutorial.mapper.UserMapper;
@SpringBootTest
class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
/**
* 保存
*/
@Test
public void save() {
User user = new User();
user.setId(6l);
user.setAge(18);
user.setEmail("zhubajie@163.com");
user.setName("zhubajie");
userMapper.insert(user);
}
/**
* 全表字段更新
*/
@Test
public void update() {
User user = new User();
user.setId(6l);
user.setAge(19);
user.setEmail("sunwukong@163.com");
user.setName("sunwukong");
userMapper.updateById(user);
}
/**
* 只更新特定字段
*/
@Test
public void updateSelected() {
User user = new User();
user.setId(6l);
user.setAge(25);
userMapper.updateById(user);
}
/**
* 删除
*/
@Test
public void delete() {
userMapper.deleteById(6l);
}
/**
* 查询
*/
@Test
public void selectList() {
QueryWrapper<User> qw = new QueryWrapper<>();
List<User> selectList = userMapper.selectList(qw);
selectList.forEach(user->{
System.out.println(user);
});
}
/**
* 等值条件查询
*/
@Test
public void listByEq() {
QueryWrapper<User> qw = new QueryWrapper<>();
qw.eq("name", "Jone");
List<User> selectList = userMapper.selectList(qw);
selectList.forEach(user->{
System.out.println(user);
});
}
/**
* 通过id查找某一条记录
*/
@Test
public void listById() {
User user = userMapper.selectById(3l);
System.out.println(user);
}
/**
* 分页查询
*/
@Test
public void page() {
QueryWrapper<User> qw = new QueryWrapper<>();
Page<User> page = new Page<>();
page.setSize(2l);
page.setCurrent(1l);
Page<User> pageResult = userMapper.selectPage(page, qw);
System.out.println("总页数:" + pageResult.getPages());
System.out.println("每页显示的最大记录数:"+pageResult.getSize());
System.out.println("当前页码:" + pageResult.getCurrent());
System.out.println("符合条件的总记录数:" + pageResult.getTotal());
pageResult.getRecords().forEach(user -> {
System.out.println(user);
});
}
}