DTO를 매개변수로 보낼 때

 

 

복수개의 DTO를 보낼때에는 Map에 키값을 달아서 전달 한다.

 


Map 방식 외의 방법으로 전달하는 법

ㅡ mybatis multiple parameter : @Mapper @Param

 


DTO 필드명과 데이터베이스 컬럼 이름이 다른 경우

 

테이블에서는 name, DTO에서는 writer 라면

 

resultMap 태그로 mapping 정보를 만들어준다.

 

 

생략된 컬럼은 자동 mapping


동적 쿼리

상세 검색

 

selectOne과 selectAll 통합 select

if 조건이 true일 경우에만

 

 

 

 

 


Mapper의 쿼리가 동적으로 동작 하는 경우

어떻게 매개변수 존재를 구분해서 호출할 것인가?

 

컨트롤러 서비스 dao에

매개변수 seq를 무조건 넣어주자

 

- seq를 입력한 경우 정상동작

- seq 값이 없는 경우 에러 발생

 

 

int seq 대신에 검색전용DTO를 매개변수로 전달

 


 

'' 값 없음의 경우 null을 integer 할수 없으므로 받을 수 없음

 

Integer 로 받으면 null도 받을 수 있다.

 

기본자료형을 둘러싸는 자료형

Wrapper Class

 

 

 

검색용으로는 dto로 받는 것이 적합

 

검색용도 dto라면 필드 자료형 int말고 Integer로 변경

 

wrapper class는 자동으로 형변환 된다

첫글자 대문자 -> 클래스로 만들어짐

Auto Boxing 으로 인해 getter setter 말고는 따로 코드를 수정해줄 필요없음

// Auto Boxing

Integer i = 10;

Integer i = new Integer(10);

Object o = 10;

 

integer 는 값이 없을 시 null로 전달

String은 값이 없을 시 ''로 전달

-> 전달되는 값에 맞추어 조건을 설정해야 한다.

 


조건이 여러개일 경우

 

 

seq name 둘다 비어있을 경우 전체 목록

 

둘중 하나만 비어있을 경우 조건 검색

 

둘다 존재할 경우 where 이 두번?

 

trim 태그

 

trim태그 inner 영역에 어떠한 값이든 존재한다면 앞에 where을 붙인다.

 

 

prefixOverrides : 발생한 텍스트의 접두사를 삭제

생성된 구문의 가장 앞이 "and"라면 오버라이드 (지워버림)

 

 

'디지털 컨버전스 > Spring' 카테고리의 다른 글

[Spring Framework] AOP  (0) 2020.06.10
[Spring Framework] Scheduler  (0) 2020.06.10
[Spring Framework] MyBatis - update  (0) 2020.06.09
[Spring Framework] MyBatis - select  (0) 2020.06.09
[Spring Framework] MyBatis 에러  (0) 2020.06.09

 

컬럼명에서는 따옴표 쓰면 안된다.

따라서 #{}이 아닌 ${}로

 

'디지털 컨버전스 > Spring' 카테고리의 다른 글

[Spring Framework] Scheduler  (0) 2020.06.10
[Spring Framework] MyBatis  (0) 2020.06.10
[Spring Framework] MyBatis - select  (0) 2020.06.09
[Spring Framework] MyBatis 에러  (0) 2020.06.09
[Spring Framework] MyBatis - insert  (0) 2020.06.09

MyBatis는 업그레이드 된 버전

 

그 이전에 iBatis가 존재

 

데이터베이스 관련 작업에 특화된 라이브러리

코드와 쿼리문을 분리 할 수 있다.

 

쿼리문을 xml에 독립적으로 작성하기 때문에 이식성이 좋아짐

 

분리 되기 때문에 가독성도 좋다.

 

상세검색 기능 등 복잡한 쿼리 조립

조립식 동적 쿼리를 만들 때 MyBatis가 더 유리

 

정석적으로는 넘어오는 타입을 명시해야 하지만 없어도 인식가능

parameterType

 

 

 

 

