ICE4에서 Mybatis를 사용하기위한 기본 가이드
Profile .yml
활성화된 profile 의 application yml 파일에 Mybatis mapper classpath가 설정되어 있어야 한다.
src/main/resources/application-{active profile name}.yml
application-dxp-loc.yml
spring: profiles: active: dxp-loc
Mybatis mapper classpath 설정
ice-mybatis: mapper-locations: classpath:mybatis/*.xml
Datasource
접속하고자 하는 DB연결에 필요한 정보를 .json 으로 작성
src/main/resources/schema/profiles/{active profile name}/datasource.json
Field | Description | |
---|---|---|
typeId | datasource 값 고정 | |
id | datasource ID | |
ssl | ssl 여부 | true / false |
dsName | datasource 이름 | |
jdbcUrl | DB url | |
dbType | DB 유형 Oracle, MySQL, Maria, MS-SQL 지원 | CODE
|
username | DB user ID | |
password | DB user Password |
[ { "typeId" : "datasource", "id" : "mySqlDb", "ssl" : false, "dsName" : "Test DB (Maria)", "jdbcUrl" : "jdbc:mysql://125.131.88.77:3306/ice2", "dbType" : "maria", "username" : "ice2", "password" : "ice2" }, { "typeId" : "datasource", "id" : "oracleDb", "ssl" : false, "dsName" : "Test DB (Oracle)", "jdbcUrl" : "jdbc:oracle:thin:@125.131.88.69:8200:ORCL", "dbType" : "oracle", "username" : "ICE", "password" : "ICE1234" }, { "typeId" : "datasource", "id" : "msDb", "ssl" : false, "dsName" : "Test DB (MS)", "jdbcUrl" : "jdbc:sqlserver://125.131.88.94:1500;databaseName=IPS", "dbType" : "msSql", "username" : "icafe13", "password" : "icafe1234!@" } ]
Mapper Interface
Mapper 설정 파일(xml)에 있는 SQL 쿼리문을 호출하기 위한 인터페이스
@Mapper annotation 사용
SQL id는 인터페이스에 정의된 메서드명과 동일하게 작성
SQL returnType은 인터페이스의 리턴 타입과 동일하게 작성
src/main/java/net/ion/ice/services/mybatis/mapper/
/src/main/java/net/ion/ice/services/mybatis/mapper/SampleLogicMybatis.java
@Mapper public interface SampleLogicMybatis { public List<Map> list(); }
Mapper XML
MyBatis Mapper XML 파일은 실행할 SQL문을 정의해놓은 파일
src/main/resources/mybatis/
namespace에 인터페이스의 전체 경로를 기입
<mapper namespace="net.ion.ice.services.mybatis.mapper.SampleLogicMybatis">
resultType, parameterType : full package + class 명칭을 명시
resultType="org.springframework.util.LinkedCaseInsensitiveMap"
[ 참조 : https://mybatis.org/mybatis-3/ko/sqlmap-xml.html ]
src/main/resources/mybatis/sampleLogicMybatis.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="net.ion.ice.services.mybatis.mapper.SampleLogicMybatis"> <select id="list" resultType="org.springframework.util.LinkedCaseInsensitiveMap"> select a.NID as id , a.CONTENTS_TITLE as title , a.RELEASE_DATE as releaseDate , a.CONTENTS_SYNOPSIS as contents , a.BASE_PRICE as price , b.SERIES_ID as seriesId , b.TITLE as seriesTitle from MOVIE a, MOVIE_SERIES b where a.SERIES_ID = b.SERIES_ID <if test="id != null"> and a.NID = #{id} </if> </select> </mapper>
Service
mybatisService.getSqlSessionTemplate("oracleDb")
src/main/java/net/ion/ice/services/
[참조 : https://mybatis.org/spring/ko/sqlsession.html#SqlSessionTemplate ]
mybatisService.getSqlSessionTemplate("datasource id")
src/main/java/net/ion/ice/test/SampleLogicMybatisService.java
@Autowired private MybatisService mybatisService; private SqlSessionTemplate sqlSessionTemplate; public SqlSessionTemplate getSessionTemplate() { if (sqlSessionTemplate == null) { sqlSessionTemplate = mybatisService.getSqlSessionTemplate("oracleDb"); } return sqlSessionTemplate; } public List<Map<String, Object>> list(QueryContext context) { List<Map<String, Object>> list = getSessionTemplate().selectList("net.ion.ice.services.mybatis.mapper.SampleLogicMybatis.list"); return list; }
LogicRepository
"repositoryType": "logic"
"serviceName": "sampleLogicMybatisService"
[ { "tid": "sampleLogicMybatis", "parentId": "test", "typeId": "nodeType", "repositoryType": "logic", "microservice": "test", "typeName": "Sample Logic Mybatis", "serviceName": "sampleLogicMybatisService", "propertyTypes":[ ... ] } ]
@Service("sampleLogicMybatisService")
implements LogicRepository
@Service("sampleLogicMybatisService") public class SampleLogicMybatisService implements LogicRepository { @Autowired private MybatisService mybatisService; private SqlSessionTemplate sqlSessionTemplate; public SqlSessionTemplate getSessionTemplate() { if (sqlSessionTemplate == null) { sqlSessionTemplate = mybatisService.getSqlSessionTemplate("oracleDb"); } return sqlSessionTemplate; } @Override public List<Map<String, Object>> list(QueryContext context) { List<Map<String, Object>> list = getSessionTemplate().selectList("net.ion.ice.services.mybatis.mapper.SampleLogicMybatis.list"); return list; } @Override public Map<String, Object> read(String id) { Map<String, Object> map = new LinkedHashMap<>(); map.put("id", id); Map<String, Object> result = getSessionTemplate().selectOne("net.ion.ice.services.mybatis.mapper.SampleLogicMybatis.list", map); return result; } @Override public int create(Node node) { return 0; } @Override public int update(Node node) { return 0; } @Override public int delete(String id) { return 0; } }