전자정부 프레임워크에서 MyBatis 사용

설명잘된곳


pom.xml
<!--  MyBatis -->
<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.2.2</version>
</dependency>

설정파일

context-mapper.xml // spring 쪽에서 읽어가도록 되어있어야함.

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.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/mybatis/sql-mapper-config.xml" />
<property name="mapperLocations" value="classpath:/egovframework/sqlmap/mybatis/mappers/*.xml" />
</bean>

<!-- MapperConfigurer setup for MyBatis Database Layer with @Mapper("deptMapper") in DeptMapper Interface -->
<!-- @Mapper 어노케이션을 읽어오도록 한다.(그냥 다른거 써도 될것 같은데->안되더라.) -->
  <bean class="egovframework.rte.psl.dataaccess.mapper.MapperConfigurer">
<property name="basePackage" value="temp" />
</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>
    <!-- mapper에서 사용할 패키지까지 표현한 클래스의 별칭, 단순한 별칭이다. 없을 경우 풀패스로 표현 -->
    <typeAliases>
     <typeAlias alias="TempVO" type="temp.example.TempVO" />
    
    </typeAliases>
    
<!-- JAVA파일에서 지정할 것이라서 여기서 지정 안함.
<mappers>
        <mapper resource="META-INF/sqlmap/mappers/mysql/department_mysql.xml" />
        <mapper resource="META-INF/sqlmap/mappers/mysql/employee_mysql.xml" />
   </mappers> 
 -->
 
</configuration>

mapper_SQL_mysql.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="temp.ex.mybatis.ExMapper">
<select id="selectList" parameterType="TempVO" resultType="TempVO">
<![CDATA[
SELECT
USER_NAME,USER_NUMBER
FROM tbl_testp01 
WHERE 1=1
]]>
<if test="user_name != null and user_name != ''">
  AND USER_NAME=#{user_name}
</if>
<if test="user_number != null  and user_number != ''">
  AND USER_NUMBER=#{user_number}
 </if>  
</select>
<insert id="insert_tbl_testp01">
INSERT INTO tbl_testp01 
( user_name
  , user_number )
VALUES ( #{user_name}
  , #{user_number}  )
</insert>
</mapper>


JAVA파일
  • @Controller
    public class ExController
    • @Service("exService")
      public class ExServiceImpl
      • @Mapper("exMapper")
        public interface ExMapper
      • // mapper_SQL_mysql에서 namespace로 지정된 인터페이스, ExServiceImpl 에서 (List<TempVO>) exMapper.selectList(vo); 처럼 바로 호출할 수 있다.
DAO를 없이 사용했다. Mapper로 설정되면 자동으로 DAO로 만들어버리는 듯... 어떻게 그래되는지는 모르겠지만.

쿼리를 어노테이션으로 할 경우

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import egovframework.rte.psl.dataaccess.mapper.Mapper;

import temp.example.TempVO;

@Mapper("exMapperByAnnotation")
public interface ExMapperByAnnotation {
/**
* 글을 조회한다.
* @param vo - 조회할 정보가 담긴 SampleVO
* @return 조회한 글
* @exception Exception
*/
@Select("SELECT USER_NAME,USER_NUMBER FROM tbl_testp01 WHERE 1=1 AND USER_NAME=#{user_name} AND USER_NUMBER=#{user_number}")
List<TempVO> selectListByAnnotation(TempVO vo) throws Exception;
@Insert("INSERT INTO tbl_testp01 ( user_name , user_number ) VALUES ( #{user_name} , #{user_number}  )")
void insert_tbl_testp01ByAnnotation(TempVO vo) throws Exception;
}

mapper를 위처럼 한다.
interface라서 if 등을 처리할 수 없어서 난감하네.
동적 쿼리가 지원안된다.
${XXXX}를 쓸 수 있는지 테스트 해봐야겠네.


댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📷 갤러리형
제목
[기본형] HTML (with 부트스트랩5.3 , jquery 3.7, vue.js)
유용한 리눅스(LINUX) 명령어
[공지] 기술 게시판
2.25
2.27
2.28
3.1
3.2
3.3
3.4
3.5
3.7
3.9
3.11
3.12
3.13
3.14
3.15
3.16
3.17
3.18
3.19
3.20
3.21
3.22
3.23
3.24
3.25
3.27
3.28
3.29
3.30
3.31
4.1
4.2
4.3
4.4
4.5
4.6