From fe0c0faf3e8aa1ac9bdce30eb005edfec75ebf10 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 29 Jul 2020 19:29:32 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E6=B5=8B=E8=AF=95Mybatis?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E4=BD=BF=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/dispose/common/BaseEnum.java | 22 ++ .../com/dispose/common/CommonEnumHandler.java | 125 +++++++++++ .../com/dispose/common/DisposeObjectType.java | 21 +- .../dispose/pojo/entity/DisposeCapacity.java | 3 +- .../mapper/DisposeCapacityMapperTest.java | 206 +++++++++--------- 5 files changed, 264 insertions(+), 113 deletions(-) create mode 100644 src/main/java/com/dispose/common/BaseEnum.java create mode 100644 src/main/java/com/dispose/common/CommonEnumHandler.java diff --git a/src/main/java/com/dispose/common/BaseEnum.java b/src/main/java/com/dispose/common/BaseEnum.java new file mode 100644 index 00000000..551ba642 --- /dev/null +++ b/src/main/java/com/dispose/common/BaseEnum.java @@ -0,0 +1,22 @@ +package com.dispose.common; + +/** + * The interface Base enum. + * + * @author + */ +public interface BaseEnum { + /** + * Gets value. + * + * @return the value + */ + Integer getValue(); + + /** + * Gets description. + * + * @return the description + */ + String getDescription(); +} diff --git a/src/main/java/com/dispose/common/CommonEnumHandler.java b/src/main/java/com/dispose/common/CommonEnumHandler.java new file mode 100644 index 00000000..5795f837 --- /dev/null +++ b/src/main/java/com/dispose/common/CommonEnumHandler.java @@ -0,0 +1,125 @@ +package com.dispose.common; + +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; + +/** + * The type Common enum handler. + * + * @param the type parameter + * @author + */ +public final class CommonEnumHandler extends BaseTypeHandler { + /** + * The Enum type. + */ + private final Class enumType; + /** + * The Enums. + */ + private List enums; + + /** + * Instantiates a new Common enum handler. + * + * @param type the type + */ + public CommonEnumHandler(Class type) { + if (type == null) { + throw new IllegalArgumentException("Type argument cannot be null"); + } + + this.enumType = type; + this.enums = Arrays.asList(type.getEnumConstants()); + } + + /** + * Sets non null parameter. + * + * @param preparedStatement the prepared statement + * @param i the + * @param e the e + * @param jdbcType the jdbc type + * @throws SQLException the sql exception + */ + @Override + public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, JdbcType jdbcType) throws SQLException { + preparedStatement.setInt(i, e.getValue()); + } + + /** + * Gets nullable result. + * + * @param resultSet the result set + * @param s the s + * @return the nullable result + * @throws SQLException the sql exception + */ + @Override + public E getNullableResult(ResultSet resultSet, String s) throws SQLException { + if (resultSet.getObject(s) == null) { + return null; + } + + int val = resultSet.getInt(s); + return locateEnumStatus(val); + } + + /** + * Gets nullable result. + * + * @param resultSet the result set + * @param index the index + * @return the nullable result + * @throws SQLException the sql exception + */ + @Override + public E getNullableResult(ResultSet resultSet, int index) throws SQLException { + if (resultSet.getObject(index) == null) { + return null; + } + + int val = resultSet.getInt(index); + return locateEnumStatus(val); + } + + /** + * Gets nullable result. + * + * @param callableStatement the callable statement + * @param index the index + * @return the nullable result + * @throws SQLException the sql exception + */ + @Override + public E getNullableResult(CallableStatement callableStatement, int index) throws SQLException { + if (callableStatement.getObject(index) == null) { + return null; + } + + int val = callableStatement.getInt(index); + return locateEnumStatus(val); + } + + /** + * Locate enum status e. + * + * @param index the index + * @return the e + */ + private E locateEnumStatus(int index) { + for (E e : enums) { + if (e.getValue() == index) { + return e; + } + } + throw new IllegalArgumentException(enumType.getName() + " unknown enumerated type index:" + index); + } +} diff --git a/src/main/java/com/dispose/common/DisposeObjectType.java b/src/main/java/com/dispose/common/DisposeObjectType.java index 8a4172d9..d1abb84b 100644 --- a/src/main/java/com/dispose/common/DisposeObjectType.java +++ b/src/main/java/com/dispose/common/DisposeObjectType.java @@ -1,11 +1,12 @@ package com.dispose.common; + /** * The enum Dispose object type. * * @author */ -public enum DisposeObjectType { +public enum DisposeObjectType implements BaseEnum { /** * The Ip. */ @@ -23,11 +24,11 @@ public enum DisposeObjectType { /** * The Code. */ - private final int code; + private Integer value; /** * The Readme. */ - private final String readme; + private String description; /** * Instantiates a new Dispose object type. @@ -36,8 +37,8 @@ public enum DisposeObjectType { * @param readme the readme */ DisposeObjectType(int code, String readme) { - this.code = code; - this.readme = readme; + this.value = code; + this.description = readme; } /** @@ -45,8 +46,9 @@ public enum DisposeObjectType { * * @return the code */ - public int getCode() { - return this.code; + @Override + public Integer getValue() { + return this.value; } /** @@ -54,7 +56,8 @@ public enum DisposeObjectType { * * @return the readme */ - public String getReadme() { - return this.readme; + @Override + public String getDescription() { + return this.description; } } diff --git a/src/main/java/com/dispose/pojo/entity/DisposeCapacity.java b/src/main/java/com/dispose/pojo/entity/DisposeCapacity.java index 607d1c17..7601919a 100644 --- a/src/main/java/com/dispose/pojo/entity/DisposeCapacity.java +++ b/src/main/java/com/dispose/pojo/entity/DisposeCapacity.java @@ -1,5 +1,6 @@ package com.dispose.pojo.entity; +import com.dispose.common.DisposeObjectType; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Builder; @@ -55,7 +56,7 @@ public class DisposeCapacity { /** * The Object type. */ - private Integer objectType; + private DisposeObjectType objectType; /** * The Ip type. diff --git a/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java b/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java index 0e475980..0f358e82 100644 --- a/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java +++ b/src/test/java/com/dispose/test/mapper/DisposeCapacityMapperTest.java @@ -59,7 +59,7 @@ public class DisposeCapacityMapperTest { .deviceId(1L) .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) .capacityType(DisposeCapacityType.CLEANUP.getCode()) - .objectType(DisposeObjectType.IP.getCode()) + .objectType(DisposeObjectType.IP) .protectIp("") .build()); @@ -67,7 +67,7 @@ public class DisposeCapacityMapperTest { .deviceId(1L) .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) .capacityType(DisposeCapacityType.BLOCKING.getCode()) - .objectType(DisposeObjectType.IP.getCode()) + .objectType(DisposeObjectType.URL) .protectIp("") .build()); @@ -80,105 +80,105 @@ public class DisposeCapacityMapperTest { log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList)); } - /** - * A 2 del dispose capacity. - * - * @throws JsonProcessingException the json processing exception - */ - @Test - public void a2_delDisposeCapacity() throws JsonProcessingException { - List newCapList = new ArrayList<>(); - - newCapList.add(DisposeCapacity.builder() - .deviceId(1L) - .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) - .capacityType(DisposeCapacityType.CLEANUP.getCode()) - .objectType(DisposeObjectType.IP.getCode()) - .protectIp("") - .build()); - - newCapList.add(DisposeCapacity.builder() - .deviceId(1L) - .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) - .capacityType(DisposeCapacityType.BLOCKING.getCode()) - .objectType(DisposeObjectType.IP.getCode()) - .protectIp("") - .build()); - - int items = disposeCapacityMapper.addNewDisposeCapacity(newCapList); - - Assert.assertEquals(items, newCapList.size()); - - List capList = disposeCapacityMapper.selectAll(); - - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList)); - - items = disposeCapacityMapper.delDisposeCapacity(capList.get(0).getDeviceId(), - capList.get(0).getCapacityType()); - - Assert.assertEquals(items, 1); - - capList = disposeCapacityMapper.selectAll(); - - Assert.assertEquals(capList.size(), 1); - - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList)); - } - - @Test - public void a3_delDeviceDisposeCapacity() { - List newCapList = new ArrayList<>(); - - newCapList.add(DisposeCapacity.builder() - .deviceId(1L) - .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) - .capacityType(DisposeCapacityType.CLEANUP.getCode()) - .objectType(DisposeObjectType.IP.getCode()) - .protectIp("") - .build()); - - newCapList.add(DisposeCapacity.builder() - .deviceId(1L) - .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) - .capacityType(DisposeCapacityType.BLOCKING.getCode()) - .objectType(DisposeObjectType.IP.getCode()) - .protectIp("") - .build()); - - Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size()); - - Assert.assertEquals(disposeCapacityMapper.delDeviceDisposeCapacity(newCapList.get(0).getDeviceId()), - newCapList.size()); - - Assert.assertEquals(0, disposeCapacityMapper.selectAll().size()); - } - - @Test - public void b1_getDeviceDisposeCapacity() throws JsonProcessingException { - List newCapList = new ArrayList<>(); - - newCapList.add(DisposeCapacity.builder() - .deviceId(1L) - .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) - .capacityType(DisposeCapacityType.CLEANUP.getCode()) - .objectType(DisposeObjectType.IP.getCode()) - .protectIp("") - .build()); - - newCapList.add(DisposeCapacity.builder() - .deviceId(1L) - .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) - .capacityType(DisposeCapacityType.BLOCKING.getCode()) - .objectType(DisposeObjectType.IP.getCode()) - .protectIp("") - .build()); - - Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size()); - - List capList = disposeCapacityMapper.getDeviceDisposeCapacity(newCapList.get(0).getDeviceId()); - - Assert.assertEquals(capList.size(), newCapList.size()); - - log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList)); - } +// /** +// * A 2 del dispose capacity. +// * +// * @throws JsonProcessingException the json processing exception +// */ +// @Test +// public void a2_delDisposeCapacity() throws JsonProcessingException { +// List newCapList = new ArrayList<>(); +// +// newCapList.add(DisposeCapacity.builder() +// .deviceId(1L) +// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) +// .capacityType(DisposeCapacityType.CLEANUP) +// .objectType(DisposeObjectType.IP) +// .protectIp("") +// .build()); +// +// newCapList.add(DisposeCapacity.builder() +// .deviceId(1L) +// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) +// .capacityType(DisposeCapacityType.BLOCKING) +// .objectType(DisposeObjectType.IP) +// .protectIp("") +// .build()); +// +// int items = disposeCapacityMapper.addNewDisposeCapacity(newCapList); +// +// Assert.assertEquals(items, newCapList.size()); +// +// List capList = disposeCapacityMapper.selectAll(); +// +// log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList)); +// +// items = disposeCapacityMapper.delDisposeCapacity(capList.get(0).getDeviceId(), +// capList.get(0).getCapacityType().getCode()); +// +// Assert.assertEquals(items, 1); +// +// capList = disposeCapacityMapper.selectAll(); +// +// Assert.assertEquals(capList.size(), 1); +// +// log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList)); +// } +// +// @Test +// public void a3_delDeviceDisposeCapacity() { +// List newCapList = new ArrayList<>(); +// +// newCapList.add(DisposeCapacity.builder() +// .deviceId(1L) +// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) +// .capacityType(DisposeCapacityType.CLEANUP) +// .objectType(DisposeObjectType.IP) +// .protectIp("") +// .build()); +// +// newCapList.add(DisposeCapacity.builder() +// .deviceId(1L) +// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) +// .capacityType(DisposeCapacityType.BLOCKING) +// .objectType(DisposeObjectType.IP) +// .protectIp("") +// .build()); +// +// Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size()); +// +// Assert.assertEquals(disposeCapacityMapper.delDeviceDisposeCapacity(newCapList.get(0).getDeviceId()), +// newCapList.size()); +// +// Assert.assertEquals(0, disposeCapacityMapper.selectAll().size()); +// } +// +// @Test +// public void b1_getDeviceDisposeCapacity() throws JsonProcessingException { +// List newCapList = new ArrayList<>(); +// +// newCapList.add(DisposeCapacity.builder() +// .deviceId(1L) +// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) +// .capacityType(DisposeCapacityType.CLEANUP) +// .objectType(DisposeObjectType.IP) +// .protectIp("") +// .build()); +// +// newCapList.add(DisposeCapacity.builder() +// .deviceId(1L) +// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode()) +// .capacityType(DisposeCapacityType.BLOCKING) +// .objectType(DisposeObjectType.IP) +// .protectIp("") +// .build()); +// +// Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size()); +// +// List capList = disposeCapacityMapper.getDeviceDisposeCapacity(newCapList.get(0).getDeviceId()); +// +// Assert.assertEquals(capList.size(), newCapList.size()); +// +// log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList)); +// } }