Mapping to a class with a value object in How to MyBatis

Thing you want to do

Recently, I'm studying object-oriented design. When using a domain object, the existence of a convenient class in the database seems to be troublesome, I was hoping to receive the value obtained from the database directly in the form of a domain object: frowning2:

I'm still studying, so I tried using MyBatis as one solution, I would like to know if there is a good way to get the value from the database: bow:

environment

Try SELECT on MyBatis

Get the MeetingRoom class using MyBatis

public class MeetingRoom {
    private int id;
    private String name;

    ...
}
@Mapper
public interface MeetingRoomMapper {

    MeetingRoom findById(int roomId);
}
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.spring.tutorial.MeetingRoomMapper">
    <select id="findById" resultType="com.spring.tutorial.MeetingRoom">
        SELECT
            room_id as id,
            room_name as name
        FROM
            meeting_room
        WHERE
            room_id = #{room_id}
    </select>
</mapper>

If you specify the return value of findById of Mapper class and MeetingRoom for ResultType of Mapper file, it will be returned in the form of MeetingRoom. Also, since the column name and property name must match, it is specified in the AS clause.

Use value objects

Change the ID of MeetingRoom from int type to Id class.

public class MeetingRoom {
    //Change to value object
    private Id id;
    private String name;

    ...
}
public class Id {
    private static final int MIN = 1;
    private Integer value;

    Id(int value) {
        if (value < MIN) throw new IllegalArgumentException("Illegal value. value:" + value);

        this.value = value;
    }
 
     ...
}

Set the Id class and MeetingRoom class using ResultMap.

    <resultMap id="IdMap" type="com.spring.tutorial.Id" >
        <id property="value" column="room_id" />
    </resultMap>

    <resultMap id="MeetingRoomMap" type="com.spring.tutorial.MeetingRoom">
        <result property="name" column="room_name" />
        <association property="id" resultMap="IdMap"/>
    </resultMap>
    
    <select id="findById" resultMap="MeetingRoomMap">
        SELECT * FROM meeting_room WHERE room_id = #{room_id}
    </select>

Change the ResultType of SELECT to ResultMap and specify the ResultMap of MeetingRoom. Use ** association ** to use ResultMap within ResultMap. When there is a result in the property of the same object, be careful because an error will occur if you do not describe the association below: point_up_2_tone1:

The content of element type "resultMap" must match "(constructor ?, id *, result *, association *, collection *, discriminator?)".

Summary

MyBatis is good because you don't have to increase the number of convenient classes in the database: relaxed:

reference

Principles of system design useful in the field mybatis --Mapper XML file

Recommended Posts

Mapping to a class with a value object in How to MyBatis
Points when mapping Value Object in MyBatis
How to delete a new_record object built with Rails
[How to insert a video in haml with Rails]
How to store an object in PostgreSQL as JSON with MyBatis (Mapper XML)
How to pass an object to Mapper in MyBatis without arguments
How to move another class with a button action of another class.
How to set up a proxy with authentication in Feign
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
How to make a jar file with no dependencies in Maven
How to run a job with docker login in AWS batch
How to get boolean value with jQuery in rails simple form
How to rename a model with foreign key constraints in Rails
How to think about class design (division) in a business system (1)
How to insert a video in Rails
How to publish a library in jCenter
[Java] How to search for a value in an array (or list) with the contains method
[Personal memo] How to interact with a random number generator in Java
How to change the value of a variable at a breakpoint in intelliJ
Android development, how to check null in the value of JSON object
How to start a Docker container with a volume mounted in a batch file
How to get keycloak credentials in interceptor class
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to display a web page in Java
How to create a class that inherits class information
How to get Class from Element in Java
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to conditionally add html.erb class in Rails
How to insert all at once with MyBatis
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to make a follow function in Rails
How to automatically generate a constructor in Eclipse
How to get the ID of a user authenticated with Firebase in Swift
How to check if an instance variable is defined in a Ruby class
How to output jar with main class specified by gradle in Intellij IDEA
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to test a class that handles application.properties with SpringBoot (request: pointed out)
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only
How to clear all data in a particular table
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
How to implement a like feature in Ajax in Rails
How to send value in HTML without screen transition
How to switch Tomcat context.xml with WTP in Eclipse
How to use Z3 library in Scala with Eclipse
How to create a Spring Boot project in IntelliJ
How to manually generate a JWT with Rails Knock
How to create a data URI (base64) in Java
[Ruby / Rails] Set a unique (unique) value in the class
How to display a browser preview in VS Code
How to delete untagged images in bulk with Docker
How to write a date comparison search in Rails
How to store Rakuten API data in a table
How to mock a super method call in PowerMock
How to use JDD library in Scala with Eclipse
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java