예시로 calendar라는 패키지를 생성하였다.
(이름만 calendar고 DB 연결은 전자정부 게시판을 이용하였당)
전자정부는 기본적으로 ibatis 설정이 되어있고 mybatis로 변경하려면 몇가지 설정이 필요하다.
├─java
│ │ │ └─egovframework
│ │ │ │ ├─calendar
│ │ │ │ │ ├─service - service, mapper, vo
│ │ │ │ │ │ └─impl - serviceimpl
│ │ │ │ │ ├─sqlmap
│ │ │ │ │ └─web
ibatis 관련 목록 삭제(context-sqlMap, sql-map)
mybatis 관련 파일 네이밍은 mapper, ibatis 관련 파일 네이밍은 map이다.
<?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"/>
<typeAlias alias="calendarVO" type="egovframework.calendar.service.CalendarVO"/>
</typeAliases>
</configuration>
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-4.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/oracle/sql-mapper-config.xml" />
<property name="mapperLocations">
<list>
<value>classpath:/egovframework/*/sqlmap/*.xml</value>
</list>
</property>
</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" />
</bean>
</beans>
public class CalendarVO {
private String bbsId;
public String getBbsId() {
return bbsId;
}
public void setBbsId(String bbsId) {
this.bbsId = bbsId;
}
}
sql.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="egovframework.calendar.service.CalendarMapper">
<resultMap type="calendarVO" id="calendarResult">
<result property="bbsId" column="BBSID"/>
</resultMap>
<select id="selectTime" resultType="String">
SELECT bbs_id FROM Lettnbbsmaster WHERE bbs_id = #{bbsId}
</select>
</mapper>
mapper
@Mapper("CalendarMapper")
public interface CalendarMapper {
public String selectTime(String id) throws Exception;
}
@Service("CalendarService")
public class CalendarServiceImpl implements CalendarService{
@Autowired
CalendarMapper calendarMapper;
@Override
public String selectTime(String id) throws Exception {
return calendarMapper.selectTime(id);
}
}
public interface service {
String selectTime(String id) throws Exception;
}
사용할 서비스를 상단에 명시해준 후 관련 메소드를 추가하여 사용해준다.
아래 예시는 연결이 잘 되나 테스트용으로 작성한 코드기 때문에 이렇게 사용하는구나만 알길 바란다..ㅠㅠ
@Resource(name = "CalendarService")
private CalendarService calendarService;
@RequestMapping("/test")
public String test(
HttpServletRequest request) throws Exception {
CalendarVO vo = new CalendarVO();
vo.setBbsId("BBSMSTR_AAAAAAAAAAAA");
String result = calendarService.selectTime(vo.getBbsId());
if(result == null) {
result = "다시 테스트";
}
return result;
}
ibatis와 mybatis의 차이
Spring boot에서 보는 Controller, Service, DAO, Mapper
스프링 MVC 구조란?