반응형



▼ 환경

eGovFrame 3.1 (전자정부프레임워크버전)

spring 3.2.9 release

egov 3.0.0 version

mod-socket-io 1.0.2 < -- 이건 머하는놈인질 모르겟음

◎ XML 설정

▼ 내용

먼저 전자정부에는 context-transaction.xml 이란 xml 파일이 있을꺼다 여기에 tx:advice 와 aop를 설정해주면 된다 .



context-transaction.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="requiredTx" expression="execution(* org.egovframework.karico..impl.*Impl.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx" /> </aop:config> </beans>

◎ JAVA에 작성하기

▼ 내용

XML을 설정하였으면 트랜잭션을 걸 메소드에 아래와 같이 작성하면된다.

본인은 컨트롤러에 작성하였다 컨트롤러 안에는 INSERT문을 실행하는 SERVICE들이 한 4~5개 된다. 만약 아래의 commit 나 rollback 를 안써놓으면 그냥 트랜잭션이 잡혀있는상태로 된다.

톰캣을끄면 그트랜잭션은 사라지고 rollback 되는거같더라.

어쩃든 이렇게 작성해놓으면 commit이나 rollback 둘중 하나가 되기 전까지 디비에 써지진 않는다.

RESTfullReportController.java

@Controller public class RESTfullReportController { @Resource(name = "txManager") protected DataSourceTransactionManager txManager; @RequestMapping("/api/saveInitialCheckReport") public ModelAndView saveInitialCheckReport (HttpServletRequest request, HttpServletResponse response) throws Exception { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus txStatus = txManager.getTransaction(def); try { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ 각종 INSERT UPDATE 등 DB 문 ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ txManager.commit(txStatus); //정상일경우 COMMIT; model.put("result", 0); } catch (Exception e) { e.printStackTrace(); txManager.rollback(txStatus); //에러날경우 CATCH로 빠져서 ROLLBACK; model.put("result", -1); } }


◎ 위에처럼만 하면 끝 생각보다 쉽다..


반응형
반응형

IBATIS 와 MYBATIS는 연동방법이 좀다르다.

IBATIS는 context-sqlMap.xml 을사용하고

MYBATIS는 context-mapper.xml 을 사용한다.

그리고 datasource도

IBATIS 는 org.springframework.jdbc.datasource.DriverManagerDataSource

MYBATIS 는 org.springframework.jdbc.datasource.SimpleDriverDataSource

더 자세한거는 밑에 소스를 참고하시길..

실행환경 SPRING 3.2.9 , EGOV 3.0.0

전자정부는 기본적으로 템플릿에 context-xxxx 이렇게 붙은파일이 나온다.

전자정부는 pom.xml에 mybatis , ibatis 라이브러리를 안써줘도된다.

다 깔려있으니간.


◎ context-mapper.xml (mybatis config 라 생각하셈)

▼ 내용

datasource 의 ref 를 보면 sub_ 를 붙엿다 ibatis, mybatis 둘다 연동을 위해

mapperLocations 의 쿼리 날릴 SQL.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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- MapperConfigurer setup for MyBatis Database Layer with @Mapper("deptMapper") in DeptMapper Interface --> <bean class="egovframework.rte.psl.dataaccess.mapper.MapperConfigurer"> <property name="basePackage" value="org.egovframework.karico.service.impl" /> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="sub_dataSource" /> <property name="mapperLocations" value="classpath:/egovframework/sqlmap/seis_web/sql/NewFile.xml" /> </bean> </beans>

◎ context-sqlMap.xml (Ibatis config 라 생각하셈)


▼ 내용

configLocation 의 SQL.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 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- SqlMap setup for iBATIS Database Layer --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="classpath:/egovframework/sqlmap/seis_web/sql-map-config.xml"/> <property name="dataSource" ref="dataSource"/> </bean> </beans>


◎ context-datasource (mybatis , ibatis 둘다 써놧음)


▼ 내용

abcd

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd"> <!-- <jdbc:embedded-database id="dataSource" type="HSQL"> --> <!-- <jdbc:script location= "classpath:/db/sampledb.sql"/> --> <!-- </jdbc:embedded-database> --> <!-- IBATIS 환경설정--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- MYBATIS 환경설정--> <bean id="sub_dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="${jdbc2.driverClassName}" /> <property name="url" value="${jdbc2.url}" /> <property name="username" value="${jdbc2.username}" /> <property name="password" value="${jdbc2.password}" /> </bean> </beans>

XML 연동은 이걸로 끝이다.

이 이후 service,dao,impl,controller 만 알아서 작성하면된다

ibatis는 생략...

밑에는 mybatis 연동이다.

(내 플젝이 ibatis가 되잇는걸 추가 db 연동으로 mybatis 연동하는거라서)

(ibatis 써논건 귀찮아서 안씀... 궁금하면 댓글 ㄱ)


◎ NewFile.xml (mybatis 쿼리.xml)


▼ 내용

위의 환경설정에 보면 MYBATIS 쿼리 SQL 경로설정하는거에 NewFile로 되잇을거다

난 이 파일명으로 사용하였다. 대충하였기 떄문에....

ibatis sql은 따로 안쓸것이다 (대충 아는사람들은 알겟지... 구지쓸필요없음을)

그래도 모르겠으면 댓글 ㄱ

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="egovframework/sqlmap/seis_web/sql/NewFile"> <select id="sub_station" resultType="Map"> select * from station where STA_NO = '1' </select> </mapper>


◎ NewFileService.java (service 인터페이스다)


▼ 내용

package org.egovframework.karico.dao; import java.util.List; import java.util.Map; public interface NewFileService { public List<Map<String,Object>> getNewFile () throws Exception; }


◎ NewFileServiceImpl.java (service 인터페이스다)


▼ 내용

package org.egovframework.karico.dao; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import egovframework.rte.fdl.cmmn.AbstractServiceImpl; @Service("NewFileService") public class NewFileServiceImpl extends AbstractServiceImpl implements NewFileService { @Resource(name="newfiledao") private NewFileDao newfiledao; @Override public List<Map<String, Object>> getNewFile() throws Exception { return newfiledao.NewFileDaoSelect(); } }


◎ NewFileDao.java


▼ 내용

package org.egovframework.karico.dao; import java.util.List; import java.util.Map; import org.springframework.stereotype.Repository; import egovframework.rte.psl.dataaccess.EgovAbstractMapper; @Repository("newfiledao") public class NewFileDao extends EgovAbstractMapper { public List NewFileDaoSelect() throws Exception { return list("sub_station", null); } }

◎ Controller.java


▼ 내용

@Resource(name = "NewFileService") NewFileService newfileservice; @RequestMapping("/testabcd") public ModelAndView abcdefg (HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println(newfileservice.getNewFile()); return null; }

마무리 하면서 위의 예제들은 진짜 순수 연동만을위한 간단간단하게 쓴것들이다.

초급자 기준으로 쌩판 모르는 사람들 기준으로 쓴건데

이해가 안가는 부분이 있으면 댓글 남기면

성실히 답변해드림


반응형

+ Recent posts