mybatis-plus中BaseMapper入門使用
具體教程參考官網文檔: baomidou.com/
入門使用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 }
創(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文件,去實現相應的方法就可以了
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入門使用的文章就介紹到這了,更多相關mybatis-plus BaseMapper入門內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java多線程處理執(zhí)行solr創(chuàng)建索引示例
這篇文章主要介紹了java多線程處理執(zhí)行solr創(chuàng)建索引示例,需要的朋友可以參考下2014-02-02mybatis 如何判斷l(xiāng)ist集合是否包含指定數據
這篇文章主要介紹了mybatis 判斷l(xiāng)ist集合是否包含指定數據的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringCloud Config分布式配置中心使用教程介紹
springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client端通過接口獲取數據、并依據此數據初始化自己的應用2022-12-12