### 使用Mapper
BeetlSQL3更為推薦的使用Mapper接口,而不是SQLManager,SQLManger是更為底層的API,使用Mapper能更容易的維護業務代碼
創建一個接口UserMapper
```java
public interface UserMapper extends BaseMapper<UserEntity> {
}
```
`BaseMapper`是BeetlSQL3提供的一個接口,內置了大量CRUD方法。
```java
//得到一個UserMapper接口的代理
UserMapper mapper = sqlManager.getMapper(UserMapper.class);
UserEntity me = mapper.unique(1);//同SQLManager.unique(UserEntity.class,1)
me.setName("newName");
mapper.updateById(me);//同同SQLManager.updateById(me);
```
其他常用的方法在SQLManager也有,BaseMapper有如下內置方法
```java
/**
* 通用插入,插入一個實體對象到數據庫,所以字段將參與操作,除非你使用ColumnIgnore注解
*SqlResource
* @param entity
*/
@AutoMapper(InsertAMI.class)
void insert(T entity);
/**
* 插入實體到數據庫,對于null值不做處理
*
* @param entity
*/
@AutoMapper(InsertTemplateAMI.class)
void insertTemplate(T entity);
/**
* 批量插入實體。此方法不會獲取自增主鍵的值,如果需要,建議不適用批量插入,適用
* <pre>
* insert(T entity,true);
* </pre>
*
* @param list
*/
@AutoMapper(InsertBatchAMI.class)
void insertBatch(List<T> list);
/**
* 根據主鍵更新對象,所以屬性都參與更新。也可以使用主鍵ColumnIgnore來控制更新的時候忽略此字段
* @param entity
* @return
*/
@AutoMapper(UpdateByIdAMI.class)
int updateById(T entity);
/**
* 根據主鍵更新對象,只有不為null的屬性參與更新
*
* @param entity
* @return
*/
@AutoMapper(UpdateTemplateByIdAMI.class)
int updateTemplateById(T entity);
/**
* 按照主鍵更新更新或插入,自增或者序列id自動賦值給entity
* @param entity 待更新/插入的實體對象
* @return 如果是插入操作,返回true,如果是更新,返回false
*/
@AutoMapper(UpsertAMI.class)
boolean upsert(T entity);
/**按照主鍵更新或插入,更新失敗,會調用插入,屬性為空的字段將不更新或者插入。自增或者序列id自動賦值給entity
* @param entity 待更新/插入的實體對象
* @return
*/
@AutoMapper(UpsertByTemplateAMI.class)
int upsertByTemplate(T entity);
/**
* 根據主鍵刪除對象,如果對象是復合主鍵,傳入對象本生即可
*
* @param key
* @return
*/
@AutoMapper(DeleteByIdAMI.class)
int deleteById(Object key);
/**
* 根據主鍵獲取對象,如果對象不存在,則會拋出一個Runtime異常
*
* @param key
* @return
*/
@AutoMapper(UniqueAMI.class)
T unique(Object key);
/**
* 根據主鍵獲取對象,如果對象不存在,返回null
*
* @param key
* @return
*/
@AutoMapper(SingleAMI.class)
T single(Object key);
/**
* 根據一批主鍵查詢
* @param key
* @return
*/
@AutoMapper(SelectByIdsAMI.class)
List<T> selectByIds(List<?> key);
default boolean exist(Object key){
return this.getSQLManager().exist(this.getTargetEntity(),key);
}
/**
* 根據主鍵獲取對象,如果在事物中執行會添加數據庫行級鎖(select * from table where id = ? for update),如果對象不存在,返回null
*
* @param key
* @return
*/
@AutoMapper(LockAMI.class)
T lock(Object key);
/**
* 返回實體對應的所有數據庫記錄
*
* @return
*/
@AutoMapper(AllAMI.class)
List<T> all();
/**
* 返回實體在數據庫里的總數
*
* @return
*/
@AutoMapper(AllCountAMI.class)
long allCount();
/**
* 模板查詢,返回符合模板得所有結果。beetlsql將取出非null值(日期類型排除在外),從數據庫找出完全匹配的結果集
*
* @param entity
* @return
*/
@AutoMapper(TemplateAMI.class)
List<T> template(T entity);
/**
* 模板查詢,返回一條結果,如果沒有,返回null
*
* @param entity
* @return
*/
@AutoMapper(TemplateOneAMI.class)
<T> T templateOne(T entity);
/**
* 符合模板得個數
*
* @param entity
* @return
*/
@AutoMapper(TemplateCountAMI.class)
long templateCount(T entity);
/**
* 執行一個jdbc sql模板查詢
*
* @param sql
* @param args
* @return
*/
@AutoMapper(ExecuteAMI.class)
List<T> execute(String sql, Object... args);
/**
* 執行一個更新的jdbc sql
*
* @param sql
* @param args
* @return
*/
@AutoMapper(ExecuteUpdateAMI.class)
int executeUpdate(String sql, Object... args);
@AutoMapper(GetSQLManagerAMI.class)
SQLManager getSQLManager();
/**
* 返回一個Query對象
*
* @return
*/
@AutoMapper(QueryAMI.class)
Query<T> createQuery();
/**
* 返回一個LambdaQuery對象
*
* @return
*/
@AutoMapper(LambdaQueryAMI.class)
LambdaQuery<T> createLambdaQuery();
/**
* 得到mapper的范型類
* @return
*/
@AutoMapper(GetTargetEntityAMI.class)
Class getTargetEntity();
```
> 如何編寫自己的BaseMaper或者擴展BaseMapper,超出了本章的范圍,會在后面章節提到,如果有興趣,可以直接查看@AutoMapper標注的類,是方法的具體實現
對于UserMapper來說,僅僅繼承BaseMapper是不夠的,可以在UserMapper上添加更多的數據庫訪問代碼
```java
@Sql("select * from sys_user where id = ?")
@Select
UserEntity queryUserById(Integer id);
@Sql("update sys_user set name=? where id = ?")
@Update
int updateName(String name,Integer id);
@Template("select * from sys_user where id = #{id}")
UserEntity getUserById(Integer id);
@Template("update sys_user set name=#{name} where id = #{id}")
@Update
int updateName(String name,Integer id);
@SpringData // 生成 select * from user hwere name=? order by id
List<UserEntity> queryByNameOrderById(String name);
/**
* 可以定義一個default接口
* @return
*/
default List<DepartmentEntity> findAllDepartment(){
Map paras = new HashMap();
paras.put("exlcudeId",1);
List<DepartmentEntity> list = getSQLManager().execute("select * from department where id != #{exlcudeId}",DepartmentEntity.class,paras);
return list;
}
```
* queryUserById 使用@Sql注解提供了Sql語句,@Select是可選的,默認都是查詢操作
* updateName, 使用@Sql注解提供了Sql語句,使用@Update,表示這是更新語句,返回更新成功數目
* @Template 表示按照模板查詢,這時候要求方法參數名字與模板語句的變量名一致。如果沒有啟用JDK8的parameter,則需要使用注解@Param來申明參數的名字
```java
@Template("select * from sys_user where id = #{id}")
UserEntity getUserById(@Param("id") Integer myId);
```
* queryByNameOrderById 是使用@SpringData風格來拼接一個SQL,這會在后面詳細介紹
* findAllDepartment 是一個接口方法實現,你也可以通過default method來實現簡單的邏輯
- BeetlSQL 3 指南
- 數據訪問框架
- 適合用戶
- 編譯源碼
- 直接看代碼
- 閑大賦介紹
- BeetlSQL3 特點
- 數據庫訪問工具的痛點
- BeetlSQL3 例子
- 基礎例子
- 結果集映射
- 翻頁查詢
- 演示like,batchUpdate,in 操作
- 自動fetch
- 多數據庫
- 代碼生成框架
- 安裝BeetlSQL
- 直接安裝
- 框架集成
- 編譯源碼
- 快速開始
- 環境準備
- 環境搭建
- 使用BeetlSQL
- 按照主鍵查尋
- 更新
- 按照模板查詢
- 執行SQL
- 執行模板SQL
- 使用Query
- 使用Mapper
- 使用模板文件
- SQLManager
- 內置語句
- 內置查詢API
- template查詢
- 更新操作
- 執行SQL
- 執行模板SQL
- 把SQL放到文件里
- 翻頁查詢
- SQLResult
- Stream查詢
- 存儲過程調用
- NameConversion(重要)
- DBStyle
- Inerceptor
- SQLManagerExtend
- 強制使用數據源
- Mapper
- 實現Mapper
- @Sql
- @Update @BatchUpdate
- @Template
- 參數名稱
- 參數返回值
- 執行SQL文件
- 翻頁查詢
- @SqlProvider
- @SpringData
- @SubQuery
- @InheritMapper
- @Call
- StreamData
- Default Method
- 定義自己的BaseMapper
- 限制Java代碼中SQL長度
- 數據模型
- POJO
- 不嚴格的POJO
- 交集(重要)
- @Table 注解
- @Column 注解
- 主鍵
- RowMapper
- ResultSetMapper
- Json配置映射
- Json自動映射
- XML支持
- 自動Fetch
- AttributeConvert
- BeanConvert
- 枚舉
- 混合模型
- Map模型
- 動態模型
- 模型其他注解
- 安全擴展注解
- BeetlSql 單表查詢工具Query
- Query使用方式和風格介紹
- Query主要操作簡介
- 查詢器獲取
- SELECT簡單的條件查詢
- 復雜的條件查詢
- 查詢字段智能處理
- 健壯的變量
- 自定義實現
- INSERT操作
- UPDATE操作
- DELETE操作
- single查詢和unique
- COUNT查詢
- GROUP分組查詢和Having子句
- 分頁查詢
- ORDER BY 排序
- page分頁查詢
- 方法調用順序
- Markdown文件
- Beetl 入門
- 定界符號
- 變量
- 算數表達式
- 邏輯表達式
- 控制語句
- 訪問變量屬性
- 判斷對象非空(重要)
- 調用方法
- 自定義方法
- 內置方法
- 標簽功能
- 附錄
- Idea 插件
- 代碼生成
- 多庫使用
- 業務主從庫
- 主從庫延遲問題
- 多個業務庫
- 合并多個業務庫 1
- 合并多個業務庫 2
- 單表多租戶
- 每個租戶一個表
- 多庫多租戶
- 使用ShardingSphere
- Saga(試驗)
- 關于Saga的一些認識
- 關于隔離性
- BeetlSQL Saga
- SagaMapper
- Saga 多庫事務實現
- Saga 微服務 實現
- 配置 Saga Server
- Swagger
- BeetlSQL 性能
- 測試方法
- 最新
- 2021-11-21
- 2020-9-28
- Spring 快速開始
- 環境準備
- 環境搭建
- SpringBoot 快速開始
- 環境準備
- 環境搭建
- 擴展BeetlSQL3
- 完整配置
- BaseMapper定制
- 代碼生成
- SQLExecutor定制
- 第三方
- MetadataManager
- JFinal集成
- ExecuteContext
- 參考ACT
- 數據庫表到Java對象
- Solon
- NameConversion
- ViewType
- RowMapper
- ResultSetMapper
- AttributeConvert
- BeanConvert
- Fetch
- 代碼生成
- 擴展BeetlSQL3
- BaseMapper定制
- SQLExecutor定制
- MetadataManager
- ExecuteContext
- 數據庫表到Java對象
- NameConversion
- ViewType
- RowMapper
- ResultSetMapper
- AttributeConvert
- BeanConvert
- Fetch
- 代碼生成
- BeetlSQL 多數據庫支持
- 多庫之間的不同
- 跨庫支持實現
- DBStyle
- MySqlStyle 例子
- H2Style例子
- ClickHouseStyle例子
- HBaseStyle例子
- DruidStyle例子
- MetadataManager
- BeanProcessor
- 與BeetlSQL2的區別
- 使用區別