Mybatis-Plus3. 2.0 metaobjecthandler cannot globally populate public fields
Problem description
Recently, the project architecture team has taken the following measures for the decision of configuration file XML configuration file
For a data console project, because the deployment environment is complex, it may need to adapt to Alibaba, Tencent, open source and other environments and computer rooms, so the configuration file and startup class are three categories
The previously written metaobjecthandler was developed in the springboot configuration mode, and suddenly changed to There are still many holes in the XML configuration file, among which the configured metaobjecthandler does not work ⬆️🔥 (LIT)
Problem code
There is nothing wrong with this method in ordinary spring boot projects
@Component public class MybatisPlusObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject MetaObject) { Date Now = new Date(); Object crteTime = getFieldValByName("crteTime",MetaObject); if (Objects.isNull(crteTime)) { setFieldValByName("crteTime",Now,MetaObject); } Object optTime = getFieldValByName("optTime",MetaObject); if (Objects.isNull(optTime)) { setFieldValByName("optTime",MetaObject); } setFieldValByName("invdFlag",TpcConstant.INVD,MetaObject); } @Override public void updateFill(MetaObject MetaObject) { Object optTime = getFieldValByName("optTime",new Date(),MetaObject); } } }
Solution
Remove the @ component on the mybatisplusobjecthandler class and replace it with Register in XML
<bean class="xxx.xxx.xxx.MybatisPlusObjectHandler" id="myMetaObjectHandler"/> <bean id="globalConfiguration" class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <property name="MetaObjectHandler" ref="myMetaObjectHandler"/> </bean> <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatissqlSessionfactorybean"> <!-- 注入公共字段填充处理器 --> <property name="globalConfig" ref="globalConfiguration" /> </bean>
Problem solving is still a question. Since the springboot development framework is adopted, why do you still use it In this way of XML configuration file, if any big man knows, you are welcome to leave a message or private letter in the comment area 👏
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.