Spring

Spring mybatis

테라시아 2024. 12. 14. 21:28

1. MyBatis
    - 자바 퍼시스턴스 프레임워크의 하나
        : 퍼시스턴스 프레임워크란 XML 서술자나
          데이터의 저장/조회/변경/삭제를 다루는
          클래스와 설정 집합
    - 어노테이션(annotation)이나 XML을 이용하여
      저장 프로시저나 SQL문과 객체를 연결
    - 아파치 라이선스 2.0 -> Free S/W

2. MyBatis 사용 방법
    - Config를 통해 Config, Driver, SessionFactory 구성
    - Annotation 방식 또는 XML 방식의 활용
      1) Annotation
         @Select("SELECT 1 FROM DUAL")
         public String getDual();

      2) XML
         public String getDual();

         <select id="getDual">
            SELECT 1 FROM DUAL
         </select>

 

☆ Code

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.koreait.board.bean.ArtVO;
import com.koreait.board.util.MyUtil;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class ExampleController {
	
	@GetMapping("")
	public void nothing() {
		log.info("Nothing Method is called");
	}
	
	@PostMapping("sports")
	public String postSports() {
		log.info(MyUtil.BLUE + "My favorite sports is the baseball." + MyUtil.END);
		return "index";
	}
	
	@GetMapping("music")
	public String getMusic() {
		log.info(MyUtil.BOLD + "My favorite muisc is the classical music" + MyUtil.END);
		return "/ex/sports";
	}
	
	// classic way
	@GetMapping("classic")
	public String classicTest(HttpServletRequest req) {
		String title = req.getParameter("title");
		String artist = req.getParameter("artist");
		log.info("classic : " + title + "(" + artist + ")");
		return "index";
	}
	
	// spring way I
	@GetMapping("modern")
	public String modern(String title, String artist) {
		log.info("modern : " + title + "(" + artist + ")");
		return "index";
	}
	
	// spring way II
	@GetMapping("future")
	public String future(ArtVO artVO, String title) {
		log.info("future : " + artVO);
		log.info("future : " + title);
		return "index";
	}
}

 

★ TimeMapper.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">
<!-- 연결할 인터페이스 정보를 namespace에 기술 -->
<mapper namespace="com.koreait.board.mapper.TimeMapper">
	<select id="getTime2" resultType="string">
		SELECT SYSDATE() FROM DUAL
	</select>
</mapper>

'Spring' 카테고리의 다른 글

Spring DAO  (2) 2024.12.18
Spring model  (1) 2024.12.17
Spring springboot  (0) 2024.12.13
Spring 의존성 주입  (0) 2024.12.12
Spring Lombok  (0) 2024.12.12