(1) 프로젝트 구조
(2) Mybatis와 Database 설정 파일
db.properties
- 데이터베이스에 대한 정보
driver=org.mariadb.jdbc.Driver url=jdbc:mariadb://localhost:3306/chat username=root password=비밀번호
mybatis-config.xml
마이바티스 설정 정보
(2) Mapper
select, insert, update, delete에 대한 쿼리
(3) VO, DAO
MemberVO.java
package com.ysh.vo; public class MemberVO { public String email; public String password; public String name; public MemberVO() { super(); } public MemberVO(String email, String password, String name) { super(); this.email = email; this.password = password; this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
MemberDAO.java
package com.ysh.dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.ysh.vo.MemberVO; public class MemberDAO { private SqlSessionFactory sqlSessionFactory = null; public MemberDAO(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } public int insertMember(MemberVO memberVO) { int id = -1; SqlSession session = sqlSessionFactory.openSession(); try { id = session.insert("member.insert", memberVO); } finally { session.commit(); session.close(); } System.out.println("insert("+memberVO+") --> "+memberVO.getEmail()); System.out.println(memberVO.getName()); return id; } public ListselectAll() { List list = null; SqlSession session = sqlSessionFactory.openSession(); try { list = session.selectList("member.selectAll"); } finally { session.close(); } System.out.println(list); return list; } public MemberVO selectOne(String email) { MemberVO member = null; SqlSession session = sqlSessionFactory.openSession(); try { member = session.selectOne("member.selectOne", email); } finally { session.close(); } System.out.println("select" + member.getEmail()); return member; } public void update(MemberVO memberVO) { int id = -1; SqlSession session = sqlSessionFactory.openSession(); try { id = session.update("member.update", memberVO); } finally { session.commit(); session.close(); } } public void delete(String email) { SqlSession session = sqlSessionFactory.openSession(); try { session.delete("member.delete", email); } finally { session.commit(); session.close(); } } }
(4) ConnectionFactory
ConnectionFactory.java
package com.ysh.config; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class ConnectionFactory { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "com/ysh/config/mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource); if (sqlSessionFactory == null) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } } catch(FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace(); } catch(IOException ioException) { ioException.printStackTrace(); } } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }
'Framework > Mybatis' 카테고리의 다른 글
Mybatis Setting (With Gradle) (0) | 2014.07.23 |
---|