본문 바로가기
Spring

[Spring]mybatis-spring

by whitedeveloper 2022. 10. 25.

Spring + mybatis 사용하기

1.pom.xml

java version
mybatis , mybatis-spring version
mybatis 의존정보

2.root-context.xml

-- 비웹관련 설정 : datasource(db) 설정, 

<--Connection Pool DataSource config : META-INF - context.xml-->

datasource 사용을 위한 설정

>value의 정보와 context.xml의 resource의 정보가 같아야한다.

root-context.xml의 datasource property의 value와 일치시킬 context.xml의 resource name

 

mybatis 관련 설정

-- <property name="mapperLocations" value="classpath:mapper/*.xml"> : mapper.xml location

>resource/mapper-sub-folder > member.xml , board.xml

>reource> member.xml , board.xml

>classpath: mapper > member.xml

dtoclss타입 설정

>위 내용을 <property name="typeAliasesPackage" value="com.ssafy.*.model"></property> 가 dtoclass통합 설정

 

3.META-INF - context.xml

>Connection Pool DataSource 관련 설정 내용 - db변경하려면 이곳에서!!

> url, driver, user, password, jdni name

><Resource name="jdbc/ssafy" username="ssafy" url=""../>

4.resources/Mepper 

  --member.xml (테이블명,엔티티명으로 하면 좋다)

**자바와 db컬럼명의 규칙이 다름

>namespace 지정 : unique(package 명 + class-name)

dto-name 과 table column-name 매핑

type="memberDto" : typeAliasesPackage설정 가능

  --board.xml

5.resources - mybatis-config.xml

필요에 따라서 사용or 비사용

비사용시 : root-context.xml
사용시 : mybatis-config.xml

6.Dao interface

  --MemberMapper, BoardMapper

  --interface 만 설계하면 됨 (구현 클래스 구현할 필요 없음)

  --root-context.xml

mapper할 위치

--@Mapper 어노테이션 생략 가능 - root-context.xml에 설정되어있어 자동 인식

--가독성을 위해 @Mapper사용

 

--interface 매서드 형식(이름, 아규먼트, 반환타입)

MemberMapper(interface)

>>매서드 이름 : id = "idCheck"

>>아규먼트 : parameterType="String" (단일타입 : string, int, map, dto)

>> 반환타입 : resultType="int"

member.xml

 

7.Service

  -- MemberService/MemberServiceImpl

mapper di 인젝션

 

  -- BoardService/BoardServiceImpl

'Spring' 카테고리의 다른 글

[Spring]Mybatis- SqlSessionFactory  (0) 2022.10.25
[Spring]Spring JDBC, DataSource  (0) 2022.10.25
[Spring]MVC  (0) 2022.10.21
[Spring]spring project  (0) 2022.10.21
[Spring] spring Framework  (0) 2022.10.19