生成文件放到項(xiàng)目
Github地址
用工具把數(shù)據(jù)庫里面的表生成對(duì)應(yīng)的文件放到項(xiàng)目里
SpringBoot集成通用mapper
pom.xml添加依賴
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.7</version>
</dependency>
特別注意藤为,如果使用了1.2.0以上版本 @MapperScan 注解,請(qǐng)使用 tk.mybatis.spring.annotation.MapperScan 注解抑进。
application.yml文件配置
mapper:
mappers: tk.mybatis.mapper.common.Mapper
not-empty: false
identity: MYSQL
style: camelhump
可配置參數(shù)介紹
- UUID:設(shè)置生成UUID的方法,需要用OGNL方式配置,不限制返回值汞舱,但是必須和字段類型匹配
- 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:這會(huì)令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法來取出由數(shù)據(jù)庫內(nèi)部生成的主鍵(比如:像 MySQL 和 SQL Server 這樣的關(guān)系數(shù)據(jù)庫管理系統(tǒng)的自動(dòng)遞增字段)。
- ORDER:<selectKey>中的order屬性宗雇,可選值為BEFORE和AFTER
- catalog:數(shù)據(jù)庫的catalog昂芜,如果設(shè)置該值,查詢的時(shí)候表名會(huì)帶catalog設(shè)置的前綴
- schema:同catalog赔蒲,catalog優(yōu)先級(jí)高于schema
- seqFormat:序列的獲取規(guī)則,使用{num}格式化參數(shù)泌神,默認(rèn)值為{0}.nextval,針對(duì)Oracle舞虱,可選參數(shù)一共4個(gè)欢际,對(duì)應(yīng)0,1,2,3分別為SequenceName,ColumnName, PropertyName矾兜,TableName
- notEmpty:insert和update中损趋,是否判斷字符串類型!='',少數(shù)方法會(huì)用到
- style:實(shí)體和表轉(zhuǎn)換時(shí)的規(guī)則椅寺,默認(rèn)駝峰轉(zhuǎn)下劃線浑槽,可選值為normal用實(shí)體名和字段名;camelhump是默認(rèn)值,駝峰轉(zhuǎn)下劃線;uppercase轉(zhuǎn)換為大寫;lowercase轉(zhuǎn)換為小寫
- enableMethodAnnotation:可以控制是否支持方法上的JPA注解返帕,默認(rèn)false桐玻。
使用通用Mapper
繼承通用的Mapper<T>必須指定泛型
例:
泛型實(shí)體類必須符合要求
實(shí)體類按照如下規(guī)則和數(shù)據(jù)庫表進(jìn)行轉(zhuǎn)換,注解全部是JPA中的注解:
- 1.表名默認(rèn)使用類名,駝峰轉(zhuǎn)下劃線(只對(duì)大寫字母進(jìn)行處理),如TestUser默認(rèn)對(duì)應(yīng)的表名為test_user
- 2.表名可以使用@Table(name = "tableName")進(jìn)行指定,對(duì)不符合第一條默認(rèn)規(guī)則的可以通過這種方式指定表名.
- 3.字段默認(rèn)和@Column一樣,都會(huì)作為表字段,表字段默認(rèn)為Java對(duì)象的Field名字駝峰轉(zhuǎn)下劃線形式.
- 4.可以使用@Column(name = "fieldName")指定不符合第3條規(guī)則的字段名
- 5.使用@Transient注解可以忽略字段,添加該注解的字段不會(huì)作為表字段使用.
- 6.建議一定是有一個(gè)@Id注解作為主鍵的字段,可以有多個(gè)@Id注解的字段作為聯(lián)合主鍵.
- 7.默認(rèn)情況下,實(shí)體類中如果不存在包含@Id注解的字段,所有的字段都會(huì)作為主鍵字段進(jìn)行使用(這種效率極低).
- 8.實(shí)體類可以繼承使用,可以參考測(cè)試代碼中的tk.mybatis.mapper.model.UserLogin2類.
- 9.由于基本類型,如int作為實(shí)體類字段時(shí)會(huì)有默認(rèn)值0,而且無法消除,所以實(shí)體類中建議不要使用基本類型.
- 10.@NameStyle注解,用來配置對(duì)象名/字段和表名/字段之間的轉(zhuǎn)換方式荆萤,該注解優(yōu)先于全局配置style镊靴,可選值:
normal:使用實(shí)體類名/屬性名作為表名/字段名
camelhump:這是默認(rèn)值,駝峰轉(zhuǎn)換為下劃線形式
uppercase:轉(zhuǎn)換為大寫
lowercase:轉(zhuǎn)換為小寫
重點(diǎn)強(qiáng)調(diào) @Transient 注解 許多人由于不仔細(xì)看文檔链韭,頻繁在這個(gè)問題上出錯(cuò)偏竟。如果你的實(shí)體類中包含了不是數(shù)據(jù)庫表中的字段,你需要給這個(gè)字段加上@Transient注解敞峭,這樣通用Mapper在處理單表操作時(shí)就不會(huì)將標(biāo)注的屬性當(dāng)成表字段處理踊谋!
主鍵策略(僅用于insert方法)
通用Mapper還提供了序列(支持Oracle)、UUID(任意數(shù)據(jù)庫,字段長度32)儡陨、主鍵自增(類似Mysql,Hsqldb)三種方式褪子,其中序列和UUID可以配置多個(gè),主鍵自增只能配置一個(gè)骗村。
- 1.@GeneratedValue(generator = "JDBC")
@Id
@GeneratedValue(generator = "JDBC")
private Integer id;
這會(huì)令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法來取出由數(shù)據(jù)庫內(nèi)部生成的主鍵(比如:像 MySQL 和 SQL Server 這樣的關(guān)系數(shù)據(jù)庫管理系統(tǒng)的自動(dòng)遞增字段)
- 2.@GeneratedValue(strategy = GenerationType.IDENTITY)
- 這個(gè)注解適用于主鍵自增的情況嫌褪,支持下面這些數(shù)據(jù)庫:
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
使用GenerationType.IDENTITY需要在全局配置中配置IDENTITY的參數(shù)值,并且需要根據(jù)數(shù)庫配置ORDER屬性胚股。
- 3.@GeneratedValue(generator = "UUID")
可以用于任意字符串類型長度超過32位的字段
@Id
@GeneratedValue(generator = "UUID")
private Integer id;
接口及使用大全
基礎(chǔ)接口
Insert
1.InsertMapper<T>
接口:InsertMapper<T>
方法:int insert(T record);
說明:保存一個(gè)實(shí)體笼痛,null的屬性也會(huì)保存,不會(huì)使用數(shù)據(jù)庫默認(rèn)值
public int insertTestUser(TestUser testUser) {
return testUserMapper.insert(testUser);
}
結(jié)果:
2.InsertSelectiveMapper<T>
接口:InsertSelectiveMapper<T>
方法:int insertSelective(T record);
說明:保存一個(gè)實(shí)體,null的屬性不會(huì)保存,會(huì)使用數(shù)據(jù)庫默認(rèn)值
結(jié)果:
請(qǐng)自行實(shí)驗(yàn)
Update
1.UpdateByPrimaryKeyMapper<T>
接口:UpdateByPrimaryKeyMapper<T>
方法:int updateByPrimaryKey(T record);
說明:根據(jù)主鍵更新實(shí)體全部字段,null值會(huì)被更新
結(jié)果:
會(huì)把沒有值的屬性變成空請(qǐng)自行實(shí)驗(yàn)
2.UpdateByPrimaryKeySelectiveMapper<T>
接口:UpdateByPrimaryKeySelectiveMapper<T>
方法:int updateByPrimaryKeySelective(T record);
說明:根據(jù)主鍵更新屬性不為null的值
public int updateTestUser() {
TestUser testUser=new TestUser();
testUser.setId("5f7139ef295d42a3b964c082e0dd838f");
testUser.setName("李四四");
return testUserMapper.updateByPrimaryKeySelective(testUser);
}
結(jié)果:
Delete
1.DeleteMapper<T>
接口:DeleteMapper<T>
方法:int delete(T record);
說明:根據(jù)實(shí)體屬性作為條件進(jìn)行刪除裹芝,查詢條件使用等號(hào)
public int deleteTestUser() {
TestUser testUser=new TestUser();
//根據(jù)屬性刪除會(huì)把所有密碼是123456的數(shù)據(jù)刪除
testUser.setPassword("123456");
return testUserMapper.delete(testUser);
}
結(jié)果:
四個(gè)已經(jīng)全部刪除
2.DeleteByPrimaryKeyMapper<T>
接口:DeleteByPrimaryKeyMapper<T>
方法:int deleteByPrimaryKey(Object key);
說明:根據(jù)主鍵字段進(jìn)行刪除,方法參數(shù)必須包含完整的主鍵屬性
public int deleteKeyTestUser() {
//根據(jù)主鍵ID刪除
return testUserMapper.deleteByPrimaryKey("5f7139ef295d42a3b964c082e0dd838f");
}
結(jié)果:
Select
1.SelectMapper<T>
接口:SelectMapper<T>
方法:List<T> select(T record);
說明:根據(jù)實(shí)體中的屬性值進(jìn)行查詢枷恕,查詢條件使用等號(hào)
public List<TestUser> selectTestUser() {
TestUser testUser=new TestUser();
testUser.setPassword("123456");
testUser.setUsername("lisi");
return testUserMapper.select(testUser);
}
結(jié)果:
2.SelectByPrimaryKeyMapper<T>
接口:SelectByPrimaryKeyMapper<T>
方法:T selectByPrimaryKey(Object key);
說明:根據(jù)主鍵字段進(jìn)行查詢,方法參數(shù)必須包含完整的主鍵屬性谭胚,查詢條件使用等號(hào)
結(jié)果:
根據(jù)主鍵查詢請(qǐng)自行實(shí)驗(yàn)
3.SelectAllMapper<T>
接口:SelectAllMapper<T>
方法:List<T> selectAll();
說明:查詢?nèi)拷Y(jié)果徐块,select(null)方法能達(dá)到同樣的效果
結(jié)果:
查詢所有請(qǐng)自行實(shí)驗(yàn)
4.SelectOneMapper<T>
接口:SelectOneMapper<T>
方法:T selectOne(T record);
說明:根據(jù)實(shí)體中的屬性進(jìn)行查詢,只能有一個(gè)返回值灾而,有多個(gè)結(jié)果是拋出異常胡控,查詢條件使用等號(hào)
public TestUser selectOneTestUser() {
TestUser testUser=new TestUser();
testUser.setUsername("wangwu");
//結(jié)果只能返回一條數(shù)據(jù)否則會(huì)拋出異常
return testUserMapper.selectOne(testUser);
}
結(jié)果:
5.SelectCountMapper<T>
接口:SelectCountMapper<T>
方法:int selectCount(T record);
說明:根據(jù)實(shí)體中的屬性查詢總數(shù),查詢條件使用等號(hào)
結(jié)果:
返回查詢個(gè)數(shù)請(qǐng)自行實(shí)驗(yàn)
Example 方法
Select 方法
1.SelectByExampleMapper<T>
接口:SelectByExampleMapper<T>
方法:List<T> selectByExample(Object example);
說明:根據(jù)Example條件進(jìn)行查詢
重點(diǎn):這個(gè)查詢支持通過Example類指定查詢列旁趟,通過selectProperties方法指定查詢列
public List<TestUser> selectExample() {
Example example = new Example(TestUser.class);
//排序方法setOrderByClause("字段名 ASC")DESC降序
example.setOrderByClause("name ASC");
example.createCriteria()
//添加xxx字段等于value條件
.andEqualTo("password","123456")
//模糊查詢xxx字段like value條件
.andLike("name","%四%")
//可以自由拼接SQL
//.andCondition("ID = '5f7139ef295d42a3b964c082e0dd838f' ")
//或者可以這么寫
.andCondition("ID =","5f7139ef295d42a3b964c082e0dd838f")
;
return testUserMapper.selectByExample(example);
}
實(shí)例解析:
mybatis的逆向工程中會(huì)生成實(shí)例及實(shí)例對(duì)應(yīng)的example昼激,example用于添加條件,相當(dāng)where后面的部分
Example example = new Example();
Criteria criteria = example.createCriteria();
方法 | 說明 |
---|---|
example.setOrderByClause(“字段名 ASC”) | 添加升序排列條件锡搜,DESC為降序 |
example.setDistinct(false) | 去除重復(fù)橙困,boolean型,true為選擇不重復(fù)的記錄余爆。 |
criteria.andIsNull("字段") | 添加字段xxx為null的條件 |
criteria.andIsNotNull("字段") | 添加字段xxx不為null的條件 |
criteria.andEqualTo("字段",value) | 添加xxx字段等于value條件 |
criteria.andNotEqualTo("字段",value) | 添加xxx字段不等于value條件 |
criteria.andGreaterThan("字段",value) | 添加xxx字段大于value條件 |
criteria.andGreaterThanOrEqualTo("字段",value) | 添加xxx字段大于等于value條件 |
criteria.andLessThan("字段",value) | 添加xxx字段小于value條件 |
criteria.andLessThanOrEqualTo("字段",value) | 添加xxx字段小于等于value條件 |
criteria.andIn("字段",List<?>) | 添加xxx字段值在List<?>條件 |
criteria.andNotIn("字段",List<?>) | 添加xxx字段值不在List<?>條件 |
criteria.andLike("字段",“%”+value+”%”) | 添加xxx字段值為value的模糊查詢條件 |
criteria.andNotLike("字段",“%”+value+”%”) | 添加xxx字段值不為value的模糊查詢條件 |
criteria.andBetween(value1,value2) | 添加xxx字段值在value1和value2之間條件 |
criteria.andNotBetween(value1,value2) | 添加xxx字段值不在value1和value2之間條件 |
criteria.andCondition("SQL") | 可以寫字符串拼接SQL |
criteria.andCondition("字段 =",value) | 前面可以寫SQL后面可以寫值 |
還有criteria.orxxxx的方法跟上面一樣這里不做解釋
2. SelectCountByExampleMapper<T>
接口:SelectCountByExampleMapper<T>
方法:int selectCountByExample(Object example);
說明:根據(jù)Example條件進(jìn)行查詢總數(shù)
查詢總數(shù)的方法跟上面的寫法一樣
Update 方法
UpdateByExampleMapper<T>
接口:UpdateByExampleMapper<T>
方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
說明:根據(jù)Example條件更新實(shí)體record包含的全部屬性纷宇,null值會(huì)被更新
UpdateByExampleSelectiveMapper<T>
接口:UpdateByExampleSelectiveMapper<T>
方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
說明:根據(jù)Example條件更新實(shí)體record包含的不是null的屬性值
Delete 方法
DeleteByExampleMapper<T>
接口:DeleteByExampleMapper<T>
方法:int deleteByExample(Object example);
說明:根據(jù)Example條件刪除數(shù)據(jù)
結(jié)語
- 更新這個(gè)文檔用的時(shí)間很長.終于寫完了,以后開發(fā)會(huì)提高不少效率夸盟。也可以把通用mapper整合到spring里詳細(xì)教程可以看
通用Mapper文檔
通用Mapper碼云文檔