MyBatisPlus
导入坐标
SpringBoot并没有收录mybatisplus,所以需要自己导入坐标
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
# 数据源
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.NonRegisteringDriver
url: jdbc:mysql://127.0.0.1:3306/mybatisplus_db?serverTimezone=UTC
username: root
password: root
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
mapper
继承mybatisplus的basemapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.freshman.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
测试功能
@Autowired
private UserMapper userMapper;
@Test
void testSelectAll() {
List<User> userList = userMapper.selectList(null);
System.out.println(userList);
}
以上就是mybatispuls一个入门案例
概述
mybatisplus是基于mybatis框架基础上开发的增强型工具,旨在简化开发、提高效率
标准数据层CRUD功能
功能 | MP接口 |
---|---|
新增 | int insert(T t) |
删除 | int deleteById(Serializable id) |
修改 | int updateById(T t) |
根据id查询 | T selectById(Serializable id) |
查询全部 | List |
分页查询 | IPage |
按条件查询 | IPage |
讲一下分页查询
-
配置分页查询拦截器
@Configuration public class MpConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }
-
测试
@Test void testPage(){ // select * from user limit 1,2; IPage<User> userPage = new Page<>(1,2); userMapper.selectPage(userPage,null); System.out.println("当前页码值:" + userPage.getCurrent()); System.out.println("每页显示数:" + userPage.getSize()); System.out.println("一共多少页:" + userPage.getPages()); System.out.println("一共多少条数据:" + userPage.getTotal()); System.out.println("要查询到的数据:" + userPage.getRecords()); } /** ==> Preparing: SELECT id,name,password,age,tel FROM user LIMIT ? ==> Parameters: 2(Long) <== Columns: id, name, password, age, tel <== Row: 1, zhangsan, 123, 12, null <== Row: 2, lisi, 123, 12, null <== Total: 2 */
条件查询:
-
lambdaquerywrapper
@Test void testSelect(){ LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>(); // SELECT id,name,password,age,tel FROM user WHERE (age > ?) wrapper.gt(User::getAge,11); List<User> users = userMapper.selectList(wrapper); System.out.println(users); } /** ==> Preparing: SELECT id,name,password,age,tel FROM user WHERE (age > ?) ==> Parameters: 11(Integer) <== Columns: id, name, password, age, tel <== Row: 1, zhangsan, 123, 12, 123 <== Row: 2, lisi, 123, 12, 123 <== Row: 3, wangwu, 123, 12, 123 <== Total: 3 */
更多方法:https://mybatis.plus/guide/wrapper.html#abstractwrapper
-
字段映射和表名映射
// 设计表的名称 @TableName("tbl_user") public class User{ private Long id; private String name; // 在数据表中为pwd,且查询时不参与查询 即 selct id name from user;没有pwd @TableField(value = "pwd",select = false) private String password; // 数据库中不存在这个字段 @TableField(exist = false) private Integer online; }
-
id生成策略
AUTO(0) 使用数据库id自增策略控制id生成 NONE(1) 不设置id生成策略 INPUT(2) 用户手工输入id ASSIGN_ID(3) 雪花算法生成id(可兼容数值型和字符串型) ASSIGN_UUID(4) 以UUID生成算法作为id生成策略 雪花算法
public class User{ @TableId(type = IdType.AuTo) private Long id; }
-
逻辑删除
为数据设置是否可用状态字段,删除时设置状态字段为不可用状态,数据保留在数据库中
// value 为 默认不删除的值 delval 为 默认删除的值 @TableLogic(value = "0",delval = "1") private Integer deleted; ==> Preparing: UPDATE user SET deleted=1 WHERE id=? AND deleted=0 ==> Parameters: 1(Long) <== Updates: 1
-
全局配置
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: id-type: assign_id logic-delete-field: deleted logic-delete-value: 1 logic-not-delete-value: 0
-
乐观锁
-
配置拦截器
@Configuration public class MpConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ // 1、定义拦截器 MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); // 2、添加分页拦截器 mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 3、添加乐观锁拦截器 mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return mybatisPlusInterceptor; } }
-
设置version
@Version private Integer version;
-
测试方法
@Test void testUpdate(){ User user = new User(); user.setId(3L); user.setName("ligeng"); user.setVersion(1); userMapper.updateById(user); } /** ==> Preparing: UPDATE user SET name=?, version=? WHERE id=? AND version=? AND deleted=0 ==> Parameters: ligeng(String), 2(Integer), 3(Long), 1(Integer) 这里第二个参数为2,即执行了version = version + 1 <== Updates: 1 */
-
-
代码生成器 (使用mybatisx插件生成即可)