select 태그를 사용할 때는 resultType을 명세해야 한다.

insert, update, delete는 어쩌피 int 리턴임

 

 

select 된 데이터 마다 DTO를 만들고 이름에 맞추어서 값 넣어줌

while이나 lowMapper 기능도 필요없다

 

쿼리에서 꺼내는 컬럼명이 resultType으로 명세한 DTO의 필드명과 동일하다는 전제 하에

 

만약 컬럼명과 DTO필드명이 다른 경우가 생긴다면 거기에 맞는 태그가 존재

 

 

출력

 


selectone

 

int 값 따옴표 안에 넣어도 검색된다.

 

 #{seq} 는 dto.getSeq로 도장

넘오는 값이 하나일 경우 #{value} 라고 작성한다.

 


value가 여러개여야 할 경우 (범위 검색)

매번 DTO를 만들기 않고

Map 사용


selectCount

 

'디지털 컨버전스 > Spring' 카테고리의 다른 글

[Spring Framework] MyBatis  (0) 2020.06.10
[Spring Framework] MyBatis - update  (0) 2020.06.09
[Spring Framework] MyBatis 에러  (0) 2020.06.09
[Spring Framework] MyBatis - insert  (0) 2020.06.09
[Spring Framework] 정리  (0) 2020.06.08

새 프로젝트 만들기

버전 세팅


 

 

컨트롤러의 리퀘스트 등의 기능은 안드로이드나 네이티브 등에서는 변경된다.

데이터베이스와 컨트롤러는 재활용성이 없다

 

서비스는 스프링과 자바를 쓰는한 계속 재활용 가능

 

-> 계층을 더 나누자 : MyBatis

 

 

쿼리문과 DAO의 자바코드 분리!

 

이상적으로는 데이터베이스 담당자는 자바코드 없이 데이터베이스만

XML만 복사하면 다른 언어에서도 사용가능

마이베티스는 자바에서 주로 쓰지만 약간의 연동을 거쳐 다른 언어에도 사용 가능


메세지 CRUD

 


component 스캔 범위 controller 밖에도 하도록


라이브러리 추가

ojdbc6

dbcp

스프링jdbc에 의존성

 

 

		<!-- DB Related -->
		<dependency>
			<groupId>oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.7.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>5.1.9.RELEASE</version>
		</dependency>
	<repositories>
		<repository>
			<id>OJDBC6 Repo</id>
			<url>http://www.datanucleus.org/downloads/maven2/</url>
		</repository>
	</repositories>

 

 

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.2</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.0</version>
		</dependency>


bean 추가


프로퍼티로 세팅

- 의존성 주입

- mapperLocations : 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
		<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
			<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
			<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
			<property name="username" value="kh"/>
			<property name="password" value="kh"/>
			<property name="initialSize" value="30"/>
		</bean>
		
		<bean id ="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource"/>
			<property name="mapperLocations" value="classpath:/mappers/*-mapper.xml/"></property>
		</bean>
</beans>

 

 

 


편의성을 위한 플러그인 설치

 

 

 

namespace 변경

 

쿼리문 작성시 ; 붙이지 않는다

?,? 대신에

 

 

어떤 xml의 기능을 쓰고 싶은지 인자값에 넣어줘야 함

 

전달된 dto의 dto.getName 과 동일한 기능 #{name}

 

EL 표현과 유사 ${name}하지만 #을 붙이는 경우 해당 값에 따옴표로 둘러짐

 

즉 문자열 처럼 따옴표가 필요한 경우 #을 쓰는것이 편하다


 

서버 가동


MyBatisPractice.zip
0.03MB

'디지털 컨버전스 > Spring' 카테고리의 다른 글

[Spring Framework] MyBatis - select  (0) 2020.06.09
[Spring Framework] MyBatis 에러  (0) 2020.06.09
[Spring Framework] 정리  (0) 2020.06.08
[Spring Framework] 댓글  (0) 2020.06.08
[Spring Framework] 파일 다운로드  (0) 2020.06.08

+ Recent posts