1. 程式人生 > 其它 >mybatis-plus的BaseMapper入門使用

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);