MybatisPlus之-----BaseMapper
簡介
ofollow,noindex" target="_blank">MyBatis-Plus (簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生
特性
- 無侵入:只做增強不做改變,引入它不會對現有工程產生影響,如絲般順滑
- 損耗小:啟動即會自動注入基本 CURD,效能基本無損耗,直接面向物件操作
- 強大的 CRUD 操作:內建通用 Mapper、通用 Service,僅僅通過少量配置即可實現單表大部分 CRUD 操作,更有強大的條件構造器,滿足各類使用需求
- 支援 Lambda 形式呼叫:通過 Lambda 表示式,方便的編寫各類查詢條件,無需再擔心欄位寫錯
- 支援多種資料庫:支援 SQL/">MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多種資料庫
- 支援主鍵自動生成:支援多達 4 種主鍵策略(內含分散式唯一 ID 生成器 - Sequence),可自由配置,完美解決主鍵問題
- 支援 XML 熱載入:Mapper 對應的 XML 支援熱載入,對於簡單的 CRUD 操作,甚至可以無 XML 啟動
- 支援 ActiveRecord 模式:支援 ActiveRecord 形式呼叫,實體類只需繼承 Model 類即可進行強大的 CRUD 操作
- 支援自定義全域性通用操作:支援全域性通用方法注入( Write once, use anywhere )
- 支援關鍵詞自動轉義:支援資料庫關鍵詞(order、key......)自動轉義,還可自定義關鍵詞
- 內建程式碼生成器:採用程式碼或者 Maven 外掛可快速生成 Mapper 、 Model 、 Service 、 Controller 層程式碼,支援模板引擎,更有超多自定義配置等您來使用
- 內建分頁外掛:基於 MyBatis 物理分頁,開發者無需關心具體操作,配置好外掛之後,寫分頁等同於普通 List 查詢
- 內建效能分析外掛:可輸出 Sql 語句以及其執行時間,建議開發測試時啟用該功能,能快速揪出慢查詢
- 內建全域性攔截外掛:提供全表 delete 、 update 操作智慧分析阻斷,也可自定義攔截規則,預防誤操作
- 內建 Sql 注入剝離器:支援 Sql 注入剝離,有效預防 Sql 注入攻擊
安裝:Maven+jdk8+spring4.0
1.引入maven依賴
<!-- mp依賴 mybatisPlus 會自動的維護Mybatis 以及MyBatis-spring相關的依賴 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.0.3</version> </dependency>
2.配置dataSource,實現繼承BaseMapper,配置mapper檔案包位置,然後就可使用內建的CRUD了
applicationContext.xml 在資料來源等配置基礎上新增如下配置 <!-- 配置mybatis 掃描mapper介面的路徑 mapper為EmployeeMapper所在包的位置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper"></property> </bean>
3.
//Beans @TableName("tbl_employee")//設定物件名和表明對應 public class Employee { //value指定與資料庫列名一致,type指定主鍵策略為自增,即不用手動插入id @TableId(value = "id",type =IdType.AUTO) private Integer id ; @TableField("last_name")//設定駝峰命名和資料庫命令對應,也可以在配置檔案中進行全域性配置。 //有說法稱:my已經預設開啟了這一對應關係,但是我的沒有生效,所以又手動配置了 private StringlastName; private Stringemail ; private Integer gender; private Integer age ; //資料庫中不存在的列,但存在類中,註解後不會注入到sql @TableField(exist = false) private Double salary ; setter(){} getter(){} } //Mapper public interface EmployeeMapper extends BaseMapper<Employee> { //不需要實現,這時候就可以呼叫baseMapper的增刪改了 } //Controller //載入配置檔案 private ApplicationContext ioc =new ClassPathXmlApplicationContext("applicationContext.xml"); private EmployeeMapper employeeMapper= ioc.getBean("employeeMapper", EmployeeMapper.class); @Test public void testCommonInsert() { //初始化Employee物件 Employee employee= new Employee(); employee.setLastName("MP"); employee.setEmail("[email protected]"); employee.setGender(1); employee.setAge(18); Integer result = employeeMapper.insert(employee); System.out.println("result: " + result ); //獲取當前資料在資料庫中的主鍵值 Integer key = employee.getId(); System.out.println("key:" + key ); } 到此已經實現了通用CRUD的操作。
4.附一下BaseMapper的包含的方法
package com.baomidou.mybatisplus.core.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; public interface BaseMapper<T> { int insert(T var1); int deleteById(Serializable var1); int deleteByMap(@Param("cm") Map<String, Object> var1); int delete(@Param("ew") Wrapper<T> var1); int deleteBatchIds(@Param("coll") Collection<? extends Serializable> var1); int updateById(@Param("et") T var1); int update(@Param("et") T var1, @Param("ew") Wrapper<T> var2); T selectById(Serializable var1); List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> var1); List<T> selectByMap(@Param("cm") Map<String, Object> var1); T selectOne(@Param("ew") Wrapper<T> var1); Integer selectCount(@Param("ew") Wrapper<T> var1); List<T> selectList(@Param("ew") Wrapper<T> var1); List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1); List<Object> selectObjs(@Param("ew") Wrapper<T> var1); IPage<T> selectPage(IPage<T> var1, @Param("ew") Wrapper<T> var2); IPage<Map<String, Object>> selectMapsPage(IPage<T> var1, @Param("ew") Wrapper<T> var2); }