mybatis工厂类封装SqlSessionFactory和SqlSession
本文最后更新于 3272 天前,其中的信息可能已经有所发展或是发生改变。

因为每次测试都西药读取mybatis-config.xml根配置文件,然后有需要重复创建SqlSessionFactory和SqlSession,这样的操作比较频繁。所以也想Hibernate一样。开发一个工厂类封装以上的重复代码…

package com.test;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtils {
	//初始化创建SqlSessionFactory
	private static SqlSessionFactory sqlSessionFactory = null;
	static{
		try{
			//读取mybaits-config.xml文件
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//获取SqlSession
	public static SqlSession getSqlSession(){
		return sqlSessionFactory.openSession();
	}
	//获取SqlSessionFactory
	public static SqlSessionFactory getSessionFactory(){
		return sqlSessionFactory;
	}
}

Image

评论

  1. Image
    沃八达
    Windows Chrome 14.0.802.30
    9 年前
    2017-9-13 15:03:52

    没事就来转一转,每天多吃两碗饭!

  2. Image
    Windows Chrome 55.0.2883.87
    9 年前
    2017-9-15 19:04:46

    说好的学php呢?

    • Image
      博主
      关关
      Windows Chrome 50.0.2661.102
      9 年前
      2017-9-17 19:19:29

      echo ‘学校的课程…’;

  3. Image
    Windows Chrome 55.0.2883.87
    9 年前
    2017-10-04 12:54:21

    学习了 谢谢

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