본문 바로가기
Web Development/Spring

전자정부프레임워크 마이바티스 설정

by 피치피치어피치 2017. 12. 29.

-전자정부프레임워크 설치 후 eGovFrame Web Project로 프로젝트를 생성하면 기본 게시판이 만들어져있다.

따로 DB에 연결되어있는건 아니고 서버를 죽이면 사라지는 휘발성 데이터로 구성되어있고 DB를 연동한다

하더라도 ibatis 연동이 되어있어서 mybatis로 바꿔주는 작업을 했다.

 

-바꿔줄 파일 list 

1 . src/main/resources/spring/context-datasource.xml -> DB연결 설정

2 . src/main/resources/spring/context-mapper.xml -> mybatis mapper 경로설정, vo alias경로 설정

3 . src/main/resources/egovframework/sqlmap/example/mappers/EgovSample_Sample_SQL.xml

    -> mapper id 및 namespace 변경

 

-삭제 파일 list

1 . src/main/resources/egovframework/sqlmap/example/sample/EgovSample_Sample_SQL.xml

    ->ibatis관련 파일

2 . src/main/resources/egovframework/sqlmap/example/sql-map-config.xml ->ibatis관련 파일

3 . src/main/resources/spring/context-sqlMap.xml -> ibatis 관련 설정파일

 

-수정내역

1.context-datasource.xml

 

DBMS별로 설정소스가 주석처리 되어있음 원하는 DBMS 주석 풀고 계정과 DB정보 입력해주면 됨

 

<?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">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/dbname" />
    <property name="username" value="dbaccount"/>
    <property name="password" value="dbpassword"/>
</bean>
</beans>

 

2.context-mapper.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">

    <!-- SqlSession setup for MyBatis Database Layer -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/egovframework/sqlmap/example/sql-mapper-config.xml" />
        <property name="mapperLocations" value="classpath:/egovframework/sqlmap/example/mappers/*.xml" />
    </bean>
</beans>

 

3.EgovSample_Sample_SQL.xml

 

namespace는 SampleMapper랑 연결되어있는데 SampleDAO로 바꿔주고 SampleMapper.java 삭제

 

insert ,update, delete, select id는 SampleDAO 리턴값이랑 일치시켜줘야함

 

<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.example.sample.service.impl.SampleDAO">

<insert id="sampleDAO_insertSample" parameterType="SampleVO">
<!--SampleDAO에 리턴값이 sampleDAO_insertSample이여야 제대로 매핑이 됨-->
<!--이하 생략-->