mybatis-plus的BaseMapper入门使用

mybatis-plus的BaseMapper入门使用

具体教程参考官网文档: https://baomidou.com/

入门使用BaseMapper完成增删改查

根据数据库表制作相应实体类

@TableName(value = "user")
@Date
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    private String name;
    private String password;
    private String username;
  
    }

创建对应mapper类

public interface UserMapper extends BaseMapper<User> {
 //这里什么都不用写
}

由于BaseMapper已经集成了基础的增删改查方法,这里对应的mapper.xml也是不用写的

添加关于mapper包的注册

package com.jihu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MybatisPlus1Application {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlus1Application.class, args);
    }

}

修改配置文件

#数据库连接配置
spring.datasource.username=root
spring.datasource.password=123456
#mysql5~8 驱动不同driver-class-name     8需要增加时区的配置serverTimezone=UTC
#useSSL=false 安全连接
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#配置日志  log-impl:日志实现
#StdOutImpl代表为控制台输出sql语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#如果要自定义一些增删改查方法,按需要在配置类中添加:
##mybatis-plus mapper xml 文件地址
mybatis-plus.mapper-locations= classpath*:mapper/*Mapper.xml  //指定自定义mapper文件的路径
##mybatis-plus type-aliases 文件地址
mybatis-plus.type-aliases-package= com.hyx.mybatisplusdemo.entity  //定义别名

测试类

@SpringBootTest
class MybatisplusdemoApplicationTests {

 @Autowired
 UserMapper userMapper;

 @Test
 void contextLoads() {
  
  User user = userMapper.selectById(7l);
  userMapper.deleteById(user);
  System.out.println(user);
 }

}

然后就与mybatis一样,创建对应的xml文件,去实现相应的方法就可以了

BaseMapper各方法详解

Select

// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

Insert

// 插入一条记录
int insert(T entity);

Delete

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

Update

// 根据 whereEntity 条件,更新记录
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);

posted @ 2022-04-06 14:20  爲誰心殇  阅读(1739)  评论(0编辑  收藏  举报
>