2013년 7월 31일 수요일

SimpleJdbcTemplate - SQL 파라미터

SimpleJdbcTemplate에 작업을 요청할 때는 문자열의 SQL을 제공해야 한다.
String 연산으로 SQL을 직접 만들었을 때 SQL 주입 공격 등에 노출 될 수 있다.
보통 Insert into table(colum1,col2) values(?,?); 와 같이 위치 치환자를 두고 파라미터를 바인딩한다.
또, SimpleJdbcTemplate은 이름을 통한 치환자 기능도 제공한다.
위의 SQL은 Intert into table (col1,col2) values (:name1,:name2); 와 같이 작성할 수 있다.
이름을 이용해 바인딩하기 때문에 중간에 순서가 바뀌어도 파라미터 바인딩에는 영향을 주지 않는다. 그리고 맵이나 오브젝트에 담긴 내용을 키 값이나 프로퍼티 이름을 이용해 바인딩 할 수 있다는 것이다.

출처: 토비의 스프링3, 이일민, 에이콘 http://www.acornpub.co.kr/book/toby-spring3

출처: http://forum.springsource.org/showthread.php?79470-Best-way-to-do-data-file-load-into-DB-table

클래스를 만들어서 jdbc에 접근하는 예제는 아래 참조를 보면 된다. 나는 위의 출처에서 xml 파일로 jdbc에 접근하는 예제를 사용했다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:batch="http://www.springframework.org/schema/batch" 
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
  
 <batch:job id="fixedLengthFileDbDumpJob">
  <batch:step id="step1">
   <batch:tasklet>
    <batch:chunk reader="itemReader" writer="itemWriter" commit-interval="2"/>
   </batch:tasklet>
  </batch:step>
 </batch:job>
 <bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
  <property name="resource" value="file:/#{jobParameters[FILEPATH]}"/>
  <property name="lineMapper">
   <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
    <property name="lineTokenizer">
     <bean
      class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
      <property name="names" value="컬럼1,컬럼2,컬럼3, 컬럼4" />
      <property name="delimiter">
       <util:constant static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_TAB"/>
     </bean>
    </property>
    <property name="fieldSetMapper">
     <bean
      class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
      <property name="targetType"
       value="my.custom.data.type" />
     </bean>
    </property>
   </bean>
  </property>
 </bean>

 <bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
  <property name="itemSqlParameterSourceProvider">
   <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
  </property>
  <property name="sql" value="insert into 테이블이름 (칼럼1이름, 칼럼2이름, 칼럼3이름, 칼럼4이름) values (:컬럼1,:컬럼2,:컬럼3, :컬럼4)" />                            
  <property name="assertUpdates" value="true"/>
   <property name="dataSource" ref="dataSource"/>
 </bean>

</beans>


참조: http://stackoverflow.com/questions/751400/how-to-xml-configure-spring-bean-for-constructor-injection-when-bean-has-varargs
http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.html
http://www.example8.com/category/view/id/452
http://www.java2s.com/Code/Java/Spring/SimpleJdbcInsertWithBeanPropertySqlParameterSource.htm
http://winmargo.tistory.com/118

댓글 없음:

댓글 쓰기