注意
使用本文的方法会导致Mybatis-plus失效,如果使用了mybatis-plus的同学请绕道;
背景
接下来说明配置MyBatis热加载的步骤(注意,本配置只适用于学习,如果用于正式环境产生的后果一概不负责)
步骤
-
下载所需要的文件(文件还没有通过审核,等审核通过了我更新一下博客):
https://download.csdn.net/download/mate_ge/11243030 -
将MapperRefresh.java,MybatisConfig.java,MyBatisRefreshProperties.java,SqlSessionFactoryBean.java四个文件放在一个包里,无需改动这四个文件;
-
将mybatis-config.xml文件放在resource目录下,这是一个空的myabtis配置文件,如果你的工程中已经有了该配置文件,则无需添加这个空的文件,如果你的myabtis是配置在yml文件中,这个空的配置文件还是必要的;
-
在yml文件中配置如下信息:
mybatis-refresh: enable: true delaySeconds: 5 sleepSeconds: 3 mappingPath: exmaple
enable表示是否启用热加载
delaySeconds表示延迟刷新秒数
sleepSecond表示休眠时间
mappingPath表示xml文件夹匹配字符串,需要根据需要修改 -
注意,mybatis需要配置一些信息,比如:
mybatis: type-aliases-package: com.exmaple configuration: map-underscore-to-camel-case: true default-fetch-size: 100 default-statement-timeout: 30 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl config-location: classpath:mybatis-config.xml mapper-locations: classpath*:com/exmaple/**/*Mapper.xml
-
整个application.xml简单示例:
mybatis: type-aliases-package: com.example configuration: map-underscore-to-camel-case: true default-fetch-size: 100 default-statement-timeout: 30 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl config-location: classpath:mybatis-config.xml mapper-locations: classpath*:com/example/**/*Mapper.xml mybatis-refresh: enable: true delaySeconds: 5 sleepSeconds: 3 mappingPath: example
-
接下来启动的时候,如果开启了热加载,应该就会有这个提示
========= Enabled refresh mybatis mapper =========