기록해! 정리해!

myBatis2 (1) 본문

SpringBoot

myBatis2 (1)

zsuling 2022. 10. 13. 11:05

1. 프로젝트 생성 (devtools, oracle, mybatis framework, spring web, lombok)

2. pom.xml

3. application

 -Jsp 경로 설정

 -mapper 폴더 생성

# 주석 ( none , servlet )
spring.main.web-application-type=servlet
server.port=8888

# 배너 OFF
spring.main.banner-mode=off 


# 오라클 컨넥션 정보
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url =jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=1234

# JSP 경로 설정
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

####  다중 mapper 설정하기
mybatis.mapper-locations=classpath:mapper/**/*.xml

4. static 에 index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h2> 2022-10-13일 수업</h2>
</div>
</body>
</html>

5. 오라클 테이블

Create table examtbl1013 (
 sno char(5)  not  null primary key ,
 sname nvarchar2(10),
 kor  number(3),
 eng  number(3),
 math number(3),
 hist number(3)
) ;

insert into examtbl1013 values('10101','김학생', 75, 85, 90, 60);
insert into examtbl1013 values('10102','이학생', 70, 75, 70, 60);
insert into examtbl1013 values('10103','박학생', 70, 85, 80, 75);
insert into examtbl1013 values('10201','조학생', 90, 85, 100, 50);
insert into examtbl1013 values('10202','황학생', 65, 65, 60, 70);
insert into examtbl1013 values('10203','임학생', 45, 55, 50, 55);
insert into examtbl1013 values('10304','천학생', 70, 75, 85, 80);
insert into examtbl1013 values('10305','남학생', 100, 85, 90, 90);
insert into examtbl1013 values('10306','여학생', 80, 95, 90, 85);
insert into examtbl1013 values('10307','노학생', 35, 55, 70, 55);

select *  from examtbl1013;

6. mapper

<?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="com.rubypaper.exam.ExamDao">

</mapper>

7. ExamDao

@Mapper로 Dao인터페이스 매핑시키기

package com.rubypaper.exam;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ExamDao {
	   void  insert (ExamVO vo);
	   void  update (ExamVO vo);
	   void  delete (ExamVO vo);
	   ExamVO getExam(ExamVO vo);
	   List<ExamVO> getExamList(ExamVO vo);
}

8. mapper (CRUD 잡기)

<?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">

<!-- 인터페이스 DAO 이름을 가지고 namespace 이름으로 지정한다.  --> 
<mapper namespace="com.rubypaper.exam.ExamDao">

<!-- id의 값은 ExamDao 인터페이스의 메소드 이름과 매핑된다.   -->
<insert id="insert"  parameterType="com.rubypaper.exam.ExamVO">
 <![CDATA[
   insert  into examtbl1011 (sno, sname, kor, eng, math, hist)
   values( #{sno}, #{sname}, #{kor}, #{eng}, #{math}, #{hist})
 ]]>
</insert>

<update id="update"  parameterType="com.rubypaper.exam.ExamVO">
 <![CDATA[
   update examtbl1011 
   set sname=#{sname}, kor=#{kor}, eng=#{eng}, math=#{math}, hist=#{hist} 
   where sno=#{sno}
 ]]>
</update>

<delete id="delete"  parameterType="com.rubypaper.exam.ExamVO">
 <![CDATA[
   delete  from examtbl1011 where sno=#{sno}
 ]]>
</delete>

<select id="getExam"  parameterType="com.rubypaper.exam.ExamVO"
                      resultType="com.rubypaper.exam.ExamVO" >
 <![CDATA[
   select  *  from examtbl1011 where sno=#{sno}
 ]]>
</select>


<select id="getExamList"  parameterType="com.rubypaper.exam.ExamVO"
                          resultType="com.rubypaper.exam.ExamVO" >
 <![CDATA[
   select  *  from examtbl1011 
 ]]>
</select>

</mapper>

 

'SpringBoot' 카테고리의 다른 글

myBatis2 (3)- 검색창  (0) 2022.10.13
myBatis2 (2)- 상세보기  (0) 2022.10.13
myBatis  (0) 2022.10.11
다중 mapper 설정하기  (0) 2022.10.11
MyBatis 사용하기  (0) 2022.10.11
Comments