1.å¦ä½å®ç°mybatisçsqlsessiontemplate
å¦ä½å®ç°mybatisçsqlsessiontemplate
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
//namespace+id
sqlSession.insert("cn.jarjar.dao.BlogMapper.insertBlog",源码发布作业 源码 blog);
sqlSession.commit(true)
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback(true);
} finally {
sqlSession.close();
}
ä¹å°±æ¯è¦ååå§çjava.sql.Connection对象ä¸æ ·ï¼å¿ é¡»æç §ï¼æ°å»ºè¿æ¥->æ§è¡SQL->æ交ï¼æ¥è¯¢ä¸éè¦ï¼->å¦ææä½æ°æ®åå¨å¼å¸¸éè¦åæ»->éæ¾æ°æ®åºè¿æ¥ã注æ第ä¸ç¹åæåä¸ç¹ï¼æ¯ä¸ªSqlSessionæ°å»ºä¹åå¿ é¡»éæ¾ï¼ä¸ç¶ä¼é ææ°æ®åºè¿æ¥æ³é²çå±é©ãä¹å°±æ¯æå³çSqlSessionæ¯ä¸ªæç¶æç对象ï¼æ¯æ æ³è¿è¡å¤ç¨çï¼æ以åªè½å±éäºrequestæè æ¹æ³çèå´ï¼ä¹å°±æ¯æè°ç线ç¨ä¸å®å ¨ã
ç°è±¡2ï¼å¦æ使ç¨springéæmybatisï¼å®æ¹æä¾äºæ´åå mybatis-spring.jarï¼å¦æå®æé ç½®ä¹åï¼ä½¿ç¨æ¹å¼åå ¶ç®åï¼ç®å示ä¾å¦ä¸ï¼
//æ³¨å ¥springä¸é ç½®çSqlSessionTemplate对象ï¼åä¾
@Resource(name="sqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate;
public void saveTestTrans(){
this.sqlSessionTemplate.selectList("testdomain.selectAnySql", "select * from my_blog where id='1'");
}
è¿éçSqlSessionTemplateä¸ä» æ¯åä¾çï¼èä¸ä¸éè¦æå·¥æ°å»ºåå ³éSqlSession
é®é¢1ï¼
é£ä¹é®é¢æ¥äºï¼ä¸ºä»ä¹mybatis-spring.jarä¸çSqlSessionTemplateå¯ä»¥è¢«å¤ä¸ªdaoå¤ç¨ï¼èä¸ä¸ä¼é ææ°æ®è¿æ¥æ³é²å¢ï¼å¹¶ä¸è¿å¯ä»¥èªå¨æ°å»ºåéæ¾æ°æ®åºè¿æ¥ï¼å®æ¹è§£çæ¯å 为SqlSessionTemplateæ¯çº¿ç¨å®å ¨çï¼ä¹å°±æ¯ç¡®ä¿æ¯ä¸ªçº¿ç¨ä½¿ç¨çsqlSessionçå¯ä¸å¹¶ä¸äºç¸å²çªã
é¦å çäºä¸ä¸mybatis-springçæºç ï¼åç°SqlSessionTemplateæ¯éè¿ä»£çæ¦æªåSqlSessionHolderå®ç°çsqlsession线ç¨å®å ¨åèªå¨æ°å»ºåéæ¾è¿æ¥çãçæé å½æ°å½æ°ä¸æ建代çç±»ï¼è¯¥ä»£çç±»å®ç°SqlSessionæ¥å£ï¼å®ä¹äºæ¹æ³æ¦æªå¨ï¼å¦æè°ç¨ä»£çç±»å®ä¾ä¸å®ç°SqlSessionæ¥å£å®ä¹çæ¹æ³ï¼è¯¥è°ç¨å被导åSqlSessionInterceptorçinvokeæ¹æ³ï¼è¿ä¸ªæ¹æ³ä¸èªå¨è¿è¡äºSqlSessionçèªå¨è¯·æ±åéæ¾ï¼å¦æä¸è¢«springæ管åèªå·±æ°å»ºåéæ¾sqlsessionï¼å¦æ被spring管çå使ç¨SqlSessionHolderè¿è¡requestårelaseæä½ï¼
以ä¸ç½åé对SqlSessionTemplateç线ç¨å®å ¨ç¹æ§è¿è¡äºè¯¦ç»çæ¢ç©¶ï¼blogs.com/daxin/p/.html
é®é¢2ï¼
ç¶ååæ³å°è¿æ ·ä¸ä¸ªé®é¢ï¼è½ç¶ç°å¨å ä¹ææ项ç®é½ä½¿ç¨springä½ä¸ºjavaç¨åºçåºæ¬æ¡æ¶ï¼å¦ææä¸ä½¿ç¨spring管çmybatisï¼ä» ä» ä½¿ç¨åå§çmybatisï¼æä¹æ ·æè½æ建ä¸ä¸ªåSqlSessionTemplateç¸ä¼¼ç对象å¢ï¼
é¦å æ³å°å¿ 须使ç¨javaçtreadLocalæ建ä¸ä¸ªsqlsessionç对象ï¼å¦ThreadLocal sqlSession = new ThreadLocal
()ã
ç»è¿æ¥æ¾ï¼åç°mybatisèªèº«å°±æè¿æ ·ä¸ä¸ªç±»å®ç°äºç±»ä¼¼çåè½ï¼ç±»è·¯å¾ï¼org.apache.ibatis.session.SqlSessionManagerï¼ä½æ¯æ²¡æ注éï¼å¯è½åå¨mybatis-springè¿ç§ç¥å¨ä¹åï¼mybatisæ¾å¼äºå¯¹è¿ä¸ªç±»çç»´æ¤ã
该类å®ç°äºSqlSessionFactory, SqlSession并ä¸å¨å ¶ä¸å®ä¹äºä¸ä¸ªtreadLocalçsqlssion对象ï¼åæ¶ä½¿ç¨äºä»£çæ¦æªè¿è¡äºsqlsessionçèªå¨ç®¡çï¼å ·ä½ä»£ç å¯ä»¥èªå·±æ¥é ï¼å¯¹äºç解mybatisåçåjavaç代çæºå¶å¾æ帮å©ã
é£ä¹å个ç®åçç¨åºéªè¯ä¸ä¸SqlSessionManageræ¯å¦ççå¯ä»¥ä¿è¯çº¿ç¨å®å ¨åèªå¨æ°å»ºåéæ¾sqlssionï¼TestSqlManager.java
private static SqlSession sqlSession;
public static SqlSession getSqlSessionTest(){
if(sqlSession == null){
//æ建使ç¨çSqlSessionFactory
SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
sqlSession = SqlSessionManager.newInstance(sqlSessionFactory);
}
return sqlSession;
}
public static void main(String[] args) throws InterruptedException {
Run run = new Run();
List
threads = new ArrayList
();
for (int i = 0; i < ; i++) {
Thread t = new Thread(run);
threads.add(t);
System.out.println("thread:{ "+t.getName()+"}, start");
t.start();
}
for (Thread t : threads) {
System.out.println("thread:{ "+t.getName()+"},join");
t.join();
}
}
ææ¬æºè£ çmysqlï¼éè¿çæ§è¯å¥ï¼select SUBSTRING_INDEX(host,â:â,1) as ip , count(*) from information_schema.processlist group by ip;åç°æ§è¡è¿ç¨ä¸åå¨è¿æ¥å¹¶åçæ åµï¼ä½æ¯æ§è¡ä¹åå ¨é¨éæ¾æäºã