mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
add @Table(onSet) config
This commit is contained in:
parent
87ee9b351f
commit
970fc6d34d
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package com.mybatisflex.annotation;
|
||||
|
||||
public class NoneListener implements InsertListener, UpdateListener {
|
||||
public class NoneListener implements InsertListener, UpdateListener, SetListener {
|
||||
@Override
|
||||
public void onInsert(Object entity) {
|
||||
|
||||
@ -25,4 +25,9 @@ public class NoneListener implements InsertListener, UpdateListener {
|
||||
public void onUpdate(Object entity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object onSet(Object entity, String property, Object value) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 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.annotation;
|
||||
|
||||
public interface SetListener {
|
||||
|
||||
Object onSet(Object entity, String property, Object value);
|
||||
}
|
||||
@ -51,4 +51,9 @@ public @interface Table {
|
||||
* 监听 entity 的 update 行为
|
||||
*/
|
||||
Class<? extends UpdateListener> onUpdate() default NoneListener.class;
|
||||
|
||||
/**
|
||||
* 监听 entity 的查询数据的 set 行为,用户主动 set 不会触发
|
||||
*/
|
||||
Class<? extends SetListener> onSet() default NoneListener.class;
|
||||
}
|
||||
@ -21,6 +21,7 @@ import com.mybatisflex.core.keygen.MultiRowKeyGenerator;
|
||||
import com.mybatisflex.core.keygen.MybatisKeyGeneratorUtil;
|
||||
import com.mybatisflex.core.keygen.RowKeyGenerator;
|
||||
import com.mybatisflex.core.row.RowMapper;
|
||||
import com.mybatisflex.core.table.EntityWrapperFactory;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
@ -35,7 +36,6 @@ import org.apache.ibatis.mapping.BoundSql;
|
||||
import org.apache.ibatis.mapping.Environment;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.ResultMap;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.ResultHandler;
|
||||
import org.apache.ibatis.session.RowBounds;
|
||||
@ -50,6 +50,7 @@ public class FlexConfiguration extends Configuration {
|
||||
public FlexConfiguration(Environment environment) {
|
||||
super(environment);
|
||||
setMapUnderscoreToCamelCase(true);
|
||||
setObjectWrapperFactory(new EntityWrapperFactory());
|
||||
initDefaultMappers();
|
||||
}
|
||||
|
||||
@ -66,11 +67,6 @@ public class FlexConfiguration extends Configuration {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MetaObject newMetaObject(Object object) {
|
||||
return super.newMetaObject(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为原生 sql 设置参数
|
||||
*/
|
||||
|
||||
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 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.core.table;
|
||||
|
||||
import com.mybatisflex.annotation.SetListener;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.apache.ibatis.reflection.property.PropertyTokenizer;
|
||||
import org.apache.ibatis.reflection.wrapper.BeanWrapper;
|
||||
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
|
||||
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public class EntityWrapperFactory implements ObjectWrapperFactory {
|
||||
|
||||
@Override
|
||||
public boolean hasWrapperFor(Object object) {
|
||||
Class<?> objectClass = object.getClass();
|
||||
if (Map.class.isAssignableFrom(objectClass) ||
|
||||
Collection.class.isAssignableFrom(objectClass)) {
|
||||
return false;
|
||||
}
|
||||
return TableInfoFactory.getByEntityClass(objectClass) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
|
||||
return new FlexBeanWrapper(metaObject, object);
|
||||
}
|
||||
|
||||
static class FlexBeanWrapper extends BeanWrapper {
|
||||
|
||||
private Object entity;
|
||||
private SetListener onSetListener;
|
||||
|
||||
public FlexBeanWrapper(MetaObject metaObject, Object object) {
|
||||
super(metaObject, object);
|
||||
this.entity = object;
|
||||
this.onSetListener = TableInfoFactory.getByEntityClass(object.getClass()).getOnSetListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(PropertyTokenizer prop, Object value) {
|
||||
Object v = value;
|
||||
if (onSetListener != null) {
|
||||
v = onSetListener.onSet(entity, prop.getName(), value);
|
||||
}
|
||||
super.set(prop, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,6 +16,7 @@
|
||||
package com.mybatisflex.core.table;
|
||||
|
||||
import com.mybatisflex.annotation.InsertListener;
|
||||
import com.mybatisflex.annotation.SetListener;
|
||||
import com.mybatisflex.annotation.UpdateListener;
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
@ -79,6 +80,7 @@ public class TableInfo {
|
||||
|
||||
private InsertListener onInsertListener;
|
||||
private UpdateListener onUpdateListener;
|
||||
private SetListener onSetListener;
|
||||
|
||||
|
||||
private final ReflectorFactory reflectorFactory = new BaseReflectorFactory() {
|
||||
@ -223,6 +225,14 @@ public class TableInfo {
|
||||
this.onUpdateListener = onUpdateListener;
|
||||
}
|
||||
|
||||
public SetListener getOnSetListener() {
|
||||
return onSetListener;
|
||||
}
|
||||
|
||||
public void setOnSetListener(SetListener onSetListener) {
|
||||
this.onSetListener = onSetListener;
|
||||
}
|
||||
|
||||
public List<ColumnInfo> getColumnInfoList() {
|
||||
return columnInfoList;
|
||||
}
|
||||
@ -523,6 +533,9 @@ public class TableInfo {
|
||||
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
||||
if (columnInfo != null && metaObject.hasSetter(columnInfo.property)) {
|
||||
Object value = ConvertUtil.convert(row.get(column), metaObject.getSetterType(columnInfo.property));
|
||||
if (onSetListener != null) {
|
||||
value = onSetListener.onSet(instance, columnInfo.property, value);
|
||||
}
|
||||
metaObject.setValue(columnInfo.property, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,6 +77,11 @@ public class TableInfoFactory {
|
||||
}
|
||||
|
||||
|
||||
public static TableInfo getByEntityClass(Class<?> entityClass) {
|
||||
return tableInfoMap.get(entityClass);
|
||||
}
|
||||
|
||||
|
||||
private static Class<?> getEntityClass(Class<?> mapperClass) {
|
||||
Type[] genericInterfaces = mapperClass.getGenericInterfaces();
|
||||
if (genericInterfaces.length == 1) {
|
||||
@ -112,6 +117,10 @@ public class TableInfoFactory {
|
||||
tableInfo.setOnUpdateListener(ClassUtil.newInstance(table.onUpdate()));
|
||||
}
|
||||
|
||||
if (table.onSet() != NoneListener.class){
|
||||
tableInfo.setOnSetListener(ClassUtil.newInstance(table.onSet()));
|
||||
}
|
||||
|
||||
if (StringUtil.isNotBlank(table.dataSource())){
|
||||
tableInfo.setDataSource(table.dataSource());
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Table(value = "tb_account",dataSource = "ds2")
|
||||
@Table(value = "tb_account",dataSource = "ds2",onSet = AccountOnSetListener.class)
|
||||
public class Account {
|
||||
|
||||
@Id(keyType = KeyType.Auto)
|
||||
|
||||
@ -0,0 +1,12 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.annotation.SetListener;
|
||||
|
||||
public class AccountOnSetListener implements SetListener {
|
||||
@Override
|
||||
public Object onSet(Object entity, String property, Object value) {
|
||||
// System.out.println(">>>>>>> entity: " + entity);
|
||||
System.out.println(">>>>>>> property: " + property +" value:" + value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -16,11 +16,13 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
public class EntityTestStarter {
|
||||
|
||||
@ -44,10 +46,14 @@ public class EntityTestStarter {
|
||||
// System.out.println(account);
|
||||
|
||||
AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Account account = accountMapper.selectOneById(1);
|
||||
}
|
||||
|
||||
|
||||
List<Account> accounts = accountMapper.selectAll();
|
||||
System.out.println(accounts);
|
||||
//
|
||||
long l = accountMapper.selectCountByQuery(QueryWrapper.create());
|
||||
System.out.println("count: "+ l);
|
||||
|
||||
// System.out.println(account);
|
||||
//
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user