-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit test for mybatis issue mybatis/mybatis-3#2956
- Loading branch information
0 parents
commit b0c6f30
Showing
10 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.example</groupId> | ||
<artifactId>mybaits-issue-2956</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
<version>2.7.15</version> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<version>2.7.15</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
<version>2.7.15</version> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> | ||
<!--<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis-spring</artifactId> | ||
<version>3.0.2</version> | ||
</dependency>--> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.3.0</version> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.33</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.26</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.example; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.EnableAspectJAutoProxy; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
@EnableTransactionManagement | ||
@EnableAspectJAutoProxy | ||
@MapperScan(basePackages = "org.example.mapper") | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.example.entity; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.example.enums.GcType; | ||
|
||
@Getter | ||
@Setter | ||
public class User { | ||
private Long id; | ||
private String uu; | ||
private GcType type; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.example.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum GcType { | ||
G1(1), | ||
ZGC(2) { | ||
@Override | ||
public String getName() { | ||
return "zgc"; | ||
} | ||
}, | ||
; | ||
|
||
public String getName() { | ||
return this.name(); | ||
} | ||
|
||
private int value; | ||
|
||
public static GcType fromValue(int i) { | ||
for (GcType t : values()) { | ||
if (t.value == i) { | ||
return t; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.example.mapper; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Options; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Select; | ||
import org.example.entity.User; | ||
import org.example.enums.GcType; | ||
|
||
@Mapper | ||
public interface UserMapper { | ||
@Options(useGeneratedKeys = true, keyProperty = "id") | ||
@Insert("insert into user (uu, `type`) values (#{user.uu}, #{user.type})") | ||
int insert(@Param("user") User user); | ||
|
||
@Select("select * from user where `type`=#{req.gcType}") | ||
User selectByRequestParam(@Param("req") RequestParam req); | ||
|
||
|
||
@Getter | ||
@Setter | ||
public static class RequestParam { | ||
private GcType gcType; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/example/typehandler/CustomEnumTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.example.typehandler; | ||
|
||
import org.apache.ibatis.type.BaseTypeHandler; | ||
import org.apache.ibatis.type.JdbcType; | ||
import org.apache.ibatis.type.MappedTypes; | ||
import org.example.enums.GcType; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@MappedTypes(GcType.class) | ||
public class CustomEnumTypeHandler extends BaseTypeHandler<org.example.enums.GcType> { | ||
private Class<GcType> type; | ||
|
||
public CustomEnumTypeHandler() {} | ||
|
||
public CustomEnumTypeHandler(Class<GcType> type) { | ||
if (type == null) { | ||
throw new IllegalArgumentException("Type argument cannot be null"); | ||
} | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public void setNonNullParameter(PreparedStatement ps, int i, GcType parameter, JdbcType jdbcType) throws SQLException { | ||
ps.setObject(i, parameter.getValue()); | ||
} | ||
|
||
@Override | ||
public GcType getNullableResult(ResultSet rs, String columnName) throws SQLException { | ||
return GcType.fromValue(rs.getInt(columnName)); | ||
} | ||
|
||
@Override | ||
public GcType getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | ||
return GcType.fromValue(rs.getInt(columnIndex)); | ||
} | ||
|
||
@Override | ||
public GcType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | ||
return GcType.fromValue(cs.getInt(columnIndex)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
spring: | ||
datasource: | ||
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8 | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
hikari: | ||
jdbc-url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8 | ||
password: | ||
username: | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
|
||
mybatis: | ||
type-handlers-package: org.example.typehandler | ||
# configuration: | ||
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
CREATE TABLE `user` | ||
( | ||
`id` bigint NOT NULL AUTO_INCREMENT, | ||
`uu` varchar(255) NOT NULL, | ||
`type` int DEFAULT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE = InnoDB | ||
DEFAULT CHARSET = utf8mb4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.test; | ||
|
||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.type.EnumTypeHandler; | ||
import org.apache.ibatis.type.TypeHandler; | ||
import org.apache.ibatis.type.TypeHandlerRegistry; | ||
import org.example.Application; | ||
import org.example.entity.User; | ||
import org.example.enums.GcType; | ||
import org.example.mapper.UserMapper; | ||
import org.example.mapper.UserMapper.RequestParam; | ||
import org.example.typehandler.CustomEnumTypeHandler; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = Application.class) | ||
@Transactional | ||
public class MybatisTest { | ||
@Autowired | ||
private SqlSessionFactory sqlSessionFactory; | ||
@Autowired | ||
private UserMapper userMapper; | ||
|
||
@Test | ||
public void testEnumTypeHandler() { | ||
User user = new User(); | ||
user.setUu("user1"); | ||
user.setType(GcType.G1); | ||
|
||
// insert into user (uu, `type`) values ('user1', '1'); | ||
userMapper.insert(user); | ||
|
||
TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry(); | ||
|
||
TypeHandler<GcType> typeHandler = typeHandlerRegistry.getTypeHandler(GcType.class); | ||
|
||
// here | ||
assertEquals(typeHandler.getClass(), CustomEnumTypeHandler.class); | ||
|
||
RequestParam req = new RequestParam(); | ||
req.setGcType(GcType.ZGC); | ||
|
||
// actual: select * from user where `type` = 'ZGC'; | ||
// expected: select * from user where `type` = 2; | ||
userMapper.selectByRequestParam(req); | ||
|
||
TypeHandler<GcType> typeHandler2 = typeHandlerRegistry.getTypeHandler(GcType.class); | ||
|
||
// here | ||
assertEquals(typeHandler2.getClass(), EnumTypeHandler.class); | ||
} | ||
} |