RuoYi-Cloud增加MyBatisPlus
发布人:shili8
发布时间:2024-11-18 00:40
阅读次数:0
**RuoYi-Cloud 增加 MyBatisPlus**
最近,我在 RuoYi-Cloud项目中增加了 MyBatisPlus 的支持。这个决定是基于以下几个原因:
* **性能优化**:MyBatisPlus 提供了更高效的 SQL 执行和结果集处理能力,能够显著提高系统的性能。
* **功能扩展**:MyBatisPlus 支持更多的特性,如分页、排序、缓存等,可以帮助我们实现更加复杂的业务逻辑。
在本文中,我们将详细介绍如何在 RuoYi-Cloud项目中增加 MyBatisPlus 的支持,包括配置、使用示例和代码注释。
### **1. 配置 MyBatisPlus**
首先,我们需要在 `pom.xml` 文件中添加 MyBatisPlus 的依赖:
xml<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency>
然后,我们需要在 `application.properties` 文件中配置 MyBatisPlus 的基本信息:
propertiesspring: datasource: username: root password:123456 url: jdbc:mysql://localhost:3306/ruoyi_cloud?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Drivermybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
### **2. 创建实体类**
接下来,我们需要创建对应的实体类,例如 `SysUser`:
javapackage com.ruoyi.cloud.entity; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; import java.util.Date; /** * <p> * * </p> * * @author ruoyi * @since2022-05-25 */ @TableName("sys_user") public class SysUser implements Serializable { private static final long serialVersionUID =1L; /** * id */ @TableId(value = "id", type = IdType.AUTO) private Long id; /** * username */ private String username; /** * password */ private String password; /** * email */ private String email; public SysUser() {} public SysUser(Long id, String username, String password, String email) { this.id = id; this.username = username; this.password = password; this.email = email; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
### **3. 创建 Mapper**
接下来,我们需要创建对应的Mapper,例如 `SysUserMapper`:
javapackage com.ruoyi.cloud.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.ruoyi.cloud.entity.SysUser; import org.apache.ibatis.annotations.Mapper; /** * <p> * * </p> * * @author ruoyi * @since2022-05-25 */ @Mapperpublic interface SysUserMapper extends BaseMapper<SysUser> { }
### **4. 使用示例**
最后,我们可以使用 MyBatisPlus 的 API 来进行 CRUD 操作:
javapackage com.ruoyi.cloud.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ruoyi.cloud.entity.SysUser; import org.springframework.stereotype.Service; /** * <p> * * </p> * * @author ruoyi * @since2022-05-25 */ @Servicepublic class SysUserService { private final SysUserMapper sysUserMapper; public SysUserService(SysUserMapper sysUserMapper) { this.sysUserMapper = sysUserMapper; } public List<SysUser> getAllUsers() { return sysUserMapper.selectList(new QueryWrapper<>()); } public SysUser getUserById(Long id) { return sysUserMapper.selectOne(new QueryWrapper<>(SysUser.builder().id(id).build())); } public void createUser(SysUser user) { sysUserMapper.insert(user); } public void updateUser(SysUser user) { sysUserMapper.update(user, new QueryWrapper<>(SysUser.builder().id(user.getId()).build())); } public void deleteUser(Long id) { sysUserMapper.delete(new QueryWrapper<>(SysUser.builder().id(id).build())); } }
通过以上步骤,我们成功地在 RuoYi-Cloud项目中增加了 MyBatisPlus 的支持。