mybatis-plus中BaseMapper入門使用
具體教程參考官網文檔: baomidou.com/
入門使用BaseMapper完成增刪改查
根據(jù)數(shù)據(jù)庫表制作相應實體類
@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
}
創(chuàng)建對應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一樣,創(chuàng)建對應的xml文件,去實現(xiàn)相應的方法就可以了
BaseMapper各方法詳解
Insert
// 插入一條記錄 int insert(T entity);
Delete
// 根據(jù) entity 條件,刪除記錄 int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); // 刪除(根據(jù)ID 批量刪除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 根據(jù) ID 刪除 int deleteById(Serializable id); // 根據(jù) columnMap 條件,刪除記錄 int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
Update
// 根據(jù) whereEntity 條件,更新記錄 int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); // 根據(jù) ID 修改 int updateById(@Param(Constants.ENTITY) T entity);
Select
// 根據(jù) ID 查詢 T selectById(Serializable id); // 根據(jù) entity 條件,查詢一條記錄 T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢(根據(jù)ID 批量查詢) List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 根據(jù) entity 條件,查詢全部記錄 List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢(根據(jù) columnMap 條件) List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 根據(jù) Wrapper 條件,查詢全部記錄 List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢全部記錄。注意: 只返回第一個字段的值 List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) entity 條件,查詢全部記錄(并翻頁) IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢全部記錄(并翻頁) IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據(jù) Wrapper 條件,查詢總記錄數(shù) Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
到此這篇關于mybatis-plus中BaseMapper入門使用的文章就介紹到這了,更多相關mybatis-plus BaseMapper入門內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Mybatis-Plus接口BaseMapper與Services使用詳解
- MybatisPlus?BaseMapper?實現(xiàn)對數(shù)據(jù)庫增刪改查源碼
- Mapper層繼承BaseMapper<T>需要引入的pom依賴方式
- mybatis抽取基類BaseMapper增刪改查的實現(xiàn)
- 淺談Mybatis Plus的BaseMapper的方法是如何注入的
- MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found Error處理)
- Mybatis-Plus BaseMapper的用法詳解
- BaseMapper接口的使用方法
相關文章
java多線程處理執(zhí)行solr創(chuàng)建索引示例
這篇文章主要介紹了java多線程處理執(zhí)行solr創(chuàng)建索引示例,需要的朋友可以參考下2014-02-02
mybatis 如何判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)
這篇文章主要介紹了mybatis 判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringCloud Config分布式配置中心使用教程介紹
springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應用2022-12-12
Java數(shù)據(jù)結構最清晰圖解二叉樹前 中 后序遍歷
樹是一種重要的非線性數(shù)據(jù)結構,直觀地看,它是數(shù)據(jù)元素(在樹中稱為結點)按分支關系組織起來的結構,很象自然界中的樹那樣。樹結構在客觀世界中廣泛存在,如人類社會的族譜和各種社會組織機構都可用樹形象表示2022-01-01

