馃殌 jdbc-plus绠€浠?/p>
馃殌 jdbc-plus鏄竴娆惧熀浜嶫dbcTemplate澧炲己宸ュ叿鍖咃紝 鍩轰簬JdbcTemplate宸插疄鐜板垎椤点€佸绉熸埛绛夋彃浠讹紝鍙嚜瀹氫箟鎵╁睍鎻掍欢銆傞」鐩湴鍧€锛?https://github.com/deeround/jdbc-plus
鐗规€э細
浣跨敤绠€鍗曪紝瀵逛唬鐮佸叆渚靛緢灏?/p>
鍙嚜瀹氫箟浠绘剰鎵╁睍鎻掍欢
澶氱鎴峰弬鑰僲ybatis-plus鍐呯疆澶氱鎴风殑瀹炵幇鍘熺悊锛岀悊璁轰笂涓巑ybatis-plus澶氱鎴锋彃浠舵敮鎸佸害涓€鏍?/p>
鍒嗛〉鎻掍欢鍙傝€働ageHelper鐨勫疄鐜板師鐞嗭紝浣跨敤绠€鍗曪紝瀵逛唬鐮佸叆渚佃緝灏忥紝杩樺彲浠ユ敞鍐屼笉鏀寔鐨勬暟鎹簱
鍏嶈垂寮€婧愶紝鍙换鎰忎娇鐢ㄤ慨鏀逛唬鐮?/p>
蹇€熷紑濮?/p>
寮曞叆jdbc-plus-spring-boot-starter
<dependency>
<groupId>com.github.deeround</groupId>
<artifactId>jdbc-plus-spring-boot-starter</artifactId>
<version>${version}</version>
</dependency>
娉ㄥ叆闇€瑕佷娇鐢ㄧ殑鎻掍欢
@Configuration
publicclassJdbcPlusConfig{
/**
* PaginationInterceptor鏄唴缃殑鍒嗛〉鎻掍欢锛堝垎椤垫彃浠朵竴瀹氳娉ㄥ叆鍦═enantLineHandler涔嬪悗锛屽彲浠ラ€氳繃Order鏉ユ帶鍒堕『搴忥級
*/
@Bean
@Order(9)
publicIInterceptorpaginationInterceptor() {
returnnewPaginationInterceptor();
聽聽 }
/**
* TenantLineHandler鏄唴缃殑澶氱鎴锋彃浠舵彃浠?/p>
*/
@Bean
@Order(1)
publicIInterceptortenantLineInterceptor() {
returnnewTenantLineInterceptor(newTenantLineHandler() {
/**
* 褰撳墠绉熸埛ID
*/
@Override
publicExpressiongetTenantId() {
StringcurrentTenantId="test_tenant_1";//鍙互浠庤姹備笂涓嬫枃涓幏鍙栵紙cookie銆乻ession銆乭eader绛夛級
returnnewStringValue(currentTenantId);
聽 聽 聽 聽 聽聽 }
/**
* 绉熸埛瀛楁鍚?/p>
*/
@Override
publicStringgetTenantIdColumn() {
return"tenant_id";
聽 聽 聽 聽 聽聽 }
/**
* 鏍规嵁琛ㄥ悕鍒ゆ柇鏄惁蹇界暐鎷兼帴澶氱鎴锋潯浠?/p>
*/
@Override
publicbooleanignoreTable(StringtableName) {
returnTenantLineHandler.super.ignoreTable(tableName);
聽 聽 聽 聽 聽聽 }
聽 聽 聽聽 });
聽聽 }
}
澶氱鎴锋彃浠?/p>
娉ㄥ叆澶氱鎴锋彃浠?/p>
/**
* TenantLineHandler鏄唴缃殑澶氱鎴锋彃浠舵彃浠?/p>
*/
@Bean
@Order(1)
publicIInterceptortenantLineInterceptor() {
returnnewTenantLineInterceptor(newTenantLineHandler() {
/**
* 褰撳墠绉熸埛ID
*/
@Override
publicExpressiongetTenantId() {
StringcurrentTenantId="test_tenant_1";//鍙互浠庤姹備笂涓嬫枃涓幏鍙栵紙cookie銆乻ession銆乭eader绛夛級
returnnewStringValue(currentTenantId);
聽 聽 聽 聽 聽聽 }
/**
* 绉熸埛瀛楁鍚?/p>
*/
@Override
publicStringgetTenantIdColumn() {
return"tenant_id";
聽 聽 聽 聽 聽聽 }
/**
* 鏍规嵁琛ㄥ悕鍒ゆ柇鏄惁蹇界暐鎷兼帴澶氱鎴锋潯浠?/p>
*/
@Override
publicbooleanignoreTable(StringtableName) {
returnTenantLineHandler.super.ignoreTable(tableName);
聽 聽 聽 聽 聽聽 }
聽 聽 聽聽 });
聽聽 }
service灞傛墽琛孲QL鏃惰嚜鍔ㄦ坊鍔犵鎴峰瓧娈?/p>
@Autowired
JdbcTemplatejdbcTemplate;
publicvoidinsert() {
this.jdbcTemplate.update("insert into test_user(id,name) values('1','wangwu')");
//鏈€缁堟墽琛孲QL锛歩nsert into test_user(id,name,tenant_id) values('1','wangwu','test_tenant_1')
聽聽 }
publicvoiddelete() {
this.jdbcTemplate.update("delete from test_user");
//鏈€缁堟墽琛孲QL锛歞elete from test_user where tenant_id='test_tenant_1'
聽聽 }
publicvoidupdate() {
this.jdbcTemplate.update("update test_user set name='lisi' where id='1'");
//鏈€缁堟墽琛孲QL锛歶pdate test_user set name='lisi' where id='1' and tenant_id='test_tenant_1'
聽聽 }
publicList<Map<String,Object>>query() {
returnthis.jdbcTemplate.queryForList("select * from test_user");
//鏈€缁堟墽琛孲QL锛歴elect * from test_user where tenant_id='test_tenant_1'
聽聽 }
鍒嗛〉鎻掍欢
娉ㄥ叆鍒嗛〉鎻掍欢
/**
* PaginationInterceptor鏄唴缃殑鍒嗛〉鎻掍欢锛堝垎椤垫彃浠朵竴瀹氳娉ㄥ叆鍦═enantLineHandler涔嬪悗锛屽彲浠ラ€氳繃Order鏉ユ帶鍒堕『搴忥級
*/
@Bean
@Order(9)
publicIInterceptorpaginationInterceptor() {
returnnewPaginationInterceptor();
聽聽 }
service灞傛墽琛孲QL鏃惰嚜鍔ㄥSQL杩涜鍒嗛〉鏌ヨ
@Autowired
JdbcTemplatejdbcTemplate;
publicPageInfo<Map<String,Object>>page1() {
PageHelper.startPage(1,2);
List<Map<String,Object>>list=this.jdbcTemplate.queryForList("select * from test_user");//鏈€缁堟墽琛孲QL锛歴elect * from test_user LIMIT 0锛?
PageInfo<Map<String,Object>>page=newPageInfo<>(list);//PageInfo瀵硅薄鍖呭惈浜嗗垎椤典俊鎭紙鎬昏鏁扮瓑锛?/p>
returnpage;
聽聽 }
publicPageInfo<Map<String,Object>>page2() {
PageHelper.startPage(2,2);
List<Map<String,Object>>list=this.jdbcTemplate.queryForList("select * from test_user");//鏈€缁堟墽琛孲QL锛歴elect * from test_user LIMIT 2锛?
PageInfo<Map<String,Object>>page=newPageInfo<>(list);//PageInfo瀵硅薄鍖呭惈浜嗗垎椤典俊鎭紙鎬昏鏁扮瓑锛?/p>
returnpage;
聽聽 }
鑷畾涔夊垎椤?/p>
褰撴彃浠朵笉鏀寔鐨勬暟鎹簱鍒嗛〉锛屽彲浠ラ€氳繃PageHelper.registerDialectAlias(String alias, Class clazz)娉ㄥ唽涓€涓嚜宸卞垎椤靛疄鐜扮被鍗冲彲锛屼篃鍙互瑕嗙洊宸叉敮鎸佺殑鏁版嵁搴撳垎椤点€?/p>
鑷畾涔夋彃浠?/p>
绀轰緥锛氬啓涓€涓墦鍗癝QL璇彞銆佹墽琛屽弬鏁般€佷互鍙婃墽琛孲QL鑰楁椂鐨勭洃鎺ф彃浠躲€?/p>
缂栧啓MyStatInterceptor鎻掍欢
/**
* SQL鐩戞帶鎻掍欢
*/
@Slf4j
publicclassMyStatInterceptorimplementsIInterceptor{
/**
* 鑷畾涔夋彃浠舵槸鍚︽敮鎸?/p>
*/
@Override
publicbooleansupportMethod(finalMethodInvocationInfomethodInfo) {
returnIInterceptor.super.supportMethod(methodInfo);
聽聽 }
/**
* SQL鎵ц鍓嶆柟娉曪紙涓昏鐢ㄤ簬瀵筍QL杩涜淇敼锛?/p>
*/
@Override
publicvoidbeforePrepare(finalMethodInvocationInfomethodInfo,JdbcTemplatejdbcTemplate) {
log.info("鍘熷SQL锛歿}",methodInfo.getSql());
log.info("鍏ュ弬锛歿}",Arrays.toString(methodInfo.getArgs()));
log.info("鎵цSQL寮€濮嬫椂闂达細{}",LocalDateTime.now());
methodInfo.getUserAttributes().put("startTime",LocalDateTime.now());
聽聽 }
/**
* SQL鎵ц瀹屾垚鍚庢柟娉曪紙涓昏鐢ㄤ簬瀵硅繑鍥炲€间慨鏀癸級
*
* @param result 鍘熷杩斿洖瀵硅薄
* @return 澶勭悊鍚庣殑杩斿洖瀵硅薄
*/
@Override
publicObjectbeforeFinish(Objectresult,finalMethodInvocationInfomethodInfo,JdbcTemplatejdbcTemplate) {
log.info("鎵цSQL缁撴潫鏃堕棿锛歿}",LocalDateTime.now());
LocalDateTimestartTime=(LocalDateTime)methodInfo.getUserAttributes().get("startTime");
log.info("鎵цSQL鑰楁椂锛歿}姣",Duration.between(startTime,LocalDateTime.now()).toMillis());
returnresult;
聽聽 }
}
娉ㄥ叆鑷畾涔夋彃浠?/p>
/**
* 鑷畾涔夋彃浠舵敞鍏ワ紝娉ㄥ叆浣嶇疆鎸夊疄闄呮儏鍐?/p>
*/
@Bean
@Order(0)
publicIInterceptormyStatInterceptor() {
returnnewMyStatInterceptor();
聽聽 }
鏌ョ湅鏁堟灉锛堟煡鐪嬫墦鍗版棩蹇楋級
c.g.d.j.p.s.config.MyStatInterceptor 聽 聽 : 鍘熷SQL锛歴elect * from test_user
c.g.d.j.p.s.config.MyStatInterceptor 聽 聽 : 鍏ュ弬锛歔select * from test_user]
c.g.d.j.p.s.config.MyStatInterceptor 聽 聽 : 鎵цSQL寮€濮嬫椂闂达細2023-04-23T16:35:58.151
c.g.d.j.p.s.config.MyStatInterceptor 聽 聽 : 鎵цSQL缁撴潫鏃堕棿锛?023-04-23T16:35:58.655
c.g.d.j.p.s.config.MyStatInterceptor 聽 聽 : 鎵цSQL鑰楁椂锛?03姣
鈽?楦h阿 鈽?/p>
https://github.com/baomidou/mybatis-plus
https://github.com/pagehelper/Mybatis-PageHelper
https://github.com/deeround/jdbc-plus