Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

ICE4에서 Mybatis를 사용하기위한 기본 가이드

...

Application yml

활성화된 profile 의 application yml 파일에 Mybatis mapper classpath가 설정되어 있어야 한다.

Info

src/main/resources/application-{active profile name}.yml

application-dxp-loc.yml

...

Info

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

  • oracle

  • mysql

  • maria

  • mssql

username

DB user ID

password

DB user Password

json
Code Block
language
[
  {
    "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!@"
  }
]

...

/src/main/java/net/ion/ice/services/mybatis/mapper/SampleLogicMybatis.java
language
Code Block
java
@Mapper
public interface SampleLogicMybatis {
    public List<Map> list();
}

...

src/main/resources/mybatis/sampleLogicMybatis.xml
language
Code Block
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

...

Info

src/main/java/net/ion/ice/services/

  • datasource에 작성한 DB 접속정보의 id 로 SqlSessionTemplate 생성

mybatisService.getSqlSessionTemplate("oracleDb")

  • SqlSessionTemplate으로 Mapper Interface를 통해 Mapper xml 쿼리 실행getSessionTemplate().selectList("net.ion.ice.services.mybatis.mapper.SampleLogicMybatis.list")

[참조 : https://mybatis.org/spring/ko/sqlsession.html#SqlSessionTemplate ]mybatisService.getSqlSessionTemplate("datasource id")

src/main/java/net/ion/ice/test/SampleLogicMybatisService.java
language
Code Block
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

...

  • "NodeType의 repositoryType": "logic"

  • "serviceName": "sampleLogicMybatisService"

...

  • logic 유형으로 Mybatis로 SQL 실행 및 반환된 결과를 사용하는데 활용할 수 있다.

  • serviceName(LogicRepository를 implement하는 서비스명)을 설정

  • 기본적인 CRUD 및 List API 호출시에 해당 서비스를 호출하여 처리된 결과 반환

src/main/resources/schema/test/sampleLogicMybatisSchema.json
Code Block
[
  {
    "tid": "sampleLogicMybatis",
    "parentId": "test",
    "typeId": "nodeType",
    "repositoryType": "logic",
    "microservice": "test",
    "typeName": "Sample Logic Mybatis",
    "serviceName": "sampleLogicMybatisService",
    "propertyTypes":[
      ...
    ]
  }
]

  • @Service annotation 설정 : nodeType의 serviceName과 동일

@Service("sampleLogicMybatisService")

...

implements LogicRepository

...

src/main/java/net/ion/ice/test/SampleLogicMybatisService.java
Code Block
@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;
    }
}

...