springboot3.2.4+Mybatis-plus在graalvm21环境下打包exe

2024-03-31 22:52

本文主要是介绍springboot3.2.4+Mybatis-plus在graalvm21环境下打包exe,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

springboot3.2.4+Mybatis-plus在graalvm21环境下打包exe

  • 前提条件为之前已经能直接打包springboot3.2.4项目了
  • 然后在此基础上接入Mybatis-plus,然后能够正常进行打包exe并且执行,参考之前的文章进行打包

核心配置如下

package com.example.demo.config;import com.baomidou.mybatisplus.annotation.IEnum;
import com.baomidou.mybatisplus.core.MybatisParameterHandler;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.handlers.CompositeEnumTypeHandler;
import com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.GsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.cache.decorators.FifoCache;
import org.apache.ibatis.cache.decorators.LruCache;
import org.apache.ibatis.cache.decorators.SoftCache;
import org.apache.ibatis.cache.decorators.WeakCache;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.BaseStatementHandler;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.javassist.util.proxy.ProxyFactory;
import org.apache.ibatis.javassist.util.proxy.RuntimeSupport;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl;
import org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.logging.nologging.NoLoggingImpl;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.reflection.TypeParameterResolver;
import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.ResolvableType;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;/*** @author lyk* @since 2024-03-30*/
@Configuration(proxyBeanMethods = false)
@ImportRuntimeHints(MyBatisNativeConfiguration.MyBaitsRuntimeHintsRegistrar.class)
public class MyBatisNativeConfiguration {@BeanMyBatisBeanFactoryInitializationAotProcessor myBatisBeanFactoryInitializationAotProcessor() {return new MyBatisBeanFactoryInitializationAotProcessor();}@Beanstatic MyBatisMapperFactoryBeanPostProcessor myBatisMapperFactoryBeanPostProcessor() {return new MyBatisMapperFactoryBeanPostProcessor();}static class MyBaitsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {@Overridepublic void registerHints(RuntimeHints hints, ClassLoader classLoader) {Stream.of(RawLanguageDriver.class,// TODO 增加了MybatisXMLLanguageDriver.classXMLLanguageDriver.class, MybatisXMLLanguageDriver.class,RuntimeSupport.class,ProxyFactory.class,Slf4jImpl.class,Log.class,JakartaCommonsLoggingImpl.class,Log4j2Impl.class,Jdk14LoggingImpl.class,StdOutImpl.class,NoLoggingImpl.class,SqlSessionFactory.class,PerpetualCache.class,FifoCache.class,LruCache.class,SoftCache.class,WeakCache.class,//TODO 增加了MybatisSqlSessionFactoryBean.classSqlSessionFactoryBean.class, MybatisSqlSessionFactoryBean.class,ArrayList.class,HashMap.class,TreeSet.class,HashSet.class).forEach(x -> hints.reflection().registerType(x, MemberCategory.values()));Stream.of("org/apache/ibatis/builder/xml/*.dtd","org/apache/ibatis/builder/xml/*.xsd").forEach(hints.resources()::registerPattern);hints.serialization().registerType(SerializedLambda.class);hints.serialization().registerType(SFunction.class);hints.serialization().registerType(java.lang.invoke.SerializedLambda.class);hints.reflection().registerType(SFunction.class);hints.reflection().registerType(SerializedLambda.class);hints.reflection().registerType(java.lang.invoke.SerializedLambda.class);hints.proxies().registerJdkProxy(StatementHandler.class);hints.proxies().registerJdkProxy(Executor.class);hints.proxies().registerJdkProxy(ResultSetHandler.class);hints.proxies().registerJdkProxy(ParameterHandler.class);//        hints.reflection().registerType(MybatisPlusInterceptor.class);hints.reflection().registerType(AbstractWrapper.class,MemberCategory.values());hints.reflection().registerType(LambdaQueryWrapper.class,MemberCategory.values());hints.reflection().registerType(LambdaUpdateWrapper.class,MemberCategory.values());hints.reflection().registerType(UpdateWrapper.class,MemberCategory.values());hints.reflection().registerType(QueryWrapper.class,MemberCategory.values());hints.reflection().registerType(BoundSql.class,MemberCategory.DECLARED_FIELDS);hints.reflection().registerType(RoutingStatementHandler.class,MemberCategory.DECLARED_FIELDS);hints.reflection().registerType(BaseStatementHandler.class,MemberCategory.DECLARED_FIELDS);hints.reflection().registerType(MybatisParameterHandler.class,MemberCategory.DECLARED_FIELDS);hints.reflection().registerType(IEnum.class,MemberCategory.INVOKE_PUBLIC_METHODS);// register typeHandlerhints.reflection().registerType(CompositeEnumTypeHandler.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);hints.reflection().registerType(FastjsonTypeHandler.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);hints.reflection().registerType(GsonTypeHandler.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);hints.reflection().registerType(JacksonTypeHandler.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);hints.reflection().registerType(MybatisEnumTypeHandler.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);}}static class MyBatisBeanFactoryInitializationAotProcessorimplements BeanFactoryInitializationAotProcessor, BeanRegistrationExcludeFilter {private final Set<Class<?>> excludeClasses = new HashSet<>();MyBatisBeanFactoryInitializationAotProcessor() {excludeClasses.add(MapperScannerConfigurer.class);}@Override public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {return excludeClasses.contains(registeredBean.getBeanClass());}@Overridepublic BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {String[] beanNames = beanFactory.getBeanNamesForType(MapperFactoryBean.class);if (beanNames.length == 0) {return null;}return (context, code) -> {RuntimeHints hints = context.getRuntimeHints();for (String beanName : beanNames) {BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName.substring(1));PropertyValue mapperInterface = beanDefinition.getPropertyValues().getPropertyValue("mapperInterface");if (mapperInterface != null && mapperInterface.getValue() != null) {Class<?> mapperInterfaceType = (Class<?>) mapperInterface.getValue();if (mapperInterfaceType != null) {registerReflectionTypeIfNecessary(mapperInterfaceType, hints);hints.proxies().registerJdkProxy(mapperInterfaceType);hints.resources().registerPattern(mapperInterfaceType.getName().replace('.', '/').concat(".xml"));registerMapperRelationships(mapperInterfaceType, hints);}}}};}private void registerMapperRelationships(Class<?> mapperInterfaceType, RuntimeHints hints) {Method[] methods = ReflectionUtils.getAllDeclaredMethods(mapperInterfaceType);for (Method method : methods) {if (method.getDeclaringClass() != Object.class) {ReflectionUtils.makeAccessible(method);registerSqlProviderTypes(method, hints, SelectProvider.class, SelectProvider::value, SelectProvider::type);registerSqlProviderTypes(method, hints, InsertProvider.class, InsertProvider::value, InsertProvider::type);registerSqlProviderTypes(method, hints, UpdateProvider.class, UpdateProvider::value, UpdateProvider::type);registerSqlProviderTypes(method, hints, DeleteProvider.class, DeleteProvider::value, DeleteProvider::type);Class<?> returnType = MyBatisMapperTypeUtils.resolveReturnClass(mapperInterfaceType, method);registerReflectionTypeIfNecessary(returnType, hints);MyBatisMapperTypeUtils.resolveParameterClasses(mapperInterfaceType, method).forEach(x -> registerReflectionTypeIfNecessary(x, hints));}}}@SafeVarargsprivate <T extends Annotation> void registerSqlProviderTypes(Method method, RuntimeHints hints, Class<T> annotationType, Function<T, Class<?>>... providerTypeResolvers) {for (T annotation : method.getAnnotationsByType(annotationType)) {for (Function<T, Class<?>> providerTypeResolver : providerTypeResolvers) {registerReflectionTypeIfNecessary(providerTypeResolver.apply(annotation), hints);}}}private void registerReflectionTypeIfNecessary(Class<?> type, RuntimeHints hints) {if (!type.isPrimitive() && !type.getName().startsWith("java")) {hints.reflection().registerType(type, MemberCategory.values());}}}static class MyBatisMapperTypeUtils {private MyBatisMapperTypeUtils() {// NOP}static Class<?> resolveReturnClass(Class<?> mapperInterface, Method method) {Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);return typeToClass(resolvedReturnType, method.getReturnType());}static Set<Class<?>> resolveParameterClasses(Class<?> mapperInterface, Method method) {return Stream.of(TypeParameterResolver.resolveParamTypes(method, mapperInterface)).map(x -> typeToClass(x, x instanceof Class ? (Class<?>) x : Object.class)).collect(Collectors.toSet());}private static Class<?> typeToClass(Type src, Class<?> fallback) {Class<?> result = null;if (src instanceof Class<?>) {if (((Class<?>) src).isArray()) {result = ((Class<?>) src).getComponentType();} else {result = (Class<?>) src;}} else if (src instanceof ParameterizedType) {ParameterizedType parameterizedType = (ParameterizedType) src;int index = (parameterizedType.getRawType() instanceof Class&& Map.class.isAssignableFrom((Class<?>) parameterizedType.getRawType())&& parameterizedType.getActualTypeArguments().length > 1) ? 1 : 0;Type actualType = parameterizedType.getActualTypeArguments()[index];result = typeToClass(actualType, fallback);}if (result == null) {result = fallback;}return result;}}static class MyBatisMapperFactoryBeanPostProcessor implements MergedBeanDefinitionPostProcessor, BeanFactoryAware {private static final org.apache.commons.logging.Log LOG = LogFactory.getLog(MyBatisMapperFactoryBeanPostProcessor.class);private static final String MAPPER_FACTORY_BEAN = "org.mybatis.spring.mapper.MapperFactoryBean";private ConfigurableBeanFactory beanFactory;@Overridepublic void setBeanFactory(BeanFactory beanFactory) {this.beanFactory = (ConfigurableBeanFactory) beanFactory;}@Overridepublic void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {if (ClassUtils.isPresent(MAPPER_FACTORY_BEAN, this.beanFactory.getBeanClassLoader())) {resolveMapperFactoryBeanTypeIfNecessary(beanDefinition);}}private void resolveMapperFactoryBeanTypeIfNecessary(RootBeanDefinition beanDefinition) {if (!beanDefinition.hasBeanClass() || !MapperFactoryBean.class.isAssignableFrom(beanDefinition.getBeanClass())) {return;}if (beanDefinition.getResolvableType().hasUnresolvableGenerics()) {Class<?> mapperInterface = getMapperInterface(beanDefinition);if (mapperInterface != null) {// Exposes a generic type information to context for prevent early initializingConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();constructorArgumentValues.addGenericArgumentValue(mapperInterface);beanDefinition.setConstructorArgumentValues(constructorArgumentValues);beanDefinition.setTargetType(ResolvableType.forClassWithGenerics(beanDefinition.getBeanClass(), mapperInterface));}}}private Class<?> getMapperInterface(RootBeanDefinition beanDefinition) {try {return (Class<?>) beanDefinition.getPropertyValues().get("mapperInterface");}catch (Exception e) {LOG.debug("Fail getting mapper interface type.", e);return null;}}}
}
package com.example.demo.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author lyk* @since 2024-03-30*/
@Configuration
//TODO 如果使用这个方式的话,记得一定要指定sqlSessionTemplateRef或sqlSessionFactoryRef https://github.com/mybatis/spring-boot-starter/wiki/Quick-Start-for-building-native-image#how-to-use-mapperscan
@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionTemplateRef = "sqlSessionTemplate")
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();//plusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));//TODO 3.5.3.1 以下插件多的话,可能会报异常,这个在最新版本修改.  https://github.com/baomidou/mybatis-plus/issues/5532
//        plusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
//        plusInterceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
//        plusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return plusInterceptor;}}

demo地址如下

https://gitee.com/lgbisha/springboot3-mybatis-plus/tree/master

这篇关于springboot3.2.4+Mybatis-plus在graalvm21环境下打包exe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/865573

相关文章

mybatis的整体架构

mybatis的整体架构分为三层: 1.基础支持层 该层包括:数据源模块、事务管理模块、缓存模块、Binding模块、反射模块、类型转换模块、日志模块、资源加载模块、解析器模块 2.核心处理层 该层包括:配置解析、参数映射、SQL解析、SQL执行、结果集映射、插件 3.接口层 该层包括:SqlSession 基础支持层 该层保护mybatis的基础模块,它们为核心处理层提供了良好的支撑。

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推

springboot3打包成war包,用tomcat8启动

1、在pom中,将打包类型改为war <packaging>war</packaging> 2、pom中排除SpringBoot内置的Tomcat容器并添加Tomcat依赖,用于编译和测试,         *依赖时一定设置 scope 为 provided (相当于 tomcat 依赖只在本地运行和测试的时候有效,         打包的时候会排除这个依赖)<scope>provided

安装nodejs环境

本文介绍了如何通过nvm(NodeVersionManager)安装和管理Node.js及npm的不同版本,包括下载安装脚本、检查版本并安装特定版本的方法。 1、安装nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash 2、查看nvm版本 nvm --version 3、安装

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

高并发环境中保持幂等性

在高并发环境中保持幂等性是一项重要的挑战。幂等性指的是无论操作执行多少次,其效果都是相同的。确保操作的幂等性可以避免重复执行带来的副作用。以下是一些保持幂等性的常用方法: 唯一标识符: 请求唯一标识:在每次请求中引入唯一标识符(如 UUID 或者生成的唯一 ID),在处理请求时,系统可以检查这个标识符是否已经处理过,如果是,则忽略重复请求。幂等键(Idempotency Key):客户端在每次

pico2 开发环境搭建-基于ubuntu

pico2 开发环境搭建-基于ubuntu 安装编译工具链下载sdk 和example编译example 安装编译工具链 sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib 注意cmake的版本,需要在3.17 以上 下载sdk 和ex

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip

跨系统环境下LabVIEW程序稳定运行

在LabVIEW开发中,不同电脑的配置和操作系统(如Win11与Win7)可能对程序的稳定运行产生影响。为了确保程序在不同平台上都能正常且稳定运行,需要从兼容性、驱动、以及性能优化等多个方面入手。本文将详细介绍如何在不同系统环境下,使LabVIEW开发的程序保持稳定运行的有效策略。 LabVIEW版本兼容性 LabVIEW各版本对不同操作系统的支持存在差异。因此,在开发程序时,尽量使用