2019. 4. 16. 11:47ㆍMybatis
#1 Mybatis를 사용하기 위해 라이브러리 설치
1) Google에서 mybatis download로 검색 후 상위 사이트에 접속
2) zip 파일을 다운로드
3) 압축을 풀면 jar 파일들을 lib에 추가
#2 xml 에서의 빌드하기
-mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="member" type="vo.Member"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="org.h2.Driver"/> <property name="url" value="jdbc:h2:tcp://localhost/~/kimjonghyun"/> <property name="username" value="sa"/> <property name="password" value=""/> </dataSource> </environment> </environments> <mappers> <mapper resource="dao/mapper/MemberMapper.xml"/> </mappers> </configuration> |
#3 Mapper 작성
-MemberMapper.xml
=> 사용될 sql 구문을 삽입하는 공간이다.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.mapper.MemberMapper">
<select id="getMemberById" parameterType="string" resultType="member"> select id as id from member where id = #{id} </select>
<select id="getAllMembers" resultType="member"> SELECT * FROM member ORDER BY id </select>
<insert id="insertMember" parameterType="member"> INSERT INTO member (id,password,name,birthday,gender,email,address,tel,mtel,reg_date) VALUES (#{id}, #{password}, #{name}, #{birthday}, #{gender}, #{email}, #{address}, #{tel}, #{mtel}, CURRENT_TIMESTAMP ) </insert> </mapper> |
#4 DB와 연결하는 Dao 클래스 생성성
package dao;
import java.io.IOException; import java.io.InputStream; import java.util.List;
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import vo.Member;
public class MemberMyBatisDao {
SqlSessionFactory factory;
private SqlSessionFactory getSqlSessionFactory() { if(factory !=null) { // 페이지가 변경될때마다 설정파일을 불러오는 작업을 하지않기 위해 선언 return factory; } InputStream is = null; try { is = Resources.getResourceAsStream("mybatis-config.xml"); // 설정파일 불러오기 } catch (IOException e) { e.printStackTrace(); } SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); // 설정파일을 읽기 위한 객체 factory= builder.build(is); return factory; }//getSqlSessionFactory
public Member getMemberById(String id) { // SqlSession 선언 => 기존의 Connection과 동일 SqlSession sqlSession= getSqlSessionFactory().openSession();
Member member = (Member)sqlSession.selectOne("dao.mapper.MemberMapper.getMemberById",id); // id에 해당하는 레코드값을 가져온다 , 한개의 레코드값 sqlSession.close(); return member; }//getMemberById
public static void main(String[] args) { // 테스트 MemberMyBatisDao dao = new MemberMyBatisDao(); Member member = dao.getMemberById("admin");
System.out.println(member);
}//main }// MemberMyBatisDao |
'Mybatis' 카테고리의 다른 글
Mybatis#4) 복합 SQL (0) | 2019.04.18 |
---|---|
Mybatis#3) 예제 : 복수변수 ,Map변수 (0) | 2019.04.17 |
Mybatis#2 : 예제(insert, update, select, delete..) (1) | 2019.04.16 |