1. 配置信息
maven pom文件中增加依賴:
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
<!--persistence依賴-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
在application.properties配置文件中配置
(1)開啟mybatis的駝峰轉(zhuǎn)換
mybatis.configuration.mapUnderscoreToCamelCase=true
(2)配置mybatis的通用mappers路徑
mapper.mappers=com.server.common.mapper.CommonMapper
(3)配置mybatis的通用mapper的主鍵生成方式
mapper.identity=MYSQL
2. Application啟動類中引入包變換
-
配置MapperScan的導(dǎo)入包時不要引用
import org.mybatis.spring.annotation.MapperScan
要引用
tk.mybatis.spring.annotation.MapperScan
3. Pojo實體類配置
Pojo中所有字段類型要寫成封裝類的類型座硕,即long類型的數(shù)據(jù)要寫成Long奄容。
-
數(shù)據(jù)庫中數(shù)據(jù)表的表名默認(rèn)使用類名旺订,駝峰轉(zhuǎn)換會自動將UserInfo轉(zhuǎn)換為user_info表名讹弯,若不使用默認(rèn)的對應(yīng)類名與表名的設(shè)置,則需要使用 @Table 為實體類Pojo配置對應(yīng)于Mysql數(shù)據(jù)庫中的數(shù)據(jù)表名亲铡。 比如MYSQL中有個user表侦副,要在類前面配置表名。
@Table(name = "user") public class UserPojo {}
-
將主鍵設(shè)置為Id 并且設(shè)置主鍵的自增方式
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long userId;
-
數(shù)據(jù)表中字段名默認(rèn)使用Pojo中屬性名的駝峰轉(zhuǎn)下劃線形式液兽,如userName屬性會對應(yīng)數(shù)據(jù)表中user_name字段名,如果不采用默認(rèn)方式來命名Pojo中屬性名掌动,則需要使用 @Column 來指定對應(yīng)的數(shù)據(jù)表中的字段名四啰。
@Column(name = "user_name") private Long userFullNameInfo;
在數(shù)據(jù)庫的數(shù)據(jù)表中不存在的字段,要用 @Transient 注解標(biāo)識粗恢,否則mybatis在生成動態(tài)sql語句時會因為數(shù)據(jù)庫表中無對應(yīng)字段而報錯柑晒。
4. 定義通用Mapper接口類
自己定義通用Mapper接口,該接口繼承自Mybatis的Mapper接口和MySqlMapper接口:
server.common.mapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface CommonMapper<T> extends Mapper<T>,MySqlMapper<T>{
}
注意:通用Mapper不能和業(yè)務(wù)Mapper放在同一目錄下眷射,會報錯(錯誤信息未記錄)
5. 定義業(yè)務(wù)Mapper接口匙赞,使用通用Mapper
繼承通用Mapper時,必須指定明確的泛型類型凭迹。
public interface UserMapper extends CommonMapper<UserPojo> {}
6. 在代碼中調(diào)用通用mapper的數(shù)據(jù)庫操作方法
通用Mapper中提供的方法傳入的參數(shù)一般是Pojo對象罚屋,實際的參數(shù)封裝在Pojo對象內(nèi)部,因此需要將Pojo中所有屬性的類型定義為對象類型嗅绸。
@Autowired
UserMapper userMapper
userMapper.insert(userPojo)
7. 通用Mapper的所有通用方法介紹
一、select
(1) List<T> select (T record)
說明: 根據(jù)實體中屬性值查詢撕彤,查詢條件使用等號鱼鸠,所根據(jù)的屬性值set進(jìn)record對象中。
(2) T selectByPrimaryKey(Object key)
說明:根據(jù)主鍵字段進(jìn)行查詢羹铅,方法參數(shù)必須包含完整的主鍵屬性蚀狰,查詢條件使用等號,主鍵set進(jìn)對應(yīng)的實體對象中。
(3) List<T> selectAll()
說明:查詢?nèi)拷Y(jié)果职员,select(null)方法能達(dá)到同樣的效果麻蹋。
(4) T selectOne(T record)
說明:根據(jù)實體中的屬性進(jìn)行查詢,只能有一個返回值焊切,有多個結(jié)果是拋出異常扮授,查詢條件使用等號,屬性值set進(jìn)record對象中专肪。
(5) int selectCount(T record)
說明:根據(jù)實體中的屬性查詢總數(shù),查詢條件使用等號,屬性值set進(jìn)record實體對象中催蝗。
二酥泞、Insert
(1) int insert(T record)
說明:保存一個實體,null的屬性也會保存,不會使用數(shù)據(jù)庫默認(rèn)值乏梁。
(2) int insertSelective(T record)
說明:保存一個實體次洼,null的屬性不會保存,會使用數(shù)據(jù)庫默認(rèn)值遇骑。
三滓玖、Update
(1) int updateByPrimaryKey(T record)
說明:根據(jù)主鍵更新實體全部字段,null值會被更新质蕉。主鍵值被set進(jìn)record對象势篡。
(2) int updateByPrimaryKeySelective(T record)
說明:根據(jù)主鍵更新屬性不為null的值,主鍵值被set進(jìn)record對象模暗。
四禁悠、 Delete
(1) int delete(T record)
說明:根據(jù)實體屬性作為條件進(jìn)行刪除,查詢條件使用等號兑宇,屬性值set進(jìn)record對象碍侦。
(2) int deleteByPrimaryKey(Object key)
說明:根據(jù)主鍵字段進(jìn)行刪除,方法參數(shù)必須包含完整的主鍵屬性,主鍵值set進(jìn)key對象中隶糕。
五瓷产、 Example方法
下面給出一個根據(jù)componentInfoIds列表批量查詢的例子:
Example modelExample = new Example(ComponentGenerateReflectPojo.class);
Example.Criteria modelCriteria = modelExample.createCriteria();
modelCriteria.andIn("resultComponentInfoId", componentInfoIds);
List<ComponentGenerateReflectPojo> modelComponentGenerateReflects = generateReflectMapper.selectByExample(modelExample);
(1)List<T> selectByExample(Object example)
說明:根據(jù)Example條件進(jìn)行查詢
重點:這個查詢支持通過Example類指定查詢列,通過selectProperties方法指定查詢列
(2)int selectCountByExample(Object example)
說明:根據(jù)Example條件進(jìn)行查詢總數(shù)
(3)int updateByExample(@Param("record") T record, @Param("example") Object example)
說明:根據(jù)Example條件更新實體record包含的全部屬性枚驻,null值會被更新
(4)int updateByExampleSelective(@Param("record") T record, @Param("example") Object example)
說明:根據(jù)Example條件更新實體record包含的不是null的屬性值
(5)int deleteByExample(Object example)
說明:根據(jù)Example條件刪除數(shù)據(jù)
六濒旦、 獲取insert之后的自增主鍵
連接postgresql數(shù)據(jù)庫時,要獲取insert一條記錄后該記錄對應(yīng)的自增主鍵再登,則只需要在pojo類的主鍵字段前增加注解:
@Id
@GeneratedValue(strategy = GenerationType.Identity, generator = "select currval('id'::regclass)")
@Column(insertable = false)
private Long id;
注解@Id用來指明該字段對應(yīng)數(shù)據(jù)表中的主鍵尔邓;
注解@Column用來指明進(jìn)行insert的pojo中該字段不需要set值
注解@GeneratedValue 參數(shù)一指明主鍵產(chǎn)生方式是自增,參數(shù)二指明返回該記錄對應(yīng)的當(dāng)前主鍵信息锉矢。其中regeclass前對應(yīng)的值在數(shù)據(jù)庫對應(yīng)的數(shù)據(jù)表屬性中獲取梯嗽。
記錄對應(yīng)的主鍵信息在完成通用mapper的insert操作后被set進(jìn)參數(shù)pojo中。