mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
test:增加单测——issues #302
This commit is contained in:
parent
eb23046d4a
commit
7d24d19fbd
@ -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<ContextRefreshedEvent> {
|
||||
factoryBean.setDataSource(dataSource);
|
||||
FlexConfiguration configuration = new FlexConfiguration();
|
||||
configuration.setLogImpl(StdOutImpl.class);
|
||||
configuration.setDefaultEnumTypeHandler(EnumTypeHandler.class);
|
||||
factoryBean.setConfiguration(configuration);
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
|
||||
@ -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 +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
package com.mybatisflex.test.model;
|
||||
|
||||
/**
|
||||
* 基础枚举接口
|
||||
* @author luozhan
|
||||
*/
|
||||
public interface BaseEnum {
|
||||
|
||||
/**
|
||||
* 获取枚举值
|
||||
* @return 枚举值
|
||||
*/
|
||||
int getValue();
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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');
|
||||
|
||||
@ -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`
|
||||
|
||||
@ -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));
|
||||
// 注意,此处虽然显示是MALE、FEMALE,但是如果使用了枚举的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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user