| 인터페이스 | 설명 |
|---|---|
Connection | DB 연결 객체 |
PreparedStatement | SQL 실행 객체 |
ResultSet | SQL 결과를 저장하는 객체 |
MyBatis FrameworkSpring WebDBMS 라이브러리 (예: MySQL Driver, Oracle Driver 등)# DB 연결 정보
spring.datasource.url=jdbc:mysql://localhost:3306/db명
spring.datasource.username=아이디
spring.datasource.password=비밀번호
# MyBatis 설정
mybatis.mapper-locations=classpath:/mapper/**/*.xml
mybatis.type-aliases-package=com.example.dto
💡
mapper-locations는 XML 기반 SQL 파일 경로
💡type-aliases-package는 DTO/VO 클래스 경로
@Mapper
public interface MemberMapper {
// [1] create
@Insert("insert into student(name, kor, math) VALUES(#{name},#{kor},#{math})")
int create(StudentDto studentDto);
// 성공한 레코드 수를 반환 : 1
// [2] readAll
@Select ("select * from student")
List< StudentDto > readAll();
// [3] read
@Select("select * from student where sno=#{sno}")
Map<String, Object> read(int sno); // func end
// [4] update
@Update("update student set kor=#{kor}, math=#{math} where sno=#{sno}")
int update(StudentDto studentDto); // func end
// 성공한 레코드 수를 반환 : 1
// [5] delete
@Delete("delete from student where sno=#{sno}")
int delete(int sno); // func end
// 성공한 레코드 수를 반환 : 1
}
📌
#{}는 SQL에 매개변수를 삽입하는 방식이며, DTO의 멤버 변수명과 일치해야 함
(단, 아래에서는 편의상 Controller 에서 InterFace를 직접 DI 하였음)
@Service
public class MemberController {
// [0] mapper 객체 DI
private final BatisMapper batisMapper;
// [1] create
@PostMapping
public ResponseEntity<Integer> create(@RequestBody StudentDto studentDto){
System.out.println("BatisController.create");
System.out.println("studentDto = " + studentDto);
int result = batisMapper.create(studentDto);
return ResponseEntity.status(200).body(result);
} // func end
// [2] readAll
@GetMapping
public ResponseEntity<List<StudentDto>> readAll(){
System.out.println("BatisController.readAll");
// batisMapper interFace의 readAll SQL문을 실행한 결과를 반환
List<StudentDto> result = batisMapper.readAll();
// ResponseEntity 응답객체로 반환
return ResponseEntity.status(200).body(result);
} // func end
// [3] read
@GetMapping("/read")
public ResponseEntity< Map<String, Object> > read(@RequestParam int sno){
System.out.println("BatisController.read");
System.out.println("sno = " + sno);
Map<String, Object> result = batisMapper.read(sno);
return ResponseEntity.status(200).body(result);
} // func end
// [4] update
@PutMapping
public ResponseEntity<Integer> update(@RequestBody StudentDto studentDto){
System.out.println("BatisController.update");
System.out.println("studentDto = " + studentDto);
int result = batisMapper.update(studentDto);
return ResponseEntity.status(200).body(result);
} // func end
// [5] delete
@DeleteMapping
public ResponseEntity<Integer> delete(@RequestParam int sno){
System.out.println("BatisController.delete");
System.out.println("sno = " + sno);
int result = batisMapper.delete(sno);
return ResponseEntity.status(200).body(result);
} // func end
}
<!-- resources/mapper/MemberMapper.xml -->
<mapper namespace="com.example.mapper.MemberMapper">
<select id="findById" resultType="MemberDto">
SELECT * FROM member WHERE id = #{id}
</select>
</mapper>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MemberDto {
private int id;
private String name;
private String email;
}
@Mapper, @Autowired로 간편 DI