-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11f177e
commit 4316e9e
Showing
5 changed files
with
159 additions
and
1 deletion.
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
24 changes: 24 additions & 0 deletions
24
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDEntity.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,24 @@ | ||
package com.baomidou.mybatisplus.test.uuid; | ||
|
||
import com.baomidou.mybatisplus.annotation.*; | ||
import lombok.Data; | ||
|
||
import java.util.UUID; | ||
|
||
@Data | ||
@TableName("entity") | ||
public class UUIDEntity { | ||
|
||
@TableId(value = "id",type = IdType.INPUT) | ||
private UUID id; | ||
|
||
private String name; | ||
|
||
@TableField(fill = FieldFill.UPDATE) | ||
private String deleteBy; | ||
|
||
@TableField(fill = FieldFill.UPDATE) | ||
@TableLogic(delval = "true", value = "false") | ||
private Boolean deleted; | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDEntityMapper.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,7 @@ | ||
package com.baomidou.mybatisplus.test.uuid; | ||
|
||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
|
||
public interface UUIDEntityMapper extends BaseMapper<UUIDEntity> { | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDEntityTest.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,88 @@ | ||
package com.baomidou.mybatisplus.test.uuid; | ||
|
||
import com.baomidou.mybatisplus.core.config.GlobalConfig; | ||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; | ||
import com.baomidou.mybatisplus.test.BaseDbTest; | ||
import org.apache.ibatis.reflection.MetaObject; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.function.Consumer; | ||
|
||
public class UUIDEntityTest extends BaseDbTest<UUIDEntityMapper> { | ||
|
||
@Test | ||
void test() { | ||
doTest(m -> { | ||
var uuidEntity = new UUIDEntity(); | ||
uuidEntity.setId(UUID.randomUUID()); | ||
uuidEntity.setName("test1"); | ||
uuidEntity.setDeleted(false); | ||
Assertions.assertEquals(1, m.insert(uuidEntity)); | ||
Assertions.assertNotNull(m.selectById(uuidEntity.getId())); | ||
Assertions.assertEquals(1, m.deleteById(uuidEntity)); | ||
}); | ||
|
||
doTest(m -> { | ||
var uuidEntity = new UUIDEntity(); | ||
uuidEntity.setId(UUID.randomUUID()); | ||
uuidEntity.setName("test2"); | ||
uuidEntity.setDeleted(false); | ||
Assertions.assertEquals(1, m.insert(uuidEntity)); | ||
Assertions.assertEquals(1, m.deleteByIds(List.of(uuidEntity.getId()))); | ||
}); | ||
|
||
doTest(m -> { | ||
var uuidEntity = new UUIDEntity(); | ||
uuidEntity.setId(UUID.randomUUID()); | ||
uuidEntity.setName("test3"); | ||
uuidEntity.setDeleted(false); | ||
Assertions.assertEquals(1, m.insert(uuidEntity)); | ||
Assertions.assertEquals(1, m.deleteByIds(List.of(uuidEntity))); | ||
}); | ||
} | ||
|
||
|
||
@Override | ||
protected Consumer<Configuration> consumer() { | ||
return configuration -> { | ||
configuration.getTypeHandlerRegistry().register(UUIDTypeHandler.class); | ||
}; | ||
} | ||
|
||
@Override | ||
protected GlobalConfig globalConfig() { | ||
return super.globalConfig().setMetaObjectHandler(new MetaObjectHandler() { | ||
@Override | ||
public void insertFill(MetaObject metaObject) { | ||
|
||
} | ||
|
||
@Override | ||
public void updateFill(MetaObject metaObject) { | ||
metaObject.setValue("deleteBy", "baomidou"); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected String tableDataSql() { | ||
return "insert into entity values('0824eb71-e124-5ba1-56b9-87185d91f309','test',null, false)"; | ||
} | ||
|
||
@Override | ||
protected List<String> tableSql() { | ||
return Arrays.asList("drop table if exists entity", | ||
"CREATE TABLE IF NOT EXISTS entity (\n" + | ||
"id VARCHAR(50) NOT NULL,\n" + | ||
"name VARCHAR(30) NULL DEFAULT NULL,\n" + | ||
"delete_by VARCHAR(30) NULL DEFAULT NULL," + | ||
"deleted BOOLEAN NOT NULL DEFAULT false," + | ||
"PRIMARY KEY (id)" + | ||
")"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDTypeHandler.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,38 @@ | ||
package com.baomidou.mybatisplus.test.uuid; | ||
|
||
import org.apache.ibatis.type.*; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.UUID; | ||
|
||
@MappedTypes(UUID.class) | ||
@MappedJdbcTypes(JdbcType.VARCHAR) | ||
public class UUIDTypeHandler extends BaseTypeHandler<UUID> { | ||
|
||
@Override | ||
public void setNonNullParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) throws SQLException { | ||
ps.setObject(i, parameter); | ||
} | ||
|
||
@Override | ||
public UUID getNullableResult(ResultSet rs, String columnName) throws SQLException { | ||
String uuidStr = rs.getString(columnName); | ||
return uuidStr == null ? null : UUID.fromString(uuidStr); | ||
} | ||
|
||
@Override | ||
public UUID getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | ||
String uuidStr = rs.getString(columnIndex); | ||
return uuidStr == null ? null : UUID.fromString(uuidStr); | ||
} | ||
|
||
@Override | ||
public UUID getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | ||
String uuidStr = cs.getString(columnIndex); | ||
return uuidStr == null ? null : UUID.fromString(uuidStr); | ||
} | ||
|
||
} |