1. InsertMain.java

package test.main;

 

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;

 

import test.dto.MembersDTO;

 

public class InsertMain {

 

 

public static void main(String[] args) {

// 마이바티스 환경설정 파일 경로

String res = "orm/mybatis/mybatis-config.xml";

try {

// mybatis 환경설정 파일을 읽어오기 위한 스트림 객체

InputStream is = Resources.getResourceAsStream(res);

// SqlSessionFactory객체 얻어오기

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(is);

// Sql명령어를 실행하기 위한 SqlSession 객체 얻어오기

SqlSession session = factory.openSession();

// insert 명령어 수행하기 

MembersDTO dto = 

new MembersDTO("sanghoone", "1234", "@nara.co.kr", "010-1234-4567", null);

int n = session.insert("orm.mybatis.MemberMapper.add",dto);

//System.out.println("데이터 추가 성공");

 

//session.commit();

 

session.close(); // 세션닫기

} catch (IOException e) {

System.out.println(e.getMessage());

}

 

}

 

}

 

 

2. DeleteMain.java

package test.main;

 

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 DeleteMain {

public static void main(String[] args) {

// 마이바티스 환경설정 파일 경로

String res = "orm/mybatis/mybatis-config.xml";

try {

// mybatis 환경설정 파일을 읽어오기 위한 스트림 객체

InputStream is = Resources.getResourceAsStream(res);

// SqlSessionFactory객체 얻어오기

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(is);

// Sql명령어를 실행하기 위한 SqlSession 객체 얻어오기

SqlSession session = factory.openSession();

// delete 명령어 수행하기

String id ="sanghoone";

session.delete("remove",id);

 

session.commit();

 

session.close(); // 세션닫기

} catch (IOException e) {

System.out.println(e.getMessage());

}

 

}

}

 

 

3. UpdateMain.java

package test.main;

 

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;

 

import test.dto.MembersDTO;

 

public class UpdateMain {

public static void main(String[] args) {

// 마이바티스 환경설정 파일 경로

String res = "orm/mybatis/mybatis-config.xml";

try {

// mybatis 환경설정 파일을 읽어오기 위한 스트림 객체

InputStream is = Resources.getResourceAsStream(res);

// SqlSessionFactory객체 얻어오기

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(is);

// Sql명령어를 실행하기 위한 SqlSession 객체 얻어오기

SqlSession session = factory.openSession();

// update 명령어 수행하기

MembersDTO dto = new MembersDTO("sanghoone1","1234","test@123","010",null);

session.update("orm.mybatis.MemberMapper.update",dto);

session.commit();

 

session.close(); // 세션닫기

} catch (IOException e) {

System.out.println(e.getMessage());

}

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

}

}

 

 

4. MemberDTO.java

package test.dto;

 

import java.sql.Date;

 

public class MembersDTO {

 

private String id;

private String pwd;

private String email;

private String phone;

private Date regdate;

 

public MembersDTO() {

 

}

 

public MembersDTO(String id, String pwd, String email, String phone,

Date regdate) {

super();

this.id = id;

this.pwd = pwd;

this.email = email;

this.phone = phone;

this.regdate = regdate;

}

 

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 getEmail() {

return email;

}

 

public void setEmail(String email) {

this.email = email;

}

 

public String getPhone() {

return phone;

}

 

public void setPhone(String phone) {

this.phone = phone;

}

 

public Date getRegdate() {

return regdate;

}

 

public void setRegdate(Date regdate) {

this.regdate = regdate;

}

 

}

 

 

5. MembersMapper.xml

<?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="orm.mybatis.MemberMapper">

 

<insert id="add" parameterType="test.dto.MembersDTO">

insert into members

values(#{id},#{pwd},#{email},#{phone},sysdate)

</insert>

 

<delete id="remove" parameterType="String">

delete from members where id=#{id}

</delete>

 

<update id="update" parameterType="test.dto.MembersDTO">

update members

set pwd = #{pwd},email = #{email},phone = #{phone}

where id = #{id}

</update>

 

 

 

</mapper>

 

 

 

DML 구문이 아주 간단히 구현이 되었다. 

DTO 에 값을 set 해주고 DML 시에 파라미터 타입으로 DTO 를 인자로 받으면 

마이바티스 내부에서 getter 메서드를 호출해서 값을 가져와서 ? , ? , ? 처럼 

바인딩 된 필드에 값을 할당해준다. 

이점은 아이바티스와 동일하게 쿼리 수행시 PreparedStatement 를 기본적으로 사용하기 때문이다. 

이렇게 되면 불필요한 리소스를 낭비하지 않고 SQL 을 날릴수가있고 SQLInjection 으로 부터 안전 하다 . 

 

 



출처: http://devsh.tistory.com/entry/MyBatis-기본-insertdeleteupdate [날샘 코딩]

 

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
38 Spring Batch 간단 정리 Sprong Batch 기초를 알아보자 file 졸리운_곰 2020.10.13 198
37 Spring Batch - ItemReader, ItemWriter, ItemProcessor 졸리운_곰 2020.10.13 370
36 spring batch» Spring Batch Tasklet Example file 졸리운_곰 2020.10.12 198
35 Spring Batch Example 2 - 간단한 Job만들기 file 졸리운_곰 2020.10.12 151
34 Spring batch Job 설정과 실행하기 file 졸리운_곰 2020.10.12 206
33 [Spring Batch] Configuring and Running a Job file 졸리운_곰 2020.10.12 121
32 [스프링] 스프링 배치 기본 개념 file 졸리운_곰 2019.12.24 244
31 Spring Batch 개념 정리 file 졸리운_곰 2019.12.24 257
30 스프링 배치 개발가이드 졸리운_곰 2019.12.24 321
29 Quartz + Spring Batch 조합! Quartz Scheduler 와 Spring Batch 의 궁합과 조합을 정리. file 졸리운_곰 2019.12.24 882
28 Spring Batch 간단 정리 file 졸리운_곰 2019.12.24 421
27 spring batch 소개 file 졸리운_곰 2019.12.24 208
26 Spring Batch - 소개 file 졸리운_곰 2019.12.24 207
25 Spring Batch Multithreading Example file 졸리운_곰 2019.01.31 285
24 Spring batch를 Parallel로 돌려보자 졸리운_곰 2019.01.31 399
23 Spring MVC 주요 애노테이션(Annotation)정리 file 졸리운_곰 2018.12.29 268
22 스프링/스프링부트 애노테이션(Annotation) 정리 졸리운_곰 2018.12.29 385
21 Spring Framework - annotation 정리 #1 졸리운_곰 2018.12.29 321
20 [Spring] Annotation 정리 졸리운_곰 2018.12.29 272
19 Maven 기초 사용법 졸리운_곰 2018.04.15 322
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED