MybatisPlus的简单使用
程序员文章站
2022-04-22 10:33:56
...
MybatisPlus的简单使用
引入依赖
一个简单的springboot项目
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
配置文件
spring:
application:
name: mybatisplus-demo
datasource:
url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=UTF-8&useSSL=false
username: root
password: 91597489
driverClassName: com.mysql.jdbc.Driver
logging:
level:
com.atguigu.mybatisplus.demo: debug
mybatis-plus:
global-config:
db-config:
id-type: auto #数据库id自增
数据库
使用mysql数据库
# 创建表user
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
# 添加数据
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
编码
实体类User
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
编写mapper类,UserMapper
public interface UserMapper extends BaseMapper<User> {
}
分页配置
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
测试
@Resource
private UserMapper userMapper;
@Test
void contextLoads() {
//User user = userMapper.selectById(1L);
//System.out.println(user);
//List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 4));
//users.stream().forEach(user1 -> System.out.println(user1));
QueryWrapper<User> wrapper = new QueryWrapper<>();
//wrapper.like("name","J");
wrapper.gt("age", 18);
wrapper.orderByDesc("age");
wrapper.select("id", "name", "age");
//List<User> users = userMapper.selectList(wrapper);
//users.stream().forEach(user1 -> System.out.println(user1));
IPage<User> page = userMapper.selectPage(new Page<>(2, 3), wrapper);
page.getRecords().forEach(System.out::println);
}
@Test
void insertTest() {
userMapper.insert(new User(null, "wangjie", 23, "[email protected]"));
}
更具id更新:
@Test
void updateTest() {
//会判断传入值是否为null
userMapper.updateById(new User(7L, "隔壁老王", 30, null));
}
控制台sql日志
==> Preparing: UPDATE user SET name=?, age=? WHERE id=?
==> Parameters: 隔壁老王(String), 30(Integer), 7(Long)
<== Updates: 1
根据其他条件更新:
@Test
void updateTest() {
userMapper.update(new User(null, "隔壁老徐", 40, null),
new UpdateWrapper<>(new User(null, null, null, "[email protected]")));
}
控制台sql日志
==> Preparing: UPDATE user SET name=?, age=? WHERE email=?
==> Parameters: 隔壁老徐(String), 40(Integer), [email protected](String)
<== Updates: 1
删除:
@Test
void delTest(){
userMapper.deleteById(7L);
}
上一篇: mybatisplus的基本使用
下一篇: MybatisPlus的使用