From 7d24d19fbdbf4937a5abc46bc617eec097e7b727 Mon Sep 17 00:00:00 2001 From: "robor.luo" Date: Mon, 25 Mar 2024 21:30:29 +0800 Subject: [PATCH] =?UTF-8?q?test=EF=BC=9A=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E6=B5=8B=E2=80=94=E2=80=94issues=20#302?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/test/AppConfig.java | 2 + .../com/mybatisflex/test/model/Account.java | 10 +++ .../com/mybatisflex/test/model/BaseEnum.java | 14 ++++ .../test/model/EnumTypeHandler.java | 57 +++++++++++++++ .../java/com/mybatisflex/test/model/Sex.java | 25 +++++++ .../src/main/resources/data.sql | 2 +- .../src/main/resources/schema.sql | 3 +- .../java/com/mybatisflex/test/EnumTest.java | 73 +++++++++++++++++++ .../test/issue113/def/TClassTableDef.java | 2 + 9 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/BaseEnum.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/EnumTypeHandler.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Sex.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/EnumTest.java diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java index 50243867..5259b9e9 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/AppConfig.java @@ -17,6 +17,7 @@ package com.mybatisflex.test; import com.mybatisflex.core.mybatis.FlexConfiguration; import com.mybatisflex.spring.FlexSqlSessionFactoryBean; +import com.mybatisflex.test.model.EnumTypeHandler; import org.apache.ibatis.logging.stdout.StdOutImpl; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; @@ -52,6 +53,7 @@ public class AppConfig implements ApplicationListener { factoryBean.setDataSource(dataSource); FlexConfiguration configuration = new FlexConfiguration(); configuration.setLogImpl(StdOutImpl.class); + configuration.setDefaultEnumTypeHandler(EnumTypeHandler.class); factoryBean.setConfiguration(configuration); return factoryBean.getObject(); } diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Account.java b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Account.java index 8370fd04..05d4e063 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Account.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Account.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.test.model; +import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Table; @@ -28,6 +29,7 @@ public class Account { private String userName; private Integer age; private Date birthday; + private Sex sex; public Long getId() { return id; @@ -61,6 +63,13 @@ public class Account { this.birthday = birthday; } + public Sex getSex() { + return sex; + } + + public void setSex(Sex sex) { + this.sex = sex; + } @Override public String toString() { return "Account{" + @@ -68,6 +77,7 @@ public class Account { ", userName='" + userName + '\'' + ", age=" + age + ", birthday=" + birthday + + ", sex=" + sex + '}'; } diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/BaseEnum.java b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/BaseEnum.java new file mode 100644 index 00000000..b8107cff --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/BaseEnum.java @@ -0,0 +1,14 @@ +package com.mybatisflex.test.model; + +/** + * 基础枚举接口 + * @author luozhan + */ +public interface BaseEnum { + + /** + * 获取枚举值 + * @return 枚举值 + */ + int getValue(); +} diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/EnumTypeHandler.java b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/EnumTypeHandler.java new file mode 100644 index 00000000..c0a84be1 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/EnumTypeHandler.java @@ -0,0 +1,57 @@ +package com.mybatisflex.test.model; + +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.stream.Stream; + +/** + * 枚举类型处理器 + * + * @author luozhan + */ +public class EnumTypeHandler & BaseEnum> extends BaseTypeHandler { + private final Class enumClass; + + public EnumTypeHandler(Class enumClass) { + this.enumClass = enumClass; + } + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException { + Integer value = parameter.getValue(); + ps.setObject(i, value); + } + + @Override + public E getNullableResult(ResultSet rs, String columnName) throws SQLException { + int value = rs.getInt(columnName); + return getEnumByValue(value); + } + + + @Override + public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + int value = rs.getInt(columnIndex); + return getEnumByValue(value); + } + + @Override + public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + int value = cs.getInt(columnIndex); + return getEnumByValue(value); + } + + /** + * 根据枚举值获取枚举对象 + * @param value 枚举值 + * @return 枚举对象 + */ + private E getEnumByValue(int value) { + return Stream.of(enumClass.getEnumConstants()).filter(e -> e.getValue() == value).findAny().orElse(null); + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Sex.java b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Sex.java new file mode 100644 index 00000000..b6db0ff2 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/java/com/mybatisflex/test/model/Sex.java @@ -0,0 +1,25 @@ +package com.mybatisflex.test.model; + + +/** + * 性别枚举 + * @author luozhan + */ + +public enum Sex implements BaseEnum { + // + MALE(1), + FEMALE(2), + UNKNOWN(0); + + private final int value; + + Sex(Integer value) { + this.value = value; + } + + @Override + public int getValue() { + return value; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/data.sql b/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/data.sql index fb75a0ae..923dce23 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/data.sql +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/data.sql @@ -1,5 +1,5 @@ INSERT INTO tb_account -VALUES (1, 'Michael Yang', 18, '2020-01-11'); +VALUES (1, 'Michael Yang', 18, '2020-01-11', 1); INSERT INTO tb_class VALUES (1, 1, 'class111'); diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/schema.sql b/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/schema.sql index a77acf68..4fe6b3f5 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/schema.sql +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/main/resources/schema.sql @@ -3,7 +3,8 @@ CREATE TABLE IF NOT EXISTS `tb_account` `id` INTEGER PRIMARY KEY, `user_name` VARCHAR(100) NOT NULL, `age` Integer, - `birthday` DATETIME + `birthday` DATETIME, + `sex` tinyint ); CREATE TABLE IF NOT EXISTS `tb_class` diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/EnumTest.java b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/EnumTest.java new file mode 100644 index 00000000..39638a96 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/EnumTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test; + +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.row.Db; +import com.mybatisflex.core.row.Row; +import com.mybatisflex.test.mapper.AccountMapper; +import com.mybatisflex.test.model.Account; +import com.mybatisflex.test.model.Sex; +import com.mybatisflex.test.model.TbClass; +import org.assertj.core.api.WithAssertions; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.List; + +import static com.mybatisflex.test.tabledef.Tables.ACCOUNT; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = AppConfig.class) +public class EnumTest implements WithAssertions { + + @Autowired + AccountMapper accountMapper; + + @Test + public void test_global_enum_type_handler() { + QueryWrapper query = new QueryWrapper() + .select() + .from(ACCOUNT).where(ACCOUNT.SEX.in(Sex.MALE, Sex.FEMALE)); + // 注意,此处虽然显示是MALE、FEMALE,但是如果使用了枚举的typeHandler,实际sql执行时会使用typeHandler进行处理 + assertThat(query.toSQL()).isEqualTo("SELECT * FROM `tb_account` WHERE `sex` IN ('MALE', 'FEMALE')"); + List list = accountMapper.selectListByQuery(query); + assertThat(list).isNotNull(); + for (Account account : list) { + assertThat(account.getSex()).isIn(Sex.MALE, Sex.FEMALE); + } + } + @Test + public void test_create_entity_with_enum_type() { + Account account = new Account(); + account.setSex(Sex.MALE); + QueryWrapper queryWrapper = QueryWrapper.create(account); + String sql = queryWrapper.toSQL(); + System.out.println(sql); + // 注意,此处虽然显示是MALE,但是如果使用了枚举的typeHandler,实际sql执行时会使用typeHandler进行处理 + assertThat(sql).isEqualTo("SELECT `id`, `user_name`, `age`, `birthday`, `sex` FROM `tb_account` WHERE `sex` = 'MALE'"); + List list = accountMapper.selectListByQuery(queryWrapper); + assertThat(list).isNotNull(); + for (Account item : list) { + assertThat(item.getSex()).isEqualTo(Sex.MALE); + } + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java index e234f606..579179ee 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/issue113/def/TClassTableDef.java @@ -28,6 +28,8 @@ public class TClassTableDef extends QueryTable { public final QueryColumn CLASS_NAME = new QueryColumn(this, "class_name"); + public final QueryColumn SEX = new QueryColumn(this, "sex"); + public TClassTableDef() { super("", "tb_class"); }