SqlSession유틸
같은 config를 공유하는SqlSessionFactory를 별개의 클래스로 작성
최종적으로 SqlSession을 반환하는 메서드를 작성한다
import java.io.IOException;
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 MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
static {
InputStream in = null;
try {
in = Resources.getResourceAsStream("kr/or/ddit/mybatis/config/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(in!=null)try {in.close();}catch(IOException e) {}
}
}
// SqlSession객체를 반환하는 메서드
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
session = MyBatisUtil.getSqlSession();
이를 통해서 간단히 세션을 메서드를 통해 불러올 수 있음!
전화번호관리
기존에 했던 전화번호관리를 mybatis로 수정하여 사용할 수 있다
member-mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 이 문서는 myBatis에서 처리할 SQL문을 작성하는 문서입니다 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- <mapper>태그의 namespace속성에 설정된 값은 Java소스에서 실행할 SQL문을 호출할 때
같이 사용되는 이름이다 -->
<mapper namespace="mem">
<insert id="insertMem" parameterType="memVo">
insert into mymember
(mem_id, mem_pass, mem_name, mem_tel, mem_addr)
values(#{mem_id}, #{mem_pass}, #{mem_name}, #{mem_tel}, #{mem_addr})
</insert>
<delete id="deleteMem" parameterType="String">
delete from mymember where mem_id=#{mem_id}
</delete>
<update id="updateMem" parameterType="memVo">
update mymember set mem_pass=#{mem_pass}, mem_name=#{mem_name},
mem_tel=#{mem_tel}, mem_addr=#{mem_addr} where mem_id=#{mem_id}
</update>
<select id="getAllMem" resultType="memVo">
select * from mymember
</select>
<select id="getMemCnt" resultType="int" parameterType="String">
select count(*) count from mymember where mem_id=#{mem_id}
</select>
<select id="getMemInfo" resultType="memVo" parameterType="String">
select * from mymember where mem_id=#{mem_id}
</select>
<!--
Map객체를 파라미터로 받아서 처리할 때의 변수명 역할을 Map의 key값으로 한다
-->
<select id="updateMem2" parameterType="map">
update mymember set ${FIELD}=#{VALUE} where mem_id=#{MEMID}
</select>
</mapper>
MemberMybatisDaoImpl.java
package kr.or.ddit.member.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import kr.or.ddit.vo.MemberVO;
import kr.or.ddit.util.MyBatisUtil;
public class MemberMybatisDaoImpl implements IMemberDao{
private static MemberMybatisDaoImpl dao;
private MemberMybatisDaoImpl() {
// TODO Auto-generated constructor stub
}
public static MemberMybatisDaoImpl getInstance() {
if(dao==null) {
dao = new MemberMybatisDaoImpl();
}
return dao;
}
@Override
public int insertMember(MemberVO memVo) {
SqlSession session = null;
int cnt = 0; // 반환값이 저장될 변수
try {
session = MyBatisUtil.getSqlSession();
cnt = session.insert("mem.insertMem", memVo);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(session!=null) try {session.close();}catch(Exception e){}
}
return cnt;
}
@Override
public int deleteMember(String mem_id) {
SqlSession session = null;
int cnt = 0; // 반환값이 저장될 변수
try {
session = MyBatisUtil.getSqlSession();
cnt = session.delete("mem.deleteMem", mem_id);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(session!=null) try {session.close();}catch(Exception e){}
}
return cnt;
}
@Override
public int updateMember(MemberVO memVo) {
SqlSession session = null;
int cnt = 0; // 반환값이 저장될 변수
try {
session = MyBatisUtil.getSqlSession();
cnt = session.update("mem.updateMem", memVo);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(session!=null) try {session.close();}catch(Exception e){}
}
return cnt;
}
@Override
public List<MemberVO> getAllMember() {
List<MemberVO> list = new ArrayList<MemberVO>();
SqlSession session = null;
try {
session = MyBatisUtil.getSqlSession();
list = session.selectList("mem.getAllMem");
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();}catch(Exception e){}
}
return list;
}
@Override
public int getMemIdCount(String mem_id) {
SqlSession session = null;
int count = 0;
try {
session = MyBatisUtil.getSqlSession();
count = session.selectOne("mem.getMemCnt", mem_id);
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();}catch(Exception e){}
}
return count;
}
@Override
public MemberVO getMemInfo(String mem_id) {
MemberVO memVo = new MemberVO();
SqlSession session = null;
try {
session = MyBatisUtil.getSqlSession();
memVo = session.selectOne("mem.getMemInfo", mem_id);
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();}catch(Exception e){}
}
return memVo;
}
@Override
public int updateMember2(Map<String, String> paramMap) {
SqlSession session = null;
int cnt = 0; // 반환값이 저장될 변수
try {
session = MyBatisUtil.getSqlSession();
cnt = session.update("mem.updateMem2", paramMap);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(session!=null) try {session.close();}catch(Exception e){}
}
return cnt;
}
}
'JAVA > HIGH JAVA' 카테고리의 다른 글
[JAVA] Log4J (0) | 2024.05.27 |
---|---|
5/24 Homework - 게시판 mybatis적용 (0) | 2024.05.24 |
5/23 Homework - mybatis적용 lprod테이블추가 (0) | 2024.05.23 |
[JAVA] MyBatis (0) | 2024.05.22 |
5/21 Homework - 게시판 만들기 (0) | 2024.05.21 |