반응형
공통 설정파일
DB 연결 정보 관리 위한 properties 설정
context-common.xml
<!-- properties 설정-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/egovframework/spring/properties/global.properties</value>
</list>
</property>
</bean>
..
<!-- properties 설정-->
<util:properties id="globalProperties" location="classpath:/egovframework/spring/properties/global.properties" />
db 연결 관리를 위한 dataSource 설정
- 해당 파일에서는 db Drive 와 주소, 아이디와 비번을 관리하는 빈을 등록한다
- context-datasource.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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
<!-- PostgreSQL iBatis -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="url" value="${global.postgres.url}"/>
<property name="username" value="${global.postgres.id}"/>
<property name="password" value="${global.postgres.pw}"/>
</bean>
<!-- Postgis MyBatis -->
<bean id="dataSource2" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="url" value="${global.postgis.url}"/>
<property name="username" value="${global.postgis.id}"/>
<property name="password" value="${global.postgis.pw}"/>
</bean>
</beans>
iBatis
- iBatis 는 sql-map 으로 관리되기 때문에 context-sqlMap.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">
<!-- lob Handler -->
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" />
<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient" class="egovframework.rte.psl.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:/egovframework/sqlmap/sql-map-config.xml"/>
<property name="dataSource" ref="dataSource"/>
<property name="lobHandler" ref="lobHandler"/>
</bean>
</beans>
sql-map-config.xml 파일로 관리한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="egovframework/sqlmap/sql/Postgresql.xml"></sqlMap>
</sqlMapConfig>
위 설정을 통해 실제로 DAO 와 연동되어 db 처리를 하는 xml 은 아래 형식과 같다
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="sqlMaps">
<typeAlias alias="egovMap"
type="egovframework.rte.psl.dataaccess.util.EgovMap"></typeAlias>
<typeAlias alias="boardVO"
type="egovframework.test02.main.service.vo.BoardVO"></typeAlias>
<select id="boardDAO.boardList" resultClass="hashMap">
select * from
board
order by id desc;
</select>
myBatis
- myBatis 는 mapper로 관리하기에 context-mapper.xml 과
(여기서 두개의 dataSoruce 를 사용할때 property name 은 dataSource 로 고정하고 뒤의 ref 이름을 바꿔주면 된다.)
<?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-4.0.xsd">
<!-- SqlSession setup for MyBatis Database Layer -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="configLocation" value="classpath:/egovframework/sqlmap/sql-mapper-config.xml" />
<property name="mapperLocations" value="classpath:/egovframework/sqlmap/mappers/*.xml" />
</bean>
<!-- MapperConfigurer setup for MyBatis Database Layer with @Mapper("deptMapper") in DeptMapper Interface
<bean class="egovframework.rte.psl.dataaccess.mapper.MapperConfigurer">
<property name="basePackage" value="egovframework.example.sample.service.impl" />
</bean>
-->
</beans>
sql-mapper-config.xml 을 사용한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="egovMap" type="egovframework.rte.psl.dataaccess.util.EgovMap"/>
</typeAliases>
</configuration>
위 설정을 통해 실제로 DAO 와 연동되어 db 처리를 하는 xml 은 아래 형식과 같다
<?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="userDAO">
<select id="selectDbTest" resultType="String">
SELECT count(*) from board;
</select>
</mapper>
xml 을 다루는 DAO 에서도 차이가 있다.
- 상속받는 전자정보의 클래스가 다르며, ibatis 의 경우 sqlMapClient 도 주입해야함
iBatis
@Repository("boardDAO")
public class boardDAO extends EgovAbstractDAO {
@Autowired
SqlMapClient sqlMapClient;
myBatis
@Repository("userDAO")
public class UserDAO extends EgovAbstractMapper{
반응형
'Spring > Spring (Legacy)' 카테고리의 다른 글
[전자정부] RestController / ResponseBody 응답 404 일때 (0) | 2024.10.22 |
---|---|
스프링 스케쥴링 사용법 (0) | 2024.08.19 |
마이바티스 result 로 vo 2개 설정하기 (0) | 2024.07.10 |
[전자정부 Egov] Swagger 적용방법(스프링 레거시) (1) | 2024.05.28 |
스프링 현재 Session 값 확인 (0) | 2024.05.05 |