Insert 과정
<insert.jsp> 객체타입으로 보냄
1 2 3 4 5 6 7 8 9 | <form method="post" action="insert.action"> 아이디<input type="text" name="membersVo.id" /><br /> 비밀번호<input type="text" name="membersVo.pwd" /><br /> 이메일<input type="text" name="membersVo.email" /><br /> 이름<input type="text" name="membersVo.name" /><br /> <input type="submit" value="가입" /> </form> | cs |
<insertAction.java>
1 2 3 4 5 6 7 8 9 10 11 | MembersDao dao = MembersDao.getInstance(); int result = dao.setMember(membersVo); if (result == 1) { System.out.println("Insert 완료"); return SUCCESS; } else { System.out.println("Insert 실패"); return null; } | cs |
<MembersDao.java>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class MembersDao { //Lazy Holder 싱글톤 방식 public static class SingletonHolder { private static final MembersDao ins = new MembersDao(); } public static MembersDao getInstance() { return SingletonHolder.ins; } private SqlSessionFactory sqlSessionFactory; private MembersDao() { sqlSessionFactory = SqlSessionFactoryService.getSqlSessionFactory(); } public int setMember(MembersVo membersVo) { SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.openSession(true); MembersMapper membersMapper = sqlSession.getMapper(MembersMapper.class); return membersMapper.insertMember(membersVo); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); return 0; } finally { if (sqlSession != null) sqlSession.close(); } } } | cs |
SessionFactory 싱글톤 객체로 가져오기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class SqlSessionFactoryService { private static SqlSessionFactory sqlSessionFactory; // static멤버변수를 초기화할때는 static블록을 사용한다.(생성자x) static { try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } catch (IOException ie) { System.out.println(ie.getMessage()); } } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } } | cs |
<mybatis-config.xml>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <configuration> <!--//////// 접속할 DB서버에 대한 정보 설정 ///////////// --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="org.mariadb.jdbc.Driver" /> <property name="url" value="jdbc:mariadb://localhost:3366/member" /> <property name="username" value="root" /> <property name="password" value="1234" /> </dataSource> </environment> </environments> <!--/////// sql구문이 들어있는 SQL Mapper파일 포함 ////////// --> <mappers> <mapper resource="/map/MembersMapper.xml" /> </mappers> </configuration> | cs |
mapper -> MembersMapper.java
1 2 3 4 5 6 7 8 | public interface MembersMapper { public int insertMember(MembersVo membersVo); public MembersVo getMember(HashMap<String, String> map); } | cs |
map -> MembersMapper.xml
11 12 13 14 15 16 17 | <mapper namespace="mapper.MembersMapper"> <insert id="insertMember" parameterType="vo.MembersVo"> insert into userinfo values(#{id}, #{name}, #{email}, #{pwd}, #{reg_date} ) </insert> </mapper> | cs |
'웹' 카테고리의 다른 글
PHP 페이징 + Ajax (코드이그나이터) (1) | 2018.07.11 |
---|---|
jquery 선택자 정리 (0) | 2018.07.09 |
HTML CSS 활용 (생활코딩) (1) | 2018.04.25 |
페이징 속도 빠르게 하는 하는 DB 쿼리 (0) | 2018.04.25 |
스트러츠 (1) | 2018.04.10 |