Error

bad SQL grammar_Oracle과 MySQL의 차이

문제 상황

; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended ]을(를) 발생시켰습니다. java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

insert into - select - 문

	<insert id="insertOrder" parameterType="orderVo">
		<selectKey keyProperty="orderNo" resultType="int" order="BEFORE">
			select orders_seq.nextval from dual
		</selectKey>
		insert into orders(orderNo, customerId, totalPrice, message
			,customerName, hp, zipcode, address, addressDetail )
		values(#{orderNo}, #{customerId}, #{totalPrice}, #{message}, #{customerName},
				#{hp}, #{zipcode}, #{address}, #{addressDetail})
	</insert>
    
        <insert id="insertOrderDetail" parameterType="orderVo">
            insert into orderDetails
            select #{orderNo}, productNo, quantity
            from cart
            where cutomerId=#{customerId}
        </insert>

 

선택한 상품들을 orders 테이블에 insert를 한 후 바로 orderDetails에 insert 처리 하는 상황이였는데, 

학원에서 수업을 들을때는 Oracle로 사용했기때문에

insert into - select - 구문에서 매퍼를 insert 로 사용해도 무방했으나, MySQL 를 사용할땐 방법이 달라진다.

 

해결 방법

 

 💡 Mybatis config 설정
<settings>
<setting name="cacheEnabled" value="true"/>
* MySQL 쓸때에는 꼭 true로 바꿔줘야 하는데, Oracle은 false 해도 됨 * 
<setting name="useGeneratedKeys" value="false"/> =true 라면 에러가 난다.

 💡 알아두자

MySQL 같은 경우에는 Oracle로 지정해두었던 시퀀스 처리하는 방법이 다르기 때문에
(insert 자체에서 번호가 생략 됨 )
insert 가 아닌 update로 변경한다!

 

	<insert id="insertOrder" parameterType="orderVo">
		<selectKey keyProperty="orderNo" resultType="int" order="BEFORE">
			select orders_seq.nextval from dual
		</selectKey>
		insert into orders(orderNo, customerId, totalPrice, message
			,customerName, hp, zipcode, address, addressDetail )
		values(#{orderNo}, #{customerId}, #{totalPrice}, #{message}, #{customerName},
				#{hp}, #{zipcode}, #{address}, #{addressDetail})
	</insert>
	
	<update id="insertOrderDetail" parameterType="orderVo">
		insert into orderDetails
		select #{orderNo}, productNo, quantity
		from cart
		where customerId=#{customerId}
	</update>

 

 

매퍼 변경 후 DAOMybatis  부분에서도 

	@Override
	public int insertOrderDetail(OrderVO vo) {
		return sqlSession.update(namespace+"insertOrderDetail", vo);
	}

sqlSession.update 로 변경해준다.

 

 


아직은 MySQL을 사용하기 전 이기때문에 미숙하지만, 사이드 프로젝트 진행하면서 MySQL 을 사용 할 예정이라 그때는 좀 더 자세하게 기술 할 수 있겠지 싶다 ...