溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mybatis-plus中BaseMapper怎么用

發布時間:2021-08-18 09:04:11 來源:億速云 閱讀:2021 作者:小新 欄目:開發技術

小編給大家分享一下mybatis-plus中BaseMapper怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

入門使用BaseMapper完成增刪改查

根據數據庫表制作相應實體類

@TableName(value = "user")
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;
    // 省略set,get
    }

創建對應mapper類

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

由于BaseMapper已經集成了基礎的增刪改查方法,這里對應的mapper.xml也是不用寫的

添加關于mapper包的注冊

@SpringBootApplication
@MapperScan("com.hyx.mybatisplusdemo.mapper")
public class MybatisplusdemoApplication {

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

}

修改配置文件

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///test?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

測試類

@SpringBootTest
class MybatisplusdemoApplicationTests {

 @Autowired
 UserMapper userMapper;

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

}

如果要自定義一些增刪改查方法,可以在配置類中添加:

##mybatis-plus mapper xml 文件地址
mybatis-plus.mapper-locations= classpath*:mapper/*Mapper.xml
##mybatis-plus type-aliases 文件地址
mybatis-plus.type-aliases-package= com.hyx.mybatisplusdemo.entity

然后就與mybatis一樣,創建對應的xml文件,去實現相應的方法就可以了

BaseMapper各方法詳解

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

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

看完了這篇文章,相信你對“mybatis-plus中BaseMapper怎么用”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女