Posted SpringBoot
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
璇存槑
MyBatis鐗堟湰锛?.5.1
鐩稿叧鍘嗗彶鏂囩珷锛?/strong>闃呰鏈枃涔嬪墠锛屾偍鍙兘闇€瑕佸厛鐪嬩笅涔嬪墠鐨勭郴鍒?/strong>馃憞锛?/strong>
鍓嶈█
杩愮甯峰箘涔嬩腑锛屽喅鑳滃崈閲屼箣澶栵紝鏄鍋氫釜浜嗙粨浜嗭紝鎶婁綘鐨勫浼欐帍鍑烘潵锛屼笂鍒哄垁銆?/span>
涓婁竴绡囧凡缁忚兘澶熶娇鐢⊿qlSession杩涜鏌ヨ杩斿洖缁撴灉浜嗐€傝繖涓€绡囨垜浠氨鏄姞鍏ョ憺澹啗鍒€Mapper銆?/span>
涓€銆佸垎鏋?/span>
鍦⊿qlSession浼氭彁渚沢etMapper鐨勬柟娉曪紝鍦―efaultSqlSession浼氫娇鐢≒roxy瀹炰緥鍖栦竴涓狹apperProxy浠g悊锛岃€孧apperProxy浠g悊浼氳幏鍙朣qlSession锛屽湪杩欓噷杩涜Sql鐨勬搷浣滐紝鐒跺悗缁撴灉銆?/span>
浜屻€佺紪鐮?/span>
2.1 MapperProxy
MapperProxy鏄痬apper鏈€缁堟墽琛岀殑鏍稿績锛?/span>
package com.kfit.mybatis.session.impl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collection;
import com.kfit.mybatis.session.SqlSession;
public class MapperProxy implements InvocationHandler{
private SqlSession sqlSession;
public MapperProxy(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String statement = method.getDeclaringClass().getName()+"."+method.getName();
//isAssignableFrom鏂规硶鏄垽鏂槸鍚︿负鏌愪釜绫荤殑鐖剁被
if(Collection.class.isAssignableFrom(method.getReturnType())) {
//杩斿洖鍊兼槸闆嗗悎鐨勮瘽锛岄偅涔堟槸璋冪敤selectList
return sqlSession.selectList(statement,null==args?null:args[0]);
}else {
return sqlSession.selectOne(statement,null==args?null:args[0]);
}
}
}
璇存槑锛?/span>
锛?锛夌敱浜嶮apperProxy鏄竴涓唬鐞嗙被锛屾墍浠ラ渶瑕佸疄鐜版帴鍙nvocationHandler鐨処nvoke鏂规硶銆?/span>
锛?锛夊湪Invoke鏂规硶涓洿鎺ヤ娇鐢⊿qlSession杩涜鎵ц锛岄偅涔堜富瑕佺殑鏍稿績灏辨槸瑕佸垽鏂叿浣撴墽琛屼粈涔堟柟娉曪紝杩欓噷鐜板湪閫氳繃杩斿洖鍊兼槸鍚︽槸闆嗗悎鏉ュ垽鏂槸鍚︽槸鎵цselectOne杩樻槸SelectList銆?/span>
2.2 SqlSession
鍦⊿qlSession涓坊鍔爂etMapper鏂规硶锛?/span>
public interface SqlSession {
<T> T selectOne(String statement, Object parameter);
<E> List<E> selectList(String statement);
<E> List<E> selectList(String statement, Object parameter);
<T> T getMapper(Class<T> type);
}
鍦―efaultSqlSession涓繘琛屽疄鐜癵etMapper鏂规硶锛?/span>
@SuppressWarnings("unchecked")
public <T> T getMapper(Class<T> type) {
T newProxyInstance = (T) Proxy.newProxyInstance(type.getClassLoader(),new Class[]{type},new MapperProxy(this));
return newProxyInstance;
}
2.3 娴嬭瘯涓?/span>
濂戒簡鍐欐浠g爜娴嬭瘯涓嬪惂锛?/span>
public static void main(String[] args) {
String resource = "mybatis-config.xml";
InputStream inputStream = App.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
System.out.println(sqlSessionFactory);
System.out.println(sqlSessionFactory.getConfiguration().getJdbcProperties().getUrl());
SqlSession sqlSession = sqlSessionFactory.openSession();
Demo demo = null;
List<Demo> demos = null;
//浣跨敤Mapper
DemoMapper demoMapper = sqlSession.getMapper(DemoMapper.class);
demo = demoMapper.getById(1);
System.out.println(demo);
demos = demoMapper.getAll();
System.out.println(demos);
}
杩愯鐪嬩笅缁撴灉锛?/span>
Demo [id=1,name=寮犱笁1]
[Demo [id=1, name=寮犱笁1], Demo [id=9, name=寮犱笁], Demo [id=10, name=寮犱笁], Demo [id=11, name=寮犱笁], Demo [id=12, name=寮犱笁], Demo [id=13, name=寮犱笁]]
寰堟槑鏄炬墽琛岀殑缁撴灉鍜岀洿鎺ヤ娇鐢⊿qlSession璋冪敤鐨勭粨鏋滄槸涓€鏍风殑銆?/span>
濂戒簡鏈夊叧鎵嬪啓MyBatis鐨勬枃绔犲氨鍏堝埌姝ゅ憡涓€娈佃惤浜嗭紝閫氳繃鎵嬪啓mybatis锛屾兂蹇呭ぇ瀹跺浜嶮yBatis鐨勮鐭ュ張楂樹簡涓€涓瓑绾т簡銆?/span>
鎴戝氨鏄垜锛屾槸棰滆壊涓嶄竴鏍风殑鐑熺伀銆?br class="mq-177">鎴戝氨鏄垜锛屾槸涓庝紬涓嶅悓鐨勫皬鑻规灉銆?br class="mq-178">
脿鎮熺┖瀛﹂櫌锛?span class="mq-182">http://t.cn/Rg3fKJD
瀛﹂櫌涓湁Spring Boot鐩稿叧鐨勮绋嬶紒鐐瑰嚮銆?/span>闃呰鍘熸枃銆嶈繘琛屾煡鐪嬶紒
SpringBoot瑙嗛锛歨ttp://t.cn/R3QepWG
Spring Cloud瑙嗛锛歨ttp://t.cn/R3QeRZc
SpringBoot Shiro瑙嗛锛歨ttp://t.cn/R3QDMbh
SpringBoot浜ゆ祦骞冲彴锛歨ttp://t.cn/R3QDhU0
SpringData鍜孞PA瑙嗛锛歨ttp://t.cn/R1pSojf
SpringSecurity5.0瑙嗛锛歨ttp://t.cn/EwlLjHh
Sharding-JDBC鍒嗗簱鍒嗚〃瀹炴垬锛歨ttp://t.cn/E4lpD6e
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章