当前位置: 首页>后端>正文

jdbc-plus是一款基于JdbcTemplate增强工具包, 基于JdbcTemplate已实现分页、多租户等插件,可自定义扩展插件

馃殌 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


https://www.xamrdz.com/backend/3fc1936950.html

相关文章: