Maven Repository 사이트에서 자르파일을 다운로드 받아서 프로젝트 lib폴더에 넣어줍니다.
https://mvnrepository.com/artifact/org.mybatis/mybatis/3.4.6
그 후 ojdbc6 도 같이 넣어줍니다.
BbsDto
package dto;
import java.io.Serializable;
/*
DROP TABLE BBS
CASCADE CONSTRAINTS;
DROP SEQUENCE SEQ_BBS;
CREATE TABLE BBS(
SEQ NUMBER(8) PRIMARY KEY,
ID VARCHAR2(50) NOT NULL,
REF NUMBER(8) NOT NULL,
STEP NUMBER(8) NOT NULL,
DEPTH NUMBER(8) NOT NULL,
TITLE VARCHAR2(200) NOT NULL,
CONTENT VARCHAR2(4000) NOT NULL,
WDATE DATE NOT NULL,
PARENT NUMBER(8) NOT NULL,
DEL NUMBER(1) NOT NULL,
READCOUNT NUMBER(8) NOT NULL
);
CREATE SEQUENCE SEQ_BBS
START WITH 1
INCREMENT BY 1;
ALTER TABLE BBS
ADD CONSTRAINT FK_BBS_ID FOREIGN KEY(ID)
REFERENCES MEMBER(ID);
*/
public class BbsDto implements Serializable {
private int seq;
private String id; // 작성자
private int ref; // 그룹번호
private int step; // 행(row)번호
private int depth; // 깊이
private String title;
private String content;
private String wdate;
private int parent;
private int del;
private int readcount;
public BbsDto() {
}
public BbsDto(int seq, String id, int ref, int step, int depth, String title, String content, String wdate,
int parent, int del, int readcount) {
super();
this.seq = seq;
this.id = id;
this.ref = ref;
this.step = step;
this.depth = depth;
this.title = title;
this.content = content;
this.wdate = wdate;
this.parent = parent;
this.del = del;
this.readcount = readcount;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "BbsDto [seq=" + seq + ", id=" + id + ", ref=" + ref + ", step=" + step + ", depth=" + depth + ", title="
+ title + ", content=" + content + ", wdate=" + wdate + ", parent=" + parent + ", del=" + del
+ ", readcount=" + readcount + "]";
}
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
public int getRef() {
return ref;
}
public void setRef(int ref) {
this.ref = ref;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
public String getWdate() {
return wdate;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
public int getParent() {
return parent;
}
public void setParent(int parent) {
this.parent = parent;
}
public int getDel() {
return del;
}
public void setDel(int del) {
this.del = del;
}
public int getReadcount() {
return readcount;
}
public void setReadcount(int readcount) {
this.readcount = readcount;
}
public BbsDto(String id, String title, String content) {
super();
this.id = id;
this.title = title;
this.content = content;
}
}
Bbsparam
package dto;
import java.io.Serializable;
public class Bbsparam implements Serializable {
private String keyword; // 검색어
private String content;
private String s_category; // 제목, 내용, 작성자
public Bbsparam() {
}
public Bbsparam(String keyword, String content, String s_category) {
super();
this.keyword = keyword;
this.content = content;
this.s_category = s_category;
}
}
MemberDto
package dto;
import java.io.Serializable;
/*
CREATE TABLE MEMBERDTO(
ID VARCHAR2(50) PRIMARY KEY,
PWD VARCHAR2(50) NOT NULL,
EMAIL VARCHAR2(50) NOT NULL
)
*/
public class MemberDto implements Serializable {
private String id;
private String pwd;
private String email;
public MemberDto() {
}
public MemberDto(String id, String pwd, String email) {
super();
this.id = id;
this.pwd = pwd;
this.email = email;
}
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;
}
@Override
public String toString() {
return "MemberDto [id=" + id + ", pwd=" + pwd + ", email=" + email + "]";
}
}
mainClass
package main;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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 dto.BbsDto;
import dto.Bbsparam;
import dto.MemberDto;
public class mainClass {
public static void main(String[] args) {
String res = "mybatis/config.xml";
try {
// mybatis 설정 파일을 읽어 들인다
InputStream is = Resources.getResourceAsStream(res);
// SqlSessionFactory 객체를 취득한다. 객체를 취득해야 데이터를 가져올수 있으니
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
// insert
/*
// MemberDto dto = new MemberDto("aaa", "111", "aaa111");
// MemberDto dto = new MemberDto("bbb", "222", "bbb222");
// MemberDto dto = new MemberDto("ccc", "333", "ccc333");
int n = session.insert("add", dto);
if(n > 0) {
session.commit();
System.out.println("추가성공");
}else {
session.rollback();
System.out.println("추가실패");
}
*/
// delete1 java.lang.String
/*
String findId = "aaa";
int n = session.delete("remove",findId);
if(n > 0) {
session.commit();
System.out.println("삭제성공");
}else {
session.rollback();
System.out.println("삭제실패");
}
*/
// delete2 obj
/*
MemberDto dto = new MemberDto("ccc", null, "");
int n = session.delete("removeobj",dto);
if(n > 0) {
session.commit();
System.out.println("삭제성공");
}else {
session.rollback();
System.out.println("삭제실패");
}
*/
// update
/*
MemberDto dto = new MemberDto("qqq", "1234", "qweqwe");
int n = session.update("update", dto);
if(n > 0) {
session.commit();
System.out.println("수정성공");
}else {
session.rollback();
System.out.println("수정실패");
}
*/
//select
String fId = "qqq";
MemberDto _dto = session.selectOne("find", fId);
System.out.println(_dto.toString());
// select list
List<MemberDto> list = session.selectList("alllist");
for (MemberDto mem : list) {
System.out.println("list:" +mem.toString());
}
// search
Bbsparam bp = new Bbsparam("qwe","","content");
List<BbsDto> bbslist = session.selectList("search", bp);
for (BbsDto bbs : bbslist) {
System.out.println("bbs : " + bbs.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="hr"/>
<property name="password" value="hr"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/MemberMapper.xml" />
</mappers>
</configuration>
MemberMapper.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="mybatis.MemberMapper">
<insert id="add" parameterType="dto.MemberDto">
INSERT INTO MEMBERDTO(ID, PWD, EMAIL)
VALUES( #{id}, #{pwd}, #{email} )
</insert>
<delete id="remove" parameterType="java.lang.String">
DELETE FROM MEMBERDTO
WHERE ID=#{id}
</delete>
<delete id="removeobj" parameterType="dto.MemberDto">
DELETE FROM MEMBERDTO
WHERE ID=#{id}
</delete>
<update id="update" parameterType="dto.MemberDto">
UPDATE MEMBERDTO
SET ID=#{id}, PWD=#{pwd}, EMAIL=#{email}
</update>
<update id="update2" parameterType="dto.MemberDto">
UPDATE MEMBERDTO
SET PWD=#{pwd}
WHERE ID=#{id}
</update>
<select id="find" parameterType="java.lang.String" resultType="dto.MemberDto">
SELECT * FROM MEMBERDTO
WHERE ID=#{id}
</select>
<select id="alllist" resultType="dto.MemberDto">
SELECT ID, PWD, EMAIL
FROM MEMBERDTO
</select>
<select id="search" parameterType="dto.Bbsparam" resultType="dto.BbsDto">
SELECT *
FROM BBS
WHERE 1=1
<if test="s_category == 'title'">
AND TITLE LIKE '%'||#{keyword}||'%'
</if>
<if test="s_category == 'content'">
AND CONTENT LIKE '%'||#{keyword}||'%'
</if>
</select>
</mapper>
'Spring' 카테고리의 다른 글
Spring으로 Mybatis 이용하기 (0) | 2020.05.26 |
---|---|
Spring 설치 로그출력 (0) | 2020.04.27 |
댓글