目录一 首先添加相关的依赖二 在Mapper层面对BaseMapper的继承同时泛型类型填写数据库对应的实体类三 数据库映射实体类的定义四 对应的方法一 首先添加相关的依赖dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.5.3/version !-- 替换为最新版本 -- /dependency二 在Mapper层面对BaseMapper的继承同时泛型类型填写数据库对应的实体类Mapper public interface UserDao extends BaseMapperUser { }三 数据库映射实体类的定义相关注解的必要性1 TableName这个注解是用于指定表名的如果说数据库当中的表名与类名相同则不需要指定。2 TableField这个注解是用于指定类的成员变量与字段之间的映射关系如果说与字段相同则不需要指定。3 TableFeild注解当中填写exist false是当这个成员变量在数据库表中没有对应的字段时。4 TableId注解主要是针对主键如果说主键是id则不需要标注否则需要将这个主键表上指明主键Data NoArgsConstructor AllArgsConstructor TableName(demo_data1) public class User { /** * 主键 姓名 */ TableId(value name) TableField(name) private String name; /** * 年龄 */ TableField(age) private String age; /** * 性别 */ TableField(sex) private String sex; /** * 地址 */ TableField(address) private String address; /** * 备注 */ TableField(exist false) private String remark; //构造方法 public User(String name, String age, String sex, String address) { this.name name; this.age age; this.sex sex; this.address address; } }四 对应的方法Autowired private UserMapper userMapper; Test void insert() { // 插入 int insert userMapper.insert(new User(小明, 10, 123456, 123456)); System.out.println(插入 insert 条数据); } Test void query() { // 1 查询单条 LambdaQueryWrapperUser wrapper new LambdaQueryWrapper(); wrapper.eq(User::getAge, 10).eq(User::getName, 小明); User user userMapper.selectOne(wrapper); System.out.println(user); // 2 查询多个符合条件的数据 ListUser users userMapper.selectList(wrapper); System.out.println(users); } Test void update() { // 根据条件去查询对应的数据 LambdaUpdateWrapperUser wrapper new LambdaUpdateWrapper(); wrapper.eq(User::getName, 小安).eq(User::getAge, 10); User user userMapper.selectOne(wrapper); // 1 根据id去寻找对应的实体数据将数据修改 user.setAge(66); int i userMapper.updateById(user); System.out.println(修改 i 条数据); // 2 按照某个条件批量(批量数据完全一样最好重新设置主键) // 新数据 User user3 new User(小安, 10, 男, 山东); // wrapper、老数据的查询标准查询到的去修改 int update userMapper.update(user3, wrapper); System.out.println(修改 update 条数据); // 3 直接使用条件更新不需要处理主键 LambdaUpdateWrapperUser wrappers new LambdaUpdateWrapper(); wrappers.eq(User::getAddress, active) .set(User::getName, 统一新名字) .setSql(age age 1); int result userMapper.update(null, wrappers); System.out.println(修改 result 条数据); } Test void delete() { LambdaQueryWrapperUser wrapper4 new LambdaQueryWrapper(); wrapper4.eq(User::getName, 小明).eq(User::getAge, 10); int delete userMapper.delete(wrapper4); System.out.println(删除 delete 条数据); }上述这种是先声明Wapper再借助Mapper进行调用的还有一种方式是直接使用类似LambdaQuery.eq().List;这种但是这种情况是属于在impl层的方法需要在impl当中去继承Mapper,Entity才可以Controller层 ↓ Service接口层 (UserService extends IServiceUser) ↓ ServiceImpl实现层 (UserServiceImpl extends ServiceImplUserMapper, User) ↓ Mapper接口层 (UserMapper extends BaseMapperUser) ↓ MyBatis-Plus框架 ↓ 数据库链式调用// 链式调用 // 1. 链式查询 ListUser result userService.lambdaQuery() .eq(User::getAge, 20) .like(User::getName, 张) .list(); // 2. 链式更新 boolean updated userService.lambdaUpdate() .eq(User::getStatus, 0) .set(User::getStatus, 1) .update();五 Mybatis的复习1 在使用Mybatis的使用过程当中Mybatis的功能是非常强大的这段代码。我们的传参是LastworkRequest类型的一个实体参数的名为param这里推荐都加上Param注解去指定变量名2 进入到Mybatis内部的编写resultMap指的是返回结果的类型这里也可以自定义而parameterType指的是传入参数的类型通常都是不写的框架自身会去进行识别select idceshi resultMap parameterType /selecteg 这种方法去自定义映射的关系 column是数据库字段名property是实体类的对应成员变量这种是较为复杂的时候自定义resultMap如果说是平时简单的映射直接去使用as别名去实现resultMap idCustomMethod typeorg.example.mybatis01.bean.Customer id propertyid columnid/ result propertycustomerName columncustomer_name/ result propertyphone columnphone/ collection propertyorders ofTypeorg.example.mybatis01.bean.Order id propertyid columno_id/ result propertyaddress columnaddress/ result propertyamount columnamount/ result propertycustomerId columncustomer_id/ /collection /resultMap select idgetCustomerByOrderId resultMapCustomMethod select c.*, o.* from orders o left join customer c on c.id o.customer_id where c.id 1; /select传参一种是传递的是对象直接获取集合当中的字段进行传参即可where if testname ! null and name ! name #{name}/if if testage ! nullAND age #{age}/if /where另一种是传递的是集合集合当中存储多个对象where if testparamList ! null and not paramList.isEmpty() foreach collectionparamList itemparam separatorOR ( if testparam.name ! null and param.name ! name #{param.name}/if if testparam.age ! nullAND age #{param.age}/if ) /foreach /if if testparamList null or paramList.isEmpty()12/if /whereforeach一对多这个主要是用于传参的一个指定。foreach collection集合参数名 item每个元素的变量名 index索引变量名 open开始符符号 close结束符号 separator分隔符 #{item变量名} /foreach返回返回对象构成的集合一对多!-- 客户的ResultMap关联一对多的订单复用你原有的resultMap仅修正字段别名 -- resultMap idCustomerWithOrdersResultMap typeorg.example.mybatis01.bean.Customer id propertyid columnc_id/ result propertycustomerName columncustomer_name/ result propertyphone columnphone/ !-- collection一对多关联订单客户→订单 -- collection propertyorders ofTypeorg.example.mybatis01.bean.Order id propertyid columno_id/ result propertyaddress columnaddress/ result propertyamount columnamount/ result propertycustomerId columncustomer_id/ /collection /resultMap返回元素构成的对象一对一!-- 订单的ResultMap关联一对一的客户 -- resultMap idOrderWithCustomerResultMap typeorg.example.mybatis01.bean.Order id propertyid columno_id/ result propertyaddress columnaddress/ result propertyamount columnamount/ result propertycustomerId columncustomer_id/ !-- association一对一关联客户订单→客户 -- association propertycustomer javaTypeorg.example.mybatis01.bean.Customer id propertyid columnc_id/ result propertycustomerName columncustomer_name/ result propertyphone columnphone/ /association /resultMap总结传集合借助foreach返回集合association处理 “一对一”关联的是单个对象比如 “一个订单属于一个用户”订单对象里有一个User类型的属性。collection处理 “一对多”关联的是多个对象的集合比如 “一个用户有多个订单”用户对象里有一个ListOrder类型的属性。