본문 바로가기
개발/jsp

ibatis

by 카앙구운 2015. 10. 15.
728x90
반응형


ibatis-2.3.4.726.jar


ibatis-common-2.jar


ibatis-dao-2.jar


ibatis-sqlmap-2.jar


log4j-1.2.17.jar



처음 시작할때 lib에 jar파일을 붙여 넣기 하여 시작한다.


  config.properties 파일


sample.Driver=com.mysql.jdbc.Driver

sample.ConnectionURL=jdbc:mysql://localhost:3306/mysql?characterEncoding=utf-8

sample.Username=root

sample.Password=apmsetup






                                                                                   Member.xml 



<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap      

    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      

    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<!-- Member.xml -->

<sqlMap namespace="SqlMap">


<!-- 클래스 별칭 지정 -->

<typeAlias alias="MemberVO" type="sample.vo.MemberVO"/>

<resultMap class="MemberVO" id="MemberVOResult">

<result property="id" column="id"/>

<result property="pwd" column="pwd"/>

<result property="name" column="name"/>

</resultMap>


<!-- 회원 정보 가져오기 -->

<select id="selectMember" resultClass="MemberVO">

select id,name from memberInfo

</select>

<insert id="insertMember" parameterClass="MemberVO">

INSERT INTO memberoinfo(id,pwd,name)

VALUES (#id#,#pwd#,#name#)

</insert>

</sqlMap>  




                                                                                 SqlMapConfig.xml 


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMapConfig      

    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      

    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<!-- SqlMapConfig.xml -->

<sqlMapConfig>

<properties resource="config.properties"/>

  <!-- DataSource 설정 -->

<transactionManager type="JDBC" commitRequired="false">

<dataSource type="SIMPLE">

<property name="JDBC.Driver" value="${sample.Driver}" />

<property name="JDBC.ConnectionURL" value="${sample.ConnectionURL}" />

<property name="JDBC.Username" value="${sample.Username}" />

<property name="JDBC.Password" value="${sample.Password}" />

</dataSource>

</transactionManager>


<!-- sqlMap 리소스 파일 -->

<sqlMap resource="Member.xml" />


</sqlMapConfig>




                                                                                  MemberVo.java

/*

 * Class name : MemberVO.java

 */

package sample.vo;


public class MemberVO {

private String id;

private String pwd;

private String name;

private int unq;

private String birthday;

private String post;

private String addr;

private String rdate;

private String gender;

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public int getUnq() {

return unq;

}

public void setUnq(int unq) {

this.unq = unq;

}

public String getBirthday() {

return birthday;

}

public void setBirthday(String birthday) {

this.birthday = birthday;

}

public String getPost() {

return post;

}

public void setPost(String post) {

this.post = post;

}

public String getAddr() {

return addr;

}

public void setAddr(String addr) {

this.addr = addr;

}

public String getRdate() {

return rdate;

}

public void setRdate(String rdate) {

this.rdate = rdate;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getPwd() {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

                                                                                     MemberDAO.java


package sample.dao;

import java.sql.SQLException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;

import sample.vo.MemberVO;

public class MemberDAO {
public List getListMember( SqlMapClient sqlMapper) throws SQLException{
List list =  sqlMapper.queryForList("selectMember");
return list;
}
public void insertMember(SqlMapClient sqlMapper, MemberVO vo) throws SQLException {
sqlMapper.insert("insertMember", vo);
}
public void updateMember(SqlMapClient sqlMapper, MemberVO vo) throws SQLException {
sqlMapper.update("updateMember", vo);
}
public void deleteMember(SqlMapClient sqlMapper, MemberVO vo) throws SQLException {
sqlMapper.delete("deleteMember", vo);
}
}



                                                                                      memberinfo.jsp 


 <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<form name="frm" method="post" action="/study_jsp/MemberInsertServlet">

ID : <input type="text" name="id"> <br>

PW : <input  type="text"  name="pwd"> <br>

NM : <input  type="text"  name="name"> <br>

<input  type="submit"  value="SEND">

</form>

</body>

</html>



                                                                               MemberInsertServlet.java

package servlet;


import java.io.IOException;

import java.io.Reader;

import java.sql.SQLException;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;


import sample.dao.MemberDAO;

import sample.vo.MemberVO;


@WebServlet("/MemberInsertServlet") // 아파치 7.0부터 지원

public class MemberInsertServlet extends HttpServlet{


public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

SqlMapClient sqlMapClient = null;

Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");

sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

reader.close(); 

MemberDAO dao = new MemberDAO();

MemberVO vo = new MemberVO();

String id = req.getParameter("id");

String pwd = req.getParameter("pwd");

String name = req.getParameter("name");

vo.setId(id);

vo.setPwd(pwd);

vo.setName(name);

try {

dao.insertMember(sqlMapClient, vo);

System.out.println("============= success =======");

System.out.println(id + "," + pwd + "," + name);

} catch (SQLException e) {

System.out.println("============= faild =======");

e.printStackTrace();

}

}

}



                                                                                   memberList.jsp 

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>


<%@page import="java.util.List"%>

<%@page import="java.io.Reader"%>

<%@page import="sample.dao.MemberDAO"%>

<%@page import="sample.vo.MemberVO"%>

<%@page import="com.ibatis.sqlmap.client.*"%>

<%@page import="com.ibatis.common.resources.Resources"%>

<%

SqlMapClient sqlMapClient = null;

Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");

sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

reader.close(); 

MemberDAO dao = new MemberDAO();

List list = (List)dao.getListMember(sqlMapClient);


MemberVO vo = null;

int len = list.size();

%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>hello iBatis</title>

</head>

<body>

<table border="1">

<tr>

<td>아이디</td>

<td>이름</td>

</tr>

<%

for(int i=0;i<len;i++) {

vo = (MemberVO)list.get(i);

%>

<tr>

<td><%=vo.getId() %></td>

<td><%=vo.getName() %></td>

</tr>

<%

}

%>

</table>

</body>

</html>



------------------------------------------------------------------------------------------------------------------------------
memberInfo.jsp실행 후 데이터를 입력한 후 send버튼을 누르면 MemberInnsertServlet.java를 통해서 값이 세팅되고 MemberDAO에서 Member.xml에 있는 쿼리문을 불러와 적용 시키게 된다.후에  memberinfo테이블에 저장되고 memberList를 실행하면 저장된 값이뜬다.



728x90
반응형

'개발 > jsp' 카테고리의 다른 글

JSP개발에 디버깅에 필요한 로그확인/쿼리까지 확인가능(log4j)v3e0e4e5a  (0) 2015.10.22
jsp  (0) 2015.10.16
servlet,db  (0) 2015.10.13
Servlet  (0) 2015.10.13
hashmap,foreach,구구단,list  (0) 2015.10.13

댓글