Versions Compared

Key

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

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

...

.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
Code Block
languagejava
@Mapper
public interface SampleLogicMybatis {
    public List<Map> list();
}

...

src/main/resources/mybatis/sampleLogicMybatis.xml
Code Block
languagexml
<?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")

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
Code Block
languagejava
@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;
}

...

  • "repositoryType": "logic"

  • "serviceName": "sampleLogicMybatisService"

Code Block
languagejson
[
  {
    "tid": "sampleLogicMybatis",
    "parentId": "test",
    "typeId": "nodeType",
    "repositoryType": "logic",
    "microservice": "test",
    "typeName": "Sample Logic Mybatis",
    "serviceName": "sampleLogicMybatisService",
    "propertyTypes":[
      ...
    ]
  }
]

...

  • @Service("sampleLogicMybatisService")

  • implements LogicRepository

java
Code Block
language
@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;
    }
}

...