https://gitee.com/free/Mapper/wikis/Home
更多內容上該網址查看
Maven依賴
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0-beta3</version>
</dependency>
Spring配置,代替org的mybatis
<!--通用mapper -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yuan.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>
注意這裏使用 tk.mybatis.spring.mapper.MapperScannerConfigure
替換原來Mybatis的 org.mybatis.spring.mapper.MapperScannerConfigurer
。
可配參數介紹:
- UUID :設置生成UUID的方法,需要用OGNL方式配置,不限制返回值,但是必須和字段類型匹配
- IDENTITY :取回主鍵的方式,可以配置的內容看下一篇如何使用中的介紹
- ORDER : <seletKey> 中的order屬性,可選值爲BEFORE和AFTER
- catalog :數據庫的catalog,如果設置該值,查詢的時候表名會帶catalog設置的前綴
- schema :同catalog,catalog優先級高於schema
- seqFormat :序列的獲取規則,使用{num}格式化參數,默認值爲{0}.nextval,針對Oracle,可選參數一共4個,對應0,1,2,3分別爲SequenceName,ColumnName, PropertyName,TableName
- notEmpty :insert和update中,是否判斷字符串類型!='',少數方法會用到
- style :實體和錶轉換時的規則,默認駝峯轉下劃線,可選值爲normal用實體名和字段名;camelhump是默認值,駝峯轉下劃線;uppercase轉換爲大寫;lowercase轉換爲小寫
- enableMethodAnnotation :可以控制是否支持方法上的JPA註解,默認false。
大多數情況下不會用到這些參數,有特殊情況可以自行研究。
實體類的寫法
記住一個原則:實體類的字段數量 >= 數據庫表中需要操作的字段數量。默認情況下,實體類中的所有字段都會作爲表中的字段來操作,如果有額外的字段,必須加上 @Transient
註解。
說明:
- 表名默認使用類名,駝峯轉下劃線(只對大寫字母進行處理),如 UserInfo 默認對應的表名爲 user_info 。
- 表名可以使用 @Table(name = "tableName") 進行指定,對不符合第一條默認規則的可以通過這種方式指定表名.
- 字段默認和 @Column 一樣,都會作爲表字段,表字段默認爲Java對象的Field名字駝峯轉下劃線形式.
- 可以使用 @Column(name = "fieldName") 指定不符合第3條規則的字段名
- 使用 @Transient 註解可以忽略字段,添加該註解的字段不會作爲表字段使用.
- 建議一定是有一個 @Id 註解作爲主鍵的字段,可以有多個 @Id 註解的字段作爲聯合主鍵.
- 如果是MySQL的自增字段,加上 @GeneratedValue(generator = "JDBC") 即可。
DAO 的寫法
在傳統的Mybatis寫法中, DAO 接口需要與 Mapper 文件關聯,即需要編寫 SQL 來實現 DAO 接口中的方法。而在通用Mapper中, DAO 只需要繼承一個通用接口,即可擁有豐富的方法:
繼承通用的Mapper ,必須指定泛型
@Component
public interface BasDictMapper extends Mapper<BasDict> {
}
一旦繼承了Mapper ,繼承的Mapper就擁有了Mapper 所有的通用方法:
Select
方法: List<T> select(T record);
說明:根據實體中的屬性值進行查詢,查詢條件使用等號
方法: T selectByPrimaryKey(Object key);
說明:根據主鍵字段進行查詢,方法參數必須包含完整的主鍵屬性,查詢條件使用等號
方法: List<T> selectAll();
說明:查詢全部結果,select(null)方法能達到同樣的效果
方法: T selectOne(T record);
說明:根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是拋出異常,查詢條件使用等號
方法: int selectCount(T record);
說明:根據實體中的屬性查詢總數,查詢條件使用等號
Insert
方法: int insert(T record);
說明:保存一個實體,null的屬性也會保存,不會使用數據庫默認值
方法: int insertSelective(T record);
說明:保存一個實體,null的屬性不會保存,會使用數據庫默認值
Update
方法: int updateByPrimaryKey(T record);
說明:根據主鍵更新實體全部字段,null值會被更新
方法: int updateByPrimaryKeySelective(T record);
說明:根據主鍵更新屬性不爲null的值
Delete
方法: int delete(T record);
說明:根據實體屬性作爲條件進行刪除,查詢條件使用等號
方法: int deleteByPrimaryKey(Object key);
說明:根據主鍵字段進行刪除,方法參數必須包含完整的主鍵屬性
Example方法
方法: List<T> selectByExample(Object example);
說明:根據Example條件進行查詢
重點:這個查詢支持通過 Example 類指定查詢列,通過 selectProperties 方法指定查詢列
方法: int selectCountByExample(Object example);
說明:根據Example條件進行查詢總數
方法: int updateByExample(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體 record 包含的全部屬性,null值會被更新
方法: int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體 record 包含的不是null的屬性值
方法: int deleteByExample(Object example);
說明:根據Example條件刪除數據
代碼中使用
在 service 中注入 dao ,即可使用。
@Service
@Transactional
public class BasDictServiceImpl extends BaseServiceImpl<BasDict>implements BasDictService {
@Autowired
private BasDictMapper basDictMapper;
}
總結
通用Mapper的原理是通過反射獲取實體類的信息,構造出相應的SQL,因此我們只需要維護好實體類即可,對於應付複雜多變的需求提供了很大的便利。上文敘述的只是通用Mapper的簡單用法,在實際項目中,還是要根據業務,在通用Mapper的基礎上封裝出粒度更大、更通用、更好用的方法。
@GeneratedValue(strategy = GenerationType.IDENTITY)
這個註解適用於主鍵自增的情況,支持下面這些數據庫:
- DB2:
VALUES IDENTITY_VAL_LOCAL()
- MYSQL:
SELECT LAST_INSERT_ID()
- SQLSERVER:
SELECT SCOPE_IDENTITY()
- CLOUDSCAPE:
VALUES IDENTITY_VAL_LOCAL()
- DERBY:
VALUES IDENTITY_VAL_LOCAL()
- HSQLDB:
CALL IDENTITY()
- SYBASE:
SELECT @@IDENTITY
- DB2_MF:
SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
- INFORMIX:
select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
- JDBC:這會令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法來取出由數據庫內部生成的主鍵(比如:像 MySQL 和 SQL Server 這樣的關係數據庫管理系統的自動遞增字段)。
使用GenerationType.IDENTITY
需要在全局配置中配置IDENTITY
的參數值,並且需要根據數庫配置ORDER
屬性。
舉例如下:
//不限於@Id註解的字段,但是一個實體類中只能存在一個(繼承關係中也只能存在一個)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
對應的XML形式爲:
<insert id="insertAuthor">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
注意<selectKey>
中的內容就是IDENTITY
參數值對應數據庫的SQL
3.@GeneratedValue(generator = "UUID")
//可以用於任意字符串類型長度超過32位的字段
@GeneratedValue(generator = "UUID")
private String username;
該字段不會回寫。這種情況對應類似如下的XML:
<insert id="insertAuthor">
<bind name="username_bind" value='@java.util.UUID@randomUUID().toString().replace("-", "")' />
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username_bind}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
注意:這種方式不能回寫
關於oracle序列:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "select SEQ_ID.nextval from dual")
private Integer id;
使用Oracle序列的時候,還需要配置:
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yuan.dao" />
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
ORDER=BEFORE
</value>
</property>
</bean>
因爲在插入數據庫前,需要先獲取到序列值,否則會報錯。
這種情況對於的xml類似下面這樣:
<insert id="insertAuthor">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select SEQ_ID.nextval from dual
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
不存在與數據庫的屬性用@Transient 註解表示