[JAVA] A story I was addicted to when getting a key that was automatically tried on MyBatis

When I tried to set the ID (primary key) that was automatically tried by MySQL AUTO_INCREMENT to the object, 1 was set.

Domain class

Hoge.java



@Data 
@NoArgsConstructor
public class Hoge{
    private Integer id;
    private String message;
}

Service class

HogeService.java


public class HogeService{

    @Autowired
    private HogeMapper hogeMapper;    

    public addHoge(){
        
        Hoge hoge = new Hoge();
        hoge.setMessage("Hello");

        hoge.setId(hogeMapper.insert());

        System.out.println(hoge.getId());//1
        //The ID will be 1.
    }
} 

Mapper

HogeMapper.java



@Mapper
public interface HogeMapper{
    Integer insert(Hoge hoge);
}

HogeMapper.xml


    <insert id="insert">
        INSERT INTO hoge(id, message)
        VALUES (#{id}, #{message})
        <selectKey resultType="Integer" keyProperty="id" order="AFTER">
            select @@IDENTITY
        </selectKey>
    </insert>

With the above method, only 1 is set in hoge.

Since the selectKey of MyBatis is directly assigned to the object, you can get the ID by using the code below.

HogeService.java


public class HogeService{

    @Autowired
    private HogeMapper hogeMapper;    

    public addHoge(){
        
        Hoge hoge = new Hoge();
        hoge.setMessage("Hello");

        hogeMapper.insert();

        System.out.println(hoge.getId());
        //The automatically numbered ID is output! !!
    }
} 

Summary

Mybatis SELECT KEY is assigned directly to the object.

Recommended Posts

A story I was addicted to when getting a key that was automatically tried on MyBatis
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
A memo that I was addicted to when making batch processing with Spring Boot
I was addicted to looping the Update statement on MyBatis
A story I was addicted to in Rails validation settings
The story I was addicted to when setting up STS
[Circle CI] A story I was addicted to at Start Building
I was addicted to using RXTX on Sierra
I was addicted to installing Ruby/Tk on MacOS
A story when I tried to make a video by linking Processing and Resolume
What I was addicted to when developing a Spring Boot application with VS Code
A site that was easy to understand when I was a beginner when I started learning Spring Boot
A story that I had a hard time trying to build PHP 7.4 on GCE's CentOS 8
I tried adding a separator line to TabLayout on Android
What I was addicted to when introducing the JNI library
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
What I was addicted to while using rspec on rails
When I tried to scroll automatically with JScrollBar, the event handler was drawn only once.
How to batch initialize arrays in Java that I didn't know when I was a beginner
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)
[It takes 3 minutes] When I tried to install VS Code on Ubuntu 18.04, it was unexpectedly easy.
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
A story I was addicted to with implicit type conversion of ActiveRecord during unit testing
Java: A story that made me feel uncomfortable when I was taught to compare strings with equals for no reason.
I was addicted to starting sbt
A story that I struggled to challenge a competition professional with Java
I want to use swipeback on a screen that uses XLPagerTabStrip
I tried to create a Spring MVC development environment on Mac
What I was addicted to when implementing google authentication with rails
About the matter that I was addicted to how to use hashmap
[Rails] How to solve ActiveSupport :: MessageVerifier :: InvalidSignature that I was addicted to when introducing twitter login [ActiveStorage]
A memo that was soberly addicted to the request of multipart / form-data
Memorandum: What I was addicted to when I hit the accounting freee API
[Rails] I was addicted to the nginx settings when using Action Cable.
I was addicted to a simple test of Jedis (Java-> Redis library)
Problems I was addicted to when building the digdag environment with docker
I was a little addicted to running old Ruby environment and old Rails
I was a little addicted to ssh connection from mac to linux (ubuntu)
A story that was embarrassing to give anison file to the production environment
A story addicted to JDBC Template placeholders
I was addicted to rewriting to @SpringApplicationConfiguration-> @SpringBootTest
I was addicted to the roll method
I was addicted to the Spring-Batch test
I tried to build AdoptOpenjdk 11 on CentOS 7
When introducing JOOQ to Spring boot, a story that was dealt with because an error occurred around Liquibase
Four technical books that I regret that I should have read when I was a newcomer
I tried to easily put CentOS-7 in a PC that I no longer need
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
What I tried when I wanted to get all the fields of a bean
I was a little addicted to the S3 Checksum comparison, so I made a note.
I tried to summarize again the devise that was difficult at first sight
I tried to automatically generate a class to convert from a data class to a Bundle with APT
SpringSecurity I was addicted to trying to log in with a hashed password (solved)
[Small story] I tried to make the java ArrayList a little more convenient
When I tried to use a Wacom tablet with ubuntu 20.04, I didn't recognize it.
A story addicted to EntityNotFoundException of getOne of JpaRepository
A story that took time to establish a connection
java I tried to break a simple block
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.