//Google AdSense
<!-- 처리업무 등록 -->
	<insert id="unpaidInsert" parameterType="Unpaid">
		
		<!-- unpaidCode -->
		<selectKey order="BEFORE" keyProperty="unpaidCode" resultType="String">
			select
				(case count(*)
				when 0 then 'unpaid_1'
				else CONCAT('unpaid_',max(cast(substring(unpaid_code,8) as decimal))+1)
				end) as unpaidCode
			from
				tb_unpaid
		</selectKey>
		<selectKey order="BEFORE" keyProperty="customerCode" resultType="String">
			SELECT
				c.customer_code
			from
				tb_customer AS c
				where
					#{customerName} = c.customer_name
					and
					#{customerTel} = c.customer_tel
		</selectKey>
		<selectKey order="BEFORE" keyProperty="unpaidRegStaffCode" resultType="String">
			SELECT
				sf.staff_code
			from
				tb_staff as sf
				join
				tb_member as m
				on
				sf.member_id = m.member_id
				join
				tb_store AS sr
				on
				sf.store_code =sr.store_code
			where
				#{unpaidRegMemberName} = m.member_name
				and
				#{unpaidRegStoreName} = sr.store_name	
		</selectKey>

		INSERT INTO tb_unpaid
			(unpaid_code
			, unpaid_exp_date
			, unpaid_part
			, unpaid_sub_name
			, unpaid_desc
			, customer_code
			, unpaid_memo
			, unpaid_status
			, unpaid_reg_date
			, unpaid_reg_staff_code)
		VALUES 
			(#{unpaidCode}
			, NOW()
			, #{unpaidPart}
			, #{unpaidSubName}
			, #{unpaidDesc}
			, #{customerCode}
			, #{unpaidMemo}
			, #{unpaidStatus}
			, NOW()
			, #{unpaidRegStaffCode})

	</insert>

mapper.xml 에서 <insert></insert> 안에 <selectKey></selectKey>를 여러개 사용하여 여러가지의 조인 값을 가져오고싶었다! 하지만 계속 오류가 발생했다.

알고보니 <selectKey></selectKey> 는 한개만 사용할 수 있다고 한다!!


<!-- 처리업무 등록 -->
	<insert id="unpaidInsert" parameterType="Unpaid">
		<selectKey order="BEFORE" keyProperty="unpaidCode,customerCode,unpaidRegStaffCode" resultType="Map">
			select
				(case count(*)
				when 0 then 'unpaid_1'
				else CONCAT('unpaid_',max(cast(substring(unpaid_code,8) as decimal))+1)
				end) as unpaidCode
				,tc.customer_code as customerCode
				,tc.staff_code as unpaidRegStaffCode
			from
				tb_unpaid AS u ,(
							SELECT
								c.customer_code,
								tf.staff_code
							from
								tb_customer AS c,
								(
								SELECT
								sf.staff_code
								from
									tb_staff as sf
									join
									tb_member as m
									on
									sf.member_id = m.member_id
									join
									tb_store AS sr
									on
									sf.store_code =sr.store_code
								where
									#{unpaidRegMemberName} = m.member_name
									and
									#{unpaidRegStoreName} = sr.store_name
									)AS tf
			where
				#{customerName} = c.customer_name
				and
				#{customerTel} = c.customer_tel
				)AS tc
		</selectKey>

		INSERT INTO tb_unpaid
			(unpaid_code
			, unpaid_exp_date
			, unpaid_part
			, unpaid_sub_name
			, unpaid_desc
			, customer_code
			, unpaid_memo
			, unpaid_status
			, unpaid_reg_date
			, unpaid_reg_staff_code)
		VALUES 
			(#{unpaidCode}
			, NOW()
			, #{unpaidPart}
			, #{unpaidSubName}
			, #{unpaidDesc}
			, #{customerCode}
			, #{unpaidMemo}
			, #{unpaidStatus}
			, NOW()
			, #{unpaidRegStaffCode})
	</insert>

그래서 서브쿼리를 사용하여 여러개의 컬럼을 한개의 selectKey에 담았다.

insert 쿼리문에서 값을 받을 때 #{paramater}와 selectKey의 select문의 alias가 대응된다~!!


친구랑 대화하다보니 하나의 서비스에서 처리하지 않고 서비스를 나누어서 처리하는 방법도 있을 거라고 깨달았다!!

서비스를 나누어 처리하면 서브쿼리를 쓰지 않고도 처리할 수 있을 것이다.

서비스를 하나로 만들면 서비스를 나눈 것 보다 유지보수 측면에서 이득이라고 하니 상황에 따라 골라서 사용해야겠다.

또, 요즘 join이 없도록 작업하는 추세라고 하는데!!

정규화를 막 배운 입장에서는 아직 실제로 사용하기에는 정규화가 마음에 걸린다...

하지만 서브쿼리 안에!! 서브쿼리 안에!! 서브쿼리를 넣다 보니 join이 없는 작업이 더 편리할 것 같다고 느꼈다.

지금 하고있는 프로젝트는 작은 규모이기 때문에 데이터베이스에 데이터가 적어서 체감을 못했지만, join이 많이 일어나면  데이터가 많이 존재할 경우 처리속도가 느려진다고 하니 참고해야겠다!!

 

 

 

+ Recent posts