반응형

Mapper >> Xml >> Mappertests 순서로 작성한다.

 

Mapper

package com.kh.studentmvc.mapper;

import java.util.List;

import com.kh.studentmvc.domain.StudentVo;

public interface StudentMapper {


// vo의 동작부터 확인한다.

List<StudentVo> getList();

void insert(StudentVo vo);

void insertSelectKey(StudentVo vo);

StudentVo read(Long bno);

int delete(Long bno);

StudentVo read2(int sno);

int delete2(int sno);

int update(StudentVo vo);

}

 

StudentMapper.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="com.kh.studentmvc.mapper.StudentMapper">

<select id = "getList" resultType="com.kh.studentmvc.domain.StudentVo">
    select * from tbl_student order by sno desc
</select>


<insert id="insert">
    insert into tbl_student (bno, sno, sname, syear, gender, major, score)
    VALUES (seq_board.nextval, seq_board.nextval + 1, #{sname}, #{syear}, #{gender}, #{major}, #{score})
</insert>


<insert id="insertSelectKey">
    <selectKey order="BEFORE" keyProperty="bno" resultType="Long"> 
    select seq_board.nextval from dual
    </selectKey>

    insert into tbl_student (bno, sno, sname, syear, gender, major, score)
    VALUES (#{bno}, seq_board.nextval + 1, #{sname}, #{syear}, #{gender}, #{major}, #{score})
</insert>

<select id = "read" resultType="com.kh.studentmvc.domain.StudentVo">
select * from tbl_student where bno = #{bno}
</select>

<delete id="delete">
delete from tbl_student where bno = #{bno}
</delete>



<select id = "read2" resultType="com.kh.studentmvc.domain.StudentVo">
select * from tbl_student where sno = #{sno}
</select>

<delete id="delete2">
delete from tbl_student where sno = #{sno}
</delete>

<update id="update">
update tbl_student
set 
sname = #{sname},
syear = #{syear},
gender = #{gender},
major = #{major},
score = #{score},
modifydate = sysdate
where sno = #{sno}
</update>


</mapper>

selectKey를 이용해서 먼저 bno를 생성하고 다시 그것을 insert에 넣는 비효율적인 방식,
하지만, 입력전에 생성할 수 있어서 이 값을 잡아올 수 있다.

StudentMapperTests.java

package com.kh.studentmvc.mapper;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.kh.studentmvc.domain.StudentVo;

import lombok.extern.log4j.Log4j;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class StudentMapperTests {

@Autowired
private StudentMapper studentMapper;

@Test
public void testGetList() {

log.info("---------------------------------------");
studentMapper.getList();
}

@Test
public void testInsert() {
StudentVo vo = new StudentVo();
vo.setSname("이순");
vo.setSyear(4);
vo.setGender("M");
vo.setMajor("eng");
vo.setScore(90);
studentMapper.insert(vo);
log.info("---------------insert-----------------");
log.info("after insert " + vo.getBno());
}

@Test
public void testInsertSelectKey() {
StudentVo vo = new StudentVo();
vo.setSname("이순");
vo.setSyear(3);
vo.setGender("M");
vo.setMajor("eng");
vo.setScore(99);

studentMapper.insertSelectKey(vo);

log.info("---------------SELECT KEY-----------------");
log.info("after selectKey " + vo.getBno());
}
@Test
public void testRead() {
StudentVo vo = studentMapper.read(9L);
log.info(vo);
}

@Test
public void testDelete() {

int count = studentMapper.delete(15L);
log.info("=========================count : " + count);
}

@Test
public void testRead2() {
StudentVo vo = studentMapper.read2(8);
log.info(vo);
}

@Test
public void testDelete2() {

int count = studentMapper.delete2(8);
log.info("=========================count : " + count);
}

@Test
public void testUpdate() {

StudentVo vo = new StudentVo();
vo.setSno("3");
vo.setSname("박");
vo.setSyear(1);
vo.setGender("F");
vo.setMajor("경영");
vo.setScore(89);

log.info("================count : " + studentMapper.update(vo));

}

}

 

반응형

+ Recent posts