본문 바로가기

spring framework 인프런강의

Spring WEB MVC03 게시판 영속계층 구현하기(Mapper Interface+ XML)

일단 persistance tire부터 

즉 간단히 애기하면 데이터베이스 안에 들어가는 메서드 이름 , xml 파일 만들기 

 

 

 

 

그다음 xml 파일 생성

 

 

https://mybatis.org/mybatis-3/getting-started.html

 

그다음 위에 있는 태그 복붙 ㄱㄱ

 

 

메퍼 인터페이스와 메퍼 파일이 연동할려면  네임 스페이스가 같아야함 

 

 

 

매핑을 시켜줌

 

xml 안에 있는 태그의 id 와 인터페이스쪽에 메소드이름을 같게 해주면 스프링이 알아서 연동해줌

 

 

그다음데이터 베이스 매핑시킬수 있도록 환겨설정

 

root-context.xml

오류나니깐 

이 밑에거 복 붙 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="
           http://mybatis.org/schema/mybatis-spring 
           http://mybatis.org/schema/mybatis-spring-1.2.xsd
           http://www.springframework.org/schema/beans 
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           https://www.springframework.org/schema/context/spring-context.xsd">

 

 

그다음  이것도 복붙 

    <!-- MyBatis sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="/WEB-INF/mybatis/config.xml"/>
    </bean>

    <!-- jdbc 연결 부분(DataSource) -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

    <!-- db properties 파일 연결 -->
    <bean id="propertySourcesPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 수정된 부분 -->
        <property name="locations" value="/WEB-INF/mybatis/db.properties"/>
    </bean>
    
     <mybatis-spring:scan  base-package="kr.inflearn.mapper" />

 

 

그다음 

  <property name="locations" value="/WEB-INF/mybatis/db.properties"/> 이거 파일하고 

        <property name="configLocation" value="/WEB-INF/mybatis/config.xml"/> 이거 두개 파일 만들기 

 

즉 WEB-INF 밑에 mybits 폴더 밑에 config.xml db.priperties 만들기

 config.xml 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--     <properties resource="kr/bit/mybatis/db.properties"/> -->     
<typeAliases>
         <typeAlias type="kr.inflearn.model.BoardVO" alias="boardVO"></typeAlias>        
  </typeAliases>   

</configuration>

 

 

db.properties

 

 

그다음 api 다운로드를 위해 

pom.xml 에

dependency 태그 사이에 붙여 넣기 

 

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>

 

 

그다음 돌아와서 

 

처음에 스프링 이 실행될때 

 

저기 root-context-xml이 실행이됨

 

 

그럼 여기거 스캔할때  패키지이름보고 

 

@Mapper 어노테이션으로 매핑을 걸어준쪽으로 연결