MyBatis-plus如何简化操作数据库以及增强的?

目录

1.Mybatis-plus实现步骤

1. 引入依赖

2. 配置数据源和 MyBatis-Plus

3. 创建实体类

4. 创建 Mapper 接口、Service接口、ServiceImpl实现类

5. 使用 MyBatis-Plus 操作数据库

2.Mybatis-plus简化操作数据库的原理

1. 自动生成基本 CRUD 操作

2. 支持 Lambda 表达式

3. 内置条件构造器

4. 分页插件

5. 自动填充功能

6. 逻辑删除


1.Mybatis-plus实现步骤

MyBatis-Plus 是一个基于 MyBatis 的增强工具,旨在简化 MyBatis 的开发过程,提供了许多常用的功能和简化操作。使用 MyBatis-Plus 操作数据库通常包括以下步骤:

1. 引入依赖

<dependencies>
    <!-- MyBatis-Plus Starter -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.2</version> <!-- 使用最新版本 -->
    </dependency>
    <!-- MySQL Driver (如果使用 MySQL) -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>
    <!-- Spring Boot Starter (如果使用 Spring Boot) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

2. 配置数据源和 MyBatis-Plus

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydatabase
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3. 创建实体类

创建与数据库表对应的实体类User。使用 @TableName 注解指定数据库表名。

4. 创建 Mapper 接口、Service接口、ServiceImpl实现类

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface UserMapper extends BaseMapper<User> {
    // 可以定义自定义查询方法
}

public interface UserService extends IService<User> {
    //自定义逻辑,可以自定义接口
}

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    // 你可以在这里添加自定义业务逻辑
}

5. 使用 MyBatis-Plus 操作数据库

2.Mybatis-plus简化操作数据库的原理

1. 自动生成基本 CRUD 操作

因为我们的Mapper接口继承BashMapper<T>接口,因此可以拿到它父接口的方法。

因为Service接口继承了IService接口,因此同样可以拿到IService定义好的方法。

问题:

如果我们UserImpl类,直接去实现IService层或者Mapper层,那么我们是需要实现这两个接口的所有抽象方法的,这样我们可读性不是大大降低。

解决方案:

UserImpl去继承ServiceImpl<mapper,User>,并且实现Service

public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {}

ServiceImpl存在的意义就是去帮我们去重写接口的所有方法

类与接口关系如下:

2. 支持 Lambda 表达式

MyBatis-Plus 提供了 LambdaQueryWrapper 和 LambdaUpdateWrapper,让你可以使用 Lambda 表达式构建 SQL 查询条件,这样可以避免硬编码 SQL 字段名,从而减少出错的可能性。

LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getAge, 30);
List<User> users = userMapper.selectList(queryWrapper);

3. 内置条件构造器

MyBatis-Plus 提供了 QueryWrapper 和 UpdateWrapper,用于构造复杂的 SQL 查询条件和更新条件。这些构造器支持链式调用,使得构建 SQL 查询更加直观和简洁。例如:

QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 30).or().like("name", "John");
List<User> users = userMapper.selectList(queryWrapper);

4. 分页插件

MyBatis-Plus 提供了分页插件,简化了分页查询的实现。你只需在配置类中启用分页插件,然后在查询时使用 Page 对象,MyBatis-Plus 会自动处理分页逻辑。例如:

Page<User> page = new Page<>(1, 10); // 第 1 页,每页 10 条记录
IPage<User> userPage = userMapper.selectPage(page, null);
List<User> users = userPage.getRecords();

5. 自动填充功能

MyBatis-Plus 支持字段自动填充功能,这对于创建和更新时间戳字段非常有用。你可以定义 MetaObjectHandler 实现类,并在插入或更新时自动填充这些字段。例如:

public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}

6. 逻辑删除

  • 20
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是一个基于MyBatis的极简、高性能、增强的ORM框架,可以快速、便捷地进行数据库操作。连接数据库的具体步骤如下: 1. 引入mybatis-plus的依赖。在Maven项目中,在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>x.x.x</version> </dependency> ``` 其中,x.x.x表示mybatis-plus的版本号。 2. 配置数据源。在application.yml或application.properties文件中添加数据源配置,如下所示: ``` spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root ``` 其中,spring.datasource.driver-class-name表示数据库驱动程序的类名,spring.datasource.url表示数据库的连接URL,spring.datasource.username表示数据库的用户名,spring.datasource.password表示数据库的密码。 3. 配置MyBatis-Plus。在application.yml或application.properties文件中添加MyBatis-Plus的配置,如下所示: ``` mybatis-plus.mapper-locations=classpath:/mapper/*.xml mybatis-plus.type-aliases-package=com.example.entity ``` 其中,mybatis-plus.mapper-locations表示mapper文件的位置,mybatis-plus.type-aliases-package表示实体类所在的包。 4. 编写mapper接口。在mapper接口中使用MyBatis-Plus的注解来进行数据库操作,如下所示: ``` @Repository @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 其中,@Repository和@Mapper注解分别表示这是一个Spring的Repository和MyBatis的Mapper,UserMapper继承BaseMapper<User>,即可直接使用MyBatis-Plus提供的CRUD方法。 5. 在Service中调用mapper接口。在Service中注入mapper接口的实例,即可调用其中的方法进行数据库操作,如下所示: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 其中,@Autowired注解表示自动注入UserMapper的实例,selectById方法是MyBatis-Plus提供的查询方法。 至此,使用MyBatis-Plus连接数据库的步骤就完成了。需要注意的是,在使用MyBatis-Plus时,可以大大简化数据库操作,但是也需要注意数据库的安全性,比如避免SQL注入等安全漏洞。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值