EmpService (인터페이스)
|
EmpMain----------EmpServiceImpl(클래스)---------EmpDAO--------MySQL
(main 메서드) 비즈니스로직 처리 | DB 연동
사용자 입력 및 트랜잭션 처리 ★ |
출력 (화면 처리) |
|
EmpDTO
데이터 전달용
cf) 환경변수들
JAVA_HOME=jdk홈디렉토리 경로
PATH=명령어 경로; 명령어 경로;
CLASSPATH=클래스파일이있는경로 (*.jar)
https://mybatis.org/mybatis-3/getting-started.html#building-sqlsessionfactory-from-xml
https://mybatis.org/mybatis-3/getting-started.html#exploring-mapped-sql-statements
https://mybatis.org/mybatis-3/getting-started.html#building-sqlsessionfactory-from-xml
원본
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
변경
MySqlSessionFactory.java
public class MySqlSessionFactory {
static SqlSessionFactory sqlSessionFactory;
static {
String resource = "com/config/Configuration.xml";
InputStream inputStream=null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}//end static 블럭
// SqlSessionFactory 로부터 SqlSession 얻는 메서드
public static SqlSession getSession() {
// MyBatis는 명시적으로 commit 지정해야 된다.
SqlSession session = sqlSessionFactory.openSession(); // openSession(false) 동일
return session;
}
com.service.EmpService (인터페이스)
|
EmpSerivceImpl (클래스)
cf) Connection과 SqlSession 특징
-> 반드시 메서드 안에서 로컬 변수로 얻어야 된다 (thread-safe 보장)
이유는 공유하면 절대 안됨!
메서드마다 따로 얻어서 쓰고 close 해줘야됨