Spring : mybatis 연동

2020. 8. 23. 14:57·WEB/Spring

<Mybatis>

 

 

정의: 객체지향 언어인 자바의 관계형 DB 프로그래밍을 쉽게 할수 있도록 도와주는 개발 프레임워크

 

특징:

  • 간단한 자바코드로 db 연동 처리
  • 자바코드에서 분리된 sql 명령어 (쿼리) > xml파일에서 관리 (mapper라고 칭함)

 

<활용하기>

 

 

1. pom.xml에 library 추가 [ https://mvnrepository.com/ 여기서 검색 가능 ]

 

  • spring-jdbc (스프링의 JDBC, Spring version과 동일 > log4jdbc 사용)
  • mysql-connector
  • mybatis
  • mybatis-spring (스프링에서 Mybatis 연동을 위한 모듈)
  • spring-test

 

2. mybatis-config.xml 설정 파일 생성 [위치 : Java Resources/src/main/resources/]

 

해당 파일에 아래의 코드 추가 > xml 유효성 체크를 위해 필요

<!DOCTYPE configuration PUBLIC "-//mybatis.org/DTD Config 3.0/EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 

 

*해당 파일에 아래와 같이 별명을 설정할 수 있는 데, 바로 뒤에 나오는 mapper 파일에서 com.web.beans.Emp (java파일) 을 사용할 때, EmpVO라는 짧은 별명으로 대신 사용하겠다는 것을 코드로 적은 것임

 

<configuration>

<typeAliases>

<typeAlias alias="EmpVO" type="com.webcamp.beans.Emp" />

</typeAliases>

</configuration>

 

3. Mapper 생성 : employee-mapper.xml (이름은 상관없음 but mapper라는 단어가 들어간 이름이면 좋음), 쿼리를 추가하는 파일

[위치 : Java Resources/src/main/resources/]

 

해당 코드 추가

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

 

*아래 코드는 예시 코드 : 이런식으로 쿼리를 작성하면 됨

<mapper namespace="Employees">

<select id="Select" parameterType="string" resultType="EmpVO"> SELECT * FROM emp99 </select>

<insert id="Insert" parameterType="EmpVO"> INSERT INTO emp99 (name, salary, designation) VALUES (#{name}, #{salary}, #{designation}) </insert>

</mapper>

 

4. root file 설정 : 어디서 쿼리를 불러오는지, db정보는 무엇인지 기입

 

아래코드 추가하기

 

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName"

value="com.mysql.jdbc.Driver" /> <!--mysql db사용, 다른 db 사용시 변경해야함-->

<property name="url" value="db url" />

<property name="username" value="db 아이디" />

<property name="password" value="db 비밀번호" />

</bean>  <!--해당 db 정보-->

 

<bean id="sqlSessionFactory"

class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="configLocation"

value="classpath:/mybatis-config.xml" />

<property name="mapperLocations"

value="classpath:/*-mapper.xml" />

</bean>

 

<bean id="sqlSession"

class="org.mybatis.spring.SqlSessionTemplate"

destroy-method="clearCache">

<constructor-arg name="sqlSessionFactory"

ref="sqlSessionFactory" />

</bean>

 

5. 관련 Dao (+interface), Service(+interface), Controller 생성하기 

'WEB > Spring' 카테고리의 다른 글

Spring : 응용 프로그램을 실행할 권한이 없습니다 오류  (2) 2021.12.02
Spring : Maven Dependencies 문제 해결법  (0) 2021.04.05
Spring : Ajax 동기/비동기  (0) 2021.01.29
Spring : Github clone 받기  (0) 2021.01.29
에러 해결법 : HTTP Status 500 - JDK 버전 문제  (0) 2020.08.12
'WEB/Spring' 카테고리의 다른 글
  • Spring : Maven Dependencies 문제 해결법
  • Spring : Ajax 동기/비동기
  • Spring : Github clone 받기
  • 에러 해결법 : HTTP Status 500 - JDK 버전 문제
HBean_
HBean_
백엔드 개발자의 개발 로그 💻
  • HBean_
    개발_log
    HBean_
  • 전체
    오늘
    어제
    • 전체 (102)
      • WEB (49)
        • Spring (14)
        • AWS EC2 (6)
        • DB (3)
        • 2020_webCamp (25)
        • JPA (1)
      • Devops (2)
      • 보안 (4)
      • Git (6)
      • JAVA (13)
      • 자료구조 (2)
      • 알고리즘 (10)
      • 네트워크 (2)
      • SStudy (2)
      • 실전프로젝트2 (4)
      • 개발 일기 (1)
      • 개발툴 (4)
      • Intellij (2)
      • 이슈 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • GITHUB
  • 공지사항

  • 인기 글

  • 태그

    웹
    플러그인
    인텔리제이
    tomcat
    IntelliJ
    톰캣
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
HBean_
Spring : mybatis 연동
상단으로

티스토리툴바