Springboot 12주차 개발일지

이동규·2023년 8월 4일

Springboot 기초

목록 보기
12/13

Mybatis란 무엇인가?

쿼리 기반 웹 애플리케이션을 개발할 때 가장 많이 사용되는 SQL 매퍼(Mapper) 프레임워크이다.

1. Java 코드로 작성한 List,HashMap등은 메모리에 저장된다.

2. 여러 서버 프로세스가 같은 기능을 하면서 data를 공유해야한다.Springboot <-> 외부 데이터 베이스
Mybatis는 자바의 함수를 SQL 선언문과 연결지어 사용이 가능하게 해준다.

spring boot에서 yml은 무엇인가?

설정 정보를 구성하는 데 사용되는 파일 형식이다.

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/demo_schema
    username: demo_user
    password: 1234

mybatis:
  mapper-locations: "classpath:mybatis/mapper/*.xml"
  configuration:
    map-underscore-to-camel-case: true

Mapping이란 무엇인가?

하나의 값을 다른 값으로 대응시키는 것을 말한다.

key : value의 형태

Data Mapper란 무엇인가?

데이터 매퍼는 데이터베이스나 다른 데이터 소스와 애플리케이션 사이에서 데이터를 벼환하고 매핑하는 역할을 한다. 즉, 애플리케이션의 데이터 객체 사이의 매핑을 처리하여 데이터의 추상화와 분리를 도와준다. 데이터매퍼는 데이터베이스의 연동과 관련된 작업을 담당한다.

Object Mapper란 무엇인가?

객체 매퍼는 객체 간의 변환을 수행하는 도구이다. 주로 Java 객체와 JSON,XML등의 데이터 포맷사이의 매핑을 처리한다.객체 매퍼는 객체의 필드와 데이터의 속성 간의 매핑을 담당하여 객체 직렬화와 역직렬화를 돕는다.

직렬화(Serialize)와 역직렬화(Deserialize)는 무엇인가?

직렬화는 자바언어에서 객체나 데이터를 다른 컴퓨터의 자바 시스템에서도 사용 할수 있도록 바이트 형태로 포맷 변환하는 기술이다.

역직렬화는 바이트로 변환된 데이터를 원래 자바 객체혹은 데이터로 변환하는 기술이다.

<?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="dev.ldkstellar.mybatis.mapper.PostMapper">

    <insert id="createPost"
            parameterType="dev.ldkstellar.mybatis.dto.PostDto">
        insert into post(title,content,writer,board)
        values (#{title},#{content},#{writer},${board})
    </insert>

    <insert id="createPostAll"
            parameterType="dev.ldkstellar.mybatis.dto.PostDto">
    insert into post(title,content,writer,board)
    values
        <foreach collection="list" item="item" separator=",">
            (#{item.title},#{item.content},#{item.writer},${item.board})
        </foreach>

    </insert>

    <select
            id="readPost"
            parameterType="int"
            resultType="dev.ldkstellar.mybatis.dto.PostDto">
        select * from post where id = ${id}
    </select>
    <select id="readPostAll"
            resultType="dev.ldkstellar.mybatis.dto.PostDto">
        select *from post
    </select>
    <select id="readPostQuery"
            resultType="dev.ldkstellar.mybatis.dto.PostDto">
        select * from post
        where title= #{title}
        <if test="writer != null">
            and writer =#{writer}
        </if>
    </select>

    <update id="updatePost" parameterType="dev.ldkstellar.mybatis.dto.PostDto">
        update post set
                        title = #{title},
                        content = #{content},
                        writer = #{writer},
                        board = ${board}
        where id = ${id}
    </update>
    <delete id="deletePost" parameterType="int">
        delete from post where id = ${id}
    </delete>

</mapper>

직렬화 혹은 역 직렬화 시 sql에서 #{}문자, ${}숫자 형태로 해야된다.

SqlSessionFactory란 무엇인가?

MyBatis라는 Java 기반의 오픈 소스 ORM(Object-Relational Mapping)프레임워크에서 사용되는 중요한 인터페이스이다. 데이터베이스 연결과 관련된 모든 설정 정보를 포함하고, MyBatis의 당향한 기능을 사용하기 위한 진입점 역할을 한다.

@Repository
public class BoardDao {
    private final SqlSessionFactory sessionFactory;

    public BoardDao(@Autowired SqlSessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;

    }

    public int createBoard(BoardDto dto){
        try(SqlSession session = sessionFactory.openSession()){
           BoardMapper mapper =  session.getMapper(BoardMapper.class);
           return mapper.createBoard(dto);
        }
    }
}

0개의 댓글