test:增加单测——issues #302

This commit is contained in:
robor.luo 2024-03-25 21:30:29 +08:00
parent eb23046d4a
commit 7d24d19fbd
9 changed files with 186 additions and 2 deletions

View File

@ -17,6 +17,7 @@ package com.mybatisflex.test;
import com.mybatisflex.core.mybatis.FlexConfiguration; import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.spring.FlexSqlSessionFactoryBean; import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
import com.mybatisflex.test.model.EnumTypeHandler;
import org.apache.ibatis.logging.stdout.StdOutImpl; import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionFactoryBean;
@ -52,6 +53,7 @@ public class AppConfig implements ApplicationListener<ContextRefreshedEvent> {
factoryBean.setDataSource(dataSource); factoryBean.setDataSource(dataSource);
FlexConfiguration configuration = new FlexConfiguration(); FlexConfiguration configuration = new FlexConfiguration();
configuration.setLogImpl(StdOutImpl.class); configuration.setLogImpl(StdOutImpl.class);
configuration.setDefaultEnumTypeHandler(EnumTypeHandler.class);
factoryBean.setConfiguration(configuration); factoryBean.setConfiguration(configuration);
return factoryBean.getObject(); return factoryBean.getObject();
} }

View File

@ -15,6 +15,7 @@
*/ */
package com.mybatisflex.test.model; package com.mybatisflex.test.model;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
@ -28,6 +29,7 @@ public class Account {
private String userName; private String userName;
private Integer age; private Integer age;
private Date birthday; private Date birthday;
private Sex sex;
public Long getId() { public Long getId() {
return id; return id;
@ -61,6 +63,13 @@ public class Account {
this.birthday = birthday; this.birthday = birthday;
} }
public Sex getSex() {
return sex;
}
public void setSex(Sex sex) {
this.sex = sex;
}
@Override @Override
public String toString() { public String toString() {
return "Account{" + return "Account{" +
@ -68,6 +77,7 @@ public class Account {
", userName='" + userName + '\'' + ", userName='" + userName + '\'' +
", age=" + age + ", age=" + age +
", birthday=" + birthday + ", birthday=" + birthday +
", sex=" + sex +
'}'; '}';
} }

View File

@ -0,0 +1,14 @@
package com.mybatisflex.test.model;
/**
* 基础枚举接口
* @author luozhan
*/
public interface BaseEnum {
/**
* 获取枚举值
* @return 枚举值
*/
int getValue();
}

View File

@ -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<E extends Enum<E> & BaseEnum> extends BaseTypeHandler<E> {
private final Class<E> enumClass;
public EnumTypeHandler(Class<E> 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);
}
}

View File

@ -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;
}
}

View File

@ -1,5 +1,5 @@
INSERT INTO tb_account 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 INSERT INTO tb_class
VALUES (1, 1, 'class111'); VALUES (1, 1, 'class111');

View File

@ -3,7 +3,8 @@ CREATE TABLE IF NOT EXISTS `tb_account`
`id` INTEGER PRIMARY KEY, `id` INTEGER PRIMARY KEY,
`user_name` VARCHAR(100) NOT NULL, `user_name` VARCHAR(100) NOT NULL,
`age` Integer, `age` Integer,
`birthday` DATETIME `birthday` DATETIME,
`sex` tinyint
); );
CREATE TABLE IF NOT EXISTS `tb_class` CREATE TABLE IF NOT EXISTS `tb_class`

View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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));
// 注意此处虽然显示是MALEFEMALE但是如果使用了枚举的typeHandler实际sql执行时会使用typeHandler进行处理
assertThat(query.toSQL()).isEqualTo("SELECT * FROM `tb_account` WHERE `sex` IN ('MALE', 'FEMALE')");
List<Account> 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<Account> list = accountMapper.selectListByQuery(queryWrapper);
assertThat(list).isNotNull();
for (Account item : list) {
assertThat(item.getSex()).isEqualTo(Sex.MALE);
}
}
}

View File

@ -28,6 +28,8 @@ public class TClassTableDef extends QueryTable {
public final QueryColumn CLASS_NAME = new QueryColumn(this, "class_name"); public final QueryColumn CLASS_NAME = new QueryColumn(this, "class_name");
public final QueryColumn SEX = new QueryColumn(this, "sex");
public TClassTableDef() { public TClassTableDef() {
super("", "tb_class"); super("", "tb_class");
} }