MyBatis persistence framework

1. Introduction

MyBatis is a persistence framework with support for custom SQL, stored procedures and advanced mappings.


1.1. Hibernate vs MyBatis

Hibernate is an object-relational mapping framework (ORM) which maps Java classes to database tables, and MyBatis is a persistence framework – not ORM, which maps SQL statements to Java methods.

2. Reference

MyBatis with Spring [https://www.baeldung.com/spring-mybatis]

mybatis-spring [https://mybatis.org/spring/index.html]

mybatis-issues [https://github.com/harawata/mybatis-issues/]

SSCCE : Short, Self Contained, Correct (Compilable), Example

Mapping Oracle user-defined object and its array [https://github.com/harawata/mybatis-issues/tree/master/so-56834806]



3. Invoke stored procedure w/ a collection of custom type parameter

Fully functional PoC "so-so-56834806" (see reference "Mapping Oracle user-defined object and its array").

It requires a running OracleDB instance. Tested successfully using: Oracle Database XE 21c.

Replace jdbc dependency by:

<dependency>

<groupId>com.oracle.database.jdbc</groupId>

<artifactId>ojdbc8</artifactId>

<version>18.3.0.0</version>

</dependency>

And configure mybatis-config.xml, eg:

<property name="url" value="jdbc:oracle:thin:@192.168.1.27:1521/xe" />

<property name="username" value="myuser" />

<property name="password" value="mypass" />

Details:

CreateDB.sql

creates the stored procedure with parameter collection of custom type:

user_list in S_USER_OBJ_LIST,

where S_USER_OBJ_LIST is:

create or replace type S_USER_OBJ_LIST as table of S_USER_OBJ;



UserListTypeHandler.java

makes the magic for the List<User> stored procedure parameter.



Mapper.xml

maps the stored procedure parameter collection of custom type:

#{users,typeHandler=test.UserListTypeHandler}



Mapper.java

defines method:

saveUsers

with a parameter which it's a collection of custom type User:

List<User>



SimpleTest.java

has test:

demoStoredProcedure

that invokes the stored procedure with a collection of 2 User

This demo can be run with

mvn test