MyBatis-Plus UserMpper接口示例实现
作者:乘风破浪酱52436
本文主要介绍了MyBatis-Plus UserMpper接口示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
UserMapper 接口示例
在 MyBatis-Plus 中,UserMapper
是一个非常简单但功能强大的接口。它通常长这样:
基本结构
package com.example.mapper; // 根据你的项目结构调整包名 import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.entity.User; // 你的User实体类所在的包 import org.apache.ibatis.annotations.Mapper; // 使用@Mapper注解让Spring管理这个接口,并生成实现类 @Mapper public interface UserMapper extends BaseMapper<User> { // 不需要写任何方法,就已经继承了BaseMapper中的所有CRUD方法 // 但你也可以根据需要添加自定义方法 }
详细解释
1. 必需的组成部分
继承 BaseMapper<T>:这是最关键的部分,通过继承并指定泛型类型为你的实体类(这里是 User),你的 UserMapper 就自动获得了 BaseMapper 中定义的约 20 个常用 CRUD 方法。
@Mapper 注解:这个注解告诉 MyBatis 这是一个映射器接口,Spring 启动时会自动为其创建代理实现类。
2. 完整的示例(包含实体类和配置)
为了让示例更完整,这里也展示一下相关的 User
实体类和 Spring Boot 配置:
User 实体类 (User.java)
package com.example.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableField; // 指定实体类对应的数据库表名 @TableName("user") public class User { // 主键字段 @TableId private Long id; // 普通字段,如果字段名与数据库列名一致,可以不加注解 private String name; private Integer age; // 如果数据库列名与字段名不一致,可以使用@TableField指定 @TableField("email_address") private String email; // 省略构造函数、getter和setter方法... public User() { } public User(Long id, String name, Integer age, String email) { this.id = id; this.name = name; this.age = age; this.email = email; } // getter 和 setter 方法 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; } // 其他getter和setter... }
Spring Boot 配置 (application.yml)
# 数据源配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf-8&useSSL=false username: your_username password: your_password # MyBatis-Plus 配置 mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml # 如果有自定义XML查询文件,指定路径 type-aliases-package: com.example.entity # 实体类包路径 configuration: map-underscore-to-camel-case: true # 自动开启驼峰命名转换 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印SQL日志
3. 如何使用 UserMapper
在你的 Service 或 Controller 中,你可以直接注入 UserMapper
并使用它:
package com.example.service; import com.example.mapper.UserMapper; import com.example.entity.User; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getAllUsers() { // 查询所有用户 return userMapper.selectList(null); } public User getUserById(Long id) { // 根据ID查询用户 return userMapper.selectById(id); } public List<User> getUsersByName(String name) { // 条件查询:使用Lambda表达式避免硬编码字段名 LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>(); wrapper.like(User::getName, name); return userMapper.selectList(wrapper); } public int addUser(User user) { // 插入用户 return userMapper.insert(user); } public int updateUser(User user) { // 更新用户 return userMapper.updateById(user); } public int deleteUser(Long id) { // 删除用户 return userMapper.deleteById(id); } }
4. 自定义方法
虽然 BaseMapper
提供了丰富的通用方法,但有时你可能需要添加自定义查询:
@Mapper public interface UserMapper extends BaseMapper<User> { // 自定义查询方法:查询年龄大于指定值的用户 @Select("SELECT * FROM user WHERE age > #{minAge}") List<User> selectUsersOlderThan(@Param("minAge") Integer minAge); // 或者使用XML配置的方式 List<User> selectUsersByComplexCondition(Map<String, Object> params); }
然后在 resources/mapper/UserMapper.xml
中:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <select id="selectUsersByComplexCondition" resultType="com.example.entity.User"> SELECT * FROM user WHERE name LIKE CONCAT('%', #{name}, '%') AND age BETWEEN #{minAge} AND #{maxAge} </select> </mapper>
总结
UserMapper
接口的核心特点是:
- 继承
BaseMapper<User>
,自动获得大量CRUD方法 - 使用
@Mapper
注解标记为MyBatis映射器 - 不需要编写任何实现代码,MyBatis-Plus会自动生成代理实现
- 可以添加自定义方法满足特定业务需求
这种设计极大地减少了传统MyBatis中需要编写的模板代码,让开发者能够更专注于业务逻辑的实现。
到此这篇关于MyBatis-Plus UserMpper接口示例的文章就介绍到这了,更多相关MyBatis-Plus UserMpper接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!