ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • bank app5 - MyBatis 설정
    spring boot 2023. 4. 17. 11:14
    학습 목표

    1. MyBatis 에 대한 개념
    2. yml 파일에 MyBatis 설정 하기 (의존성 추가 되어 있는 상태)
    3. UserRepository 인터페이스 선언 하기
    4. user.xml 파일 정의
    5. AccountRepository 인터페이스 선언
    6. account.xml 파일 정의
    7. HistoryRepository 인터페이스 선언
    8. history.xml 파일 정의

     

    MyBatis

    자바 객체와 SQL문 사이의 자동 매핑을 지원하는 매퍼 프레임 워크입니다.

    MyBatis에서는 SQL 쿼리를 작성할 때 XML 또는 어노테이션을 이용해서 작업할 수 있습니다.
    이를 통해 쿼리구문을 작성하고 데이터 베이스와 통신을 수행할 수 있습니다.

    MyBatis는 매우 유연한 구조를 가지고 있어 SQL 쿼리와 자바 객체의 매핑 규칙을 세부적으로 지정 할 수 있으며 동적 SQL 쿼리 구문을 작성할 수 있게 지원합니다.

    2. mybatis yml 파일에 설정하기

    :ibatis - 이전 이름 (2.4 이후부터는 mybatis 변경 됨)

    server:
      port: 8080
      servlet:
        encoding:
          charset: utf-8
          force: true
    
          
          
    spring:
      mvc:
        view:
          prefix: /WEB-INF/view/
          suffix: .jsp
      datasource:
        url: jdbc:h2:mem:testdb;MODE=MySQL
        driver-class-name: org.h2.Driver
        username: sa
        password: 
      sql:
        init:
          schema-locations:
          - classpath:db/table.sql
          data-locations:
          - classpath:db/data.sql
      
      h2:
        console:
          enabled: true
      output:
        ansi:
          enabled: always
          
          
    mybatis:
      mapper-locations:
      - classpath:mapper/**.xml
      configuration:
        map-underscore-to-camel-case: true

     

    UserRepository 인터페이스 선언하기

    @Mapper  // MyBatis 의존 설정 함(build.gradle 파일) 
    public interface UserRepository {
    	public int insert(User user);
    	public int updateById(User user); 
    	public int deleteById(Integer id);
    	public User findById(Integer id); 
    	public List<User> findAll();
    }

     

     

     

    mapper/user.xml 파일 생성

    DB 접근 기술 - UserDAO를 구현 했다면 Mybatis 는 xml 파일로 설정 합니다.

    이점은 Connection, PreparedStatement, ResultSet 알아서 관리 및 매핑 해주고

    close 처리를 자동으로 해준다

     

    user.xml 파일 정의

     

     

     

    package com.tenco.bank.repository.interfaces;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Mapper;
    
    import com.tenco.bank.repository.model.User;
    
    @Mapper	// MyBatis 의존 설정 함 (build.gradle 파일)
    public interface UserRepository {
    
    	// 무엇을 기반으로 변경하거나 찾는지 정확하게 명시 해준다.
    	// principal 접근 주체
    	
    	public int insert(User user);
    	public int updateById(User user);
    	public int deleteById(Integer id);
    	public User findById(Integer id);
    	// 한사람의 정보만 담음
    	public List<User> findAll();
    	// 여러명의 정보를 담음
    }

     

    AccountRepository 인터페이스 선언

    package com.tenco.bank.repository.interfaces;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Mapper;
    
    import com.tenco.bank.repository.model.Account;
    
    @Mapper // mybatis 연결 처리 
    public interface AccountRepository {
    	
    	public int insert(Account account); 
    	public int updateById(Account account); 
    	public int deleteById(int id); 
    	
    	public List<Account> findAll();   
    	public Account findById(int id); 
    }

    'spring boot' 카테고리의 다른 글

    bank app7 - 화면 구현(2)  (0) 2023.04.17
    bank app6 - 화면구현(1)  (0) 2023.04.17
    bank app4 - h2 db 초기 값 설정  (0) 2023.04.14
    bank app3 - 모델링  (0) 2023.04.14
    bank app2 - 네이밍  (0) 2023.04.14
Designed by Tistory.