Skip to content

Commit b0c6f30

Browse files
committed
unit test for mybatis issue mybatis/mybatis-3#2956
0 parents  commit b0c6f30

File tree

10 files changed

+329
-0
lines changed

10 files changed

+329
-0
lines changed

.gitignore

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

pom.xml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>mybaits-issue-2956</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-jdbc</artifactId>
21+
<version>2.7.15</version>
22+
</dependency>
23+
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-test</artifactId>
28+
<version>2.7.15</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-aop</artifactId>
35+
<version>2.7.15</version>
36+
</dependency>
37+
38+
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
39+
<!--<dependency>
40+
<groupId>org.mybatis</groupId>
41+
<artifactId>mybatis-spring</artifactId>
42+
<version>3.0.2</version>
43+
</dependency>-->
44+
45+
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
46+
<dependency>
47+
<groupId>org.mybatis.spring.boot</groupId>
48+
<artifactId>mybatis-spring-boot-starter</artifactId>
49+
<version>2.3.0</version>
50+
</dependency>
51+
52+
53+
<dependency>
54+
<groupId>mysql</groupId>
55+
<artifactId>mysql-connector-java</artifactId>
56+
<version>8.0.33</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.projectlombok</groupId>
61+
<artifactId>lombok</artifactId>
62+
<version>1.18.26</version>
63+
<scope>provided</scope>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>junit</groupId>
68+
<artifactId>junit</artifactId>
69+
<version>4.12</version>
70+
<scope>test</scope>
71+
</dependency>
72+
</dependencies>
73+
</project>
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.example;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
7+
import org.springframework.transaction.annotation.EnableTransactionManagement;
8+
9+
@EnableTransactionManagement
10+
@EnableAspectJAutoProxy
11+
@MapperScan(basePackages = "org.example.mapper")
12+
@SpringBootApplication
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(Application.class);
17+
}
18+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example.entity;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.example.enums.GcType;
6+
7+
@Getter
8+
@Setter
9+
public class User {
10+
private Long id;
11+
private String uu;
12+
private GcType type;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.example.enums;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@AllArgsConstructor
7+
@Getter
8+
public enum GcType {
9+
G1(1),
10+
ZGC(2) {
11+
@Override
12+
public String getName() {
13+
return "zgc";
14+
}
15+
},
16+
;
17+
18+
public String getName() {
19+
return this.name();
20+
}
21+
22+
private int value;
23+
24+
public static GcType fromValue(int i) {
25+
for (GcType t : values()) {
26+
if (t.value == i) {
27+
return t;
28+
}
29+
}
30+
return null;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.example.mapper;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.apache.ibatis.annotations.Insert;
6+
import org.apache.ibatis.annotations.Mapper;
7+
import org.apache.ibatis.annotations.Options;
8+
import org.apache.ibatis.annotations.Param;
9+
import org.apache.ibatis.annotations.Select;
10+
import org.example.entity.User;
11+
import org.example.enums.GcType;
12+
13+
@Mapper
14+
public interface UserMapper {
15+
@Options(useGeneratedKeys = true, keyProperty = "id")
16+
@Insert("insert into user (uu, `type`) values (#{user.uu}, #{user.type})")
17+
int insert(@Param("user") User user);
18+
19+
@Select("select * from user where `type`=#{req.gcType}")
20+
User selectByRequestParam(@Param("req") RequestParam req);
21+
22+
23+
@Getter
24+
@Setter
25+
public static class RequestParam {
26+
private GcType gcType;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.example.typehandler;
2+
3+
import org.apache.ibatis.type.BaseTypeHandler;
4+
import org.apache.ibatis.type.JdbcType;
5+
import org.apache.ibatis.type.MappedTypes;
6+
import org.example.enums.GcType;
7+
8+
import java.sql.CallableStatement;
9+
import java.sql.PreparedStatement;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
13+
@MappedTypes(GcType.class)
14+
public class CustomEnumTypeHandler extends BaseTypeHandler<org.example.enums.GcType> {
15+
private Class<GcType> type;
16+
17+
public CustomEnumTypeHandler() {}
18+
19+
public CustomEnumTypeHandler(Class<GcType> type) {
20+
if (type == null) {
21+
throw new IllegalArgumentException("Type argument cannot be null");
22+
}
23+
this.type = type;
24+
}
25+
26+
@Override
27+
public void setNonNullParameter(PreparedStatement ps, int i, GcType parameter, JdbcType jdbcType) throws SQLException {
28+
ps.setObject(i, parameter.getValue());
29+
}
30+
31+
@Override
32+
public GcType getNullableResult(ResultSet rs, String columnName) throws SQLException {
33+
return GcType.fromValue(rs.getInt(columnName));
34+
}
35+
36+
@Override
37+
public GcType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
38+
return GcType.fromValue(rs.getInt(columnIndex));
39+
}
40+
41+
@Override
42+
public GcType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
43+
return GcType.fromValue(cs.getInt(columnIndex));
44+
}
45+
}

src/main/resources/application.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spring:
2+
datasource:
3+
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
4+
driver-class-name: com.mysql.cj.jdbc.Driver
5+
hikari:
6+
jdbc-url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
7+
password:
8+
username:
9+
driver-class-name: com.mysql.cj.jdbc.Driver
10+
11+
mybatis:
12+
type-handlers-package: org.example.typehandler
13+
# configuration:
14+
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

src/main/resources/schema.sql

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
CREATE TABLE `user`
3+
(
4+
`id` bigint NOT NULL AUTO_INCREMENT,
5+
`uu` varchar(255) NOT NULL,
6+
`type` int DEFAULT NULL,
7+
PRIMARY KEY (`id`)
8+
) ENGINE = InnoDB
9+
DEFAULT CHARSET = utf8mb4;
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.test;
2+
3+
import org.apache.ibatis.session.SqlSessionFactory;
4+
import org.apache.ibatis.type.EnumTypeHandler;
5+
import org.apache.ibatis.type.TypeHandler;
6+
import org.apache.ibatis.type.TypeHandlerRegistry;
7+
import org.example.Application;
8+
import org.example.entity.User;
9+
import org.example.enums.GcType;
10+
import org.example.mapper.UserMapper;
11+
import org.example.mapper.UserMapper.RequestParam;
12+
import org.example.typehandler.CustomEnumTypeHandler;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.test.context.SpringBootTest;
17+
import org.springframework.test.context.junit4.SpringRunner;
18+
import org.springframework.transaction.annotation.Transactional;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
@RunWith(SpringRunner.class)
23+
@SpringBootTest(classes = Application.class)
24+
@Transactional
25+
public class MybatisTest {
26+
@Autowired
27+
private SqlSessionFactory sqlSessionFactory;
28+
@Autowired
29+
private UserMapper userMapper;
30+
31+
@Test
32+
public void testEnumTypeHandler() {
33+
User user = new User();
34+
user.setUu("user1");
35+
user.setType(GcType.G1);
36+
37+
// insert into user (uu, `type`) values ('user1', '1');
38+
userMapper.insert(user);
39+
40+
TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
41+
42+
TypeHandler<GcType> typeHandler = typeHandlerRegistry.getTypeHandler(GcType.class);
43+
44+
// here
45+
assertEquals(typeHandler.getClass(), CustomEnumTypeHandler.class);
46+
47+
RequestParam req = new RequestParam();
48+
req.setGcType(GcType.ZGC);
49+
50+
// actual: select * from user where `type` = 'ZGC';
51+
// expected: select * from user where `type` = 2;
52+
userMapper.selectByRequestParam(req);
53+
54+
TypeHandler<GcType> typeHandler2 = typeHandlerRegistry.getTypeHandler(GcType.class);
55+
56+
// here
57+
assertEquals(typeHandler2.getClass(), EnumTypeHandler.class);
58+
}
59+
}

0 commit comments

Comments
 (0)