Skip to content

Commit 450439e

Browse files
authored
BAEL-7395 - Return Auto Generated Id from Insert with MyBatis (#16745)
* BAEL-7395 - Return Auto Generated Id from Insert with MyBatis * Minor fix * Add MapperScan on Application class * Update Application.java
1 parent 7ba1720 commit 450439e

File tree

12 files changed

+206
-3
lines changed

12 files changed

+206
-3
lines changed

mybatis/src/main/java/com/baeldung/mybatis_sqlscript/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.mybatis_sqlscript;
1+
package com.baeldung.mybatis_sqlscript;
22

33
import org.apache.ibatis.jdbc.ScriptRunner;
44

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.mybatis.generatedid;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@MapperScan("com.baeldung.mybatis.generatedid")
8+
@SpringBootApplication
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.mybatis.generatedid;
2+
3+
public class Car {
4+
5+
private Long id;
6+
7+
private String model;
8+
9+
// getters and setters
10+
11+
public Long getId() {
12+
return id;
13+
}
14+
15+
public void setId(final Long id) {
16+
this.id = id;
17+
}
18+
19+
public String getModel() {
20+
return model;
21+
}
22+
23+
public void setModel(final String model) {
24+
this.model = model;
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.mybatis.generatedid;
2+
3+
import org.apache.ibatis.annotations.Insert;
4+
import org.apache.ibatis.annotations.Mapper;
5+
import org.apache.ibatis.annotations.Options;
6+
import org.apache.ibatis.annotations.SelectKey;
7+
8+
@Mapper
9+
public interface CarMapper {
10+
11+
@Insert("INSERT INTO CAR(MODEL) values (#{model})")
12+
@Options(useGeneratedKeys = true, keyColumn = "ID", keyProperty = "id")
13+
void saveUsingOptions(Car car);
14+
15+
@Insert("INSERT INTO CAR(MODEL) values (#{model})")
16+
@SelectKey(statement = "CALL IDENTITY()", before = false, keyColumn = "ID", keyProperty = "id", resultType = Long.class)
17+
void saveUsingSelectKey(Car car);
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.mybatis.generatedid;
2+
3+
import org.apache.ibatis.annotations.Mapper;
4+
5+
@Mapper
6+
public interface CarXmlMapper {
7+
8+
void saveUsingOptions(Car car);
9+
10+
void saveUsingSelectKey(Car car);
11+
12+
}

persistence-modules/spring-mybatis/src/main/java/com/baeldung/mybatis/spring/PersistenceAutoConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@SpringBootConfiguration
99
@EnableAutoConfiguration
10-
@ComponentScan(basePackages = { "com.baeldung.mybatis" }, excludeFilters = {
10+
@ComponentScan(basePackages = { "com.baeldung.mybatis.spring" }, excludeFilters = {
1111
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { PersistenceConfig.class })
1212
})
1313
public class PersistenceAutoConfig {

persistence-modules/spring-mybatis/src/main/java/com/baeldung/mybatis/spring/PersistenceConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import javax.sql.DataSource;
1515

1616
@Configuration
17-
@MapperScan("com.baeldung.mybatis")
17+
@MapperScan("com.baeldung.mybatis.spring")
1818
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class })
1919
public class PersistenceConfig {
2020

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE IF NOT EXISTS CAR
2+
(
3+
ID INTEGER PRIMARY KEY AUTO_INCREMENT,
4+
MODEL VARCHAR(100) NOT NULL
5+
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
4+
5+
<mapper namespace="com.baeldung.mybatis.generatedid.CarXmlMapper">
6+
7+
<insert id="saveUsingOptions" parameterType="com.baeldung.mybatis.generatedid.Car"
8+
useGeneratedKeys="true" keyColumn="ID" keyProperty="id">
9+
INSERT INTO CAR(MODEL)
10+
VALUES (#{model});
11+
</insert>
12+
13+
<insert id="saveUsingSelectKey" parameterType="com.baeldung.mybatis.generatedid.Car">
14+
INSERT INTO CAR(MODEL)
15+
VALUES (#{model});
16+
17+
<selectKey resultType="Long" order="AFTER" keyColumn="ID" keyProperty="id">
18+
CALL IDENTITY()
19+
</selectKey>
20+
</insert>
21+
22+
</mapper>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.mybatis.generatedid;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.ContextConfiguration;
7+
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
10+
@SpringBootTest(classes = Application.class)
11+
@ContextConfiguration(classes = TestConfig.class)
12+
class CarMapperUnitTest {
13+
14+
@Autowired
15+
private CarMapper carMapper;
16+
17+
@Test
18+
void givenCar_whenSaveUsingOptions_thenReturnId() {
19+
Car car = new Car();
20+
car.setModel("BMW");
21+
22+
carMapper.saveUsingOptions(car);
23+
24+
assertNotNull(car.getId());
25+
}
26+
27+
@Test
28+
void givenCar_whenSaveUsingSelectKey_thenReturnId() {
29+
Car car = new Car();
30+
car.setModel("BMW");
31+
32+
carMapper.saveUsingSelectKey(car);
33+
34+
assertNotNull(car.getId());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)