千家信息网

SpringBoot怎么解决TypeAliases配置失败问题

发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇文章主要介绍"SpringBoot怎么解决TypeAliases配置失败问题"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"SpringBoot怎么解决Ty
千家信息网最后更新 2025年11月11日SpringBoot怎么解决TypeAliases配置失败问题

这篇文章主要介绍"SpringBoot怎么解决TypeAliases配置失败问题"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"SpringBoot怎么解决TypeAliases配置失败问题"文章能帮助大家解决问题。

问题描述

在应用MyBatis时,使用对象关系映射,将对象和Aliase映射起来。

在Mybatis的文档明确写出,如果你没有明确定义实体类的Aliase,框架会自动将Class Name自动作为别名。

那么问题来了,当使用java -jar xxx.jar&启动的时候,会报出以下错误,

Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias "XXXXX".Cause: java.lang.ClassNotFoundException: Cannot find class: XXXXX

从异常信息来看,明显就是无法从本地检索到alise对应的类,并最终导致sqlSessionFactory等初始化失败。而且吊轨的是,直接在Idea中启动是没有问题的,启动jar包才会出现这个问题

解决方法

参考博主A_Beaver的文章,原来mybatis的facroty需要加载SpringBoot独特的虚拟文件系统,才能识别类路径

public SpringBootVFS() {    this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());}

从以上代码看,其实是通过PathMatchingResourcePatternResolver实现资源的加载

修复该问题只需要在mybatis的配置类中,设置一下factory即可,

@Bean(name = "masterSqlSessionFactory")    @Primary    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        bean.setVfs(SpringBootVFS.class);//设置SpringBootVFS        bean.setTypeAliasesPackage("com.fulan.domain.red");        ...    }

SpringBoot整合Mybatis及遇到的坑

1. 搭建项目环境

1.1 创建项目

1.2 修改POM文件,添加相关依赖

修改pom.xml文件,在其中添加下面依赖。

  org.springframework.boot spring-boot-starter-thymeleaf   org.springframework.boot spring-boot-starter-web    org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3    org.springframework.boot spring-boot-starter-jdbc    mysql mysql-connector-java 8.0.12    com.alibaba druid 1.1.10 

1.3 配置数据源

在application.yml文件中配置如下代码。

spring:  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEnconding=utf-8&useSSL=false&serverTimezone=GMT%2B8    username: root    password: root    type: com.alibaba.druid.pool.DruidDataSource

2. 配置Maven的generator插件

2.1 添加generator插件坐标

  org.mybatis.generator mybatis-generator-maven-plugin 1.4.0   mysql mysql-connector-java 8.0.12     ${project.basedir}/src/main/resources/generator.xml true true  

2.2 添加generator配置文件

将文件命名为generator.xml,在src/main/resources中添加。

                                                                                                                                                                                                                                                                                                                                                                                                                                                    

2.3 添加generator配置文件的DTD文件

可以在工具栏中的File->Settings中添加,也可以直接在文件中按alt+shift自动添加。


2.4 运行generator插件生成代码

3. 配置资源拷贝插件

3.1 添加资源拷贝插件坐标

   src/main/java  **/*.xml    src/main/resources  **/*.yml   

3.2 修改启动类添加@MapperScan注解

package com.example.springbootmybatis;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.example.springbootmybatis.mapper")//指定扫描接口与映射配置文件的包名public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}

4. 其他配置项

mybatis:  # 扫描classpath中mapper目录下的映射配置文件,针对于映射文件放到了resources目录下  mapper-locations: classpath:/mapper/*.xml  # 定义包别名,使用pojo时可以直接使用pojo的类型名称不用加包名  type-aliases-package: com.example.springbootmybatis.pojo

5. 添加用户功能

5.1 创建页面

        测试SpringBoot连接PostgreSQL数据库    




5.2 创建Controller

5.2.1 PageController

package com.example.springbootmybatis.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;/** * 页面跳转Controller */@Controllerpublic class PageController {    /**     * 页面跳转方法     */    @RequestMapping("/{page}")    public String showPage(@PathVariable String page){        return page;    }}

5.2.2 UsersController

package com.example.springbootmybatis.controller;import com.example.springbootmybatis.pojo.Users;import com.example.springbootmybatis.service.UsersService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * 用户管理Controller */@RestController@RequestMapping("/user")public class UsersController {    @Autowired    private UsersService usersService;    /**     * 添加用户     */    @PostMapping("/addUser")    public String addUsers(Users users){        try {            this.usersService.addUsers(users);        } catch (Exception e){            e.printStackTrace();            return "error";        }        return "redirect:/ok";    }}

5.3 创建Service 接口实现类Impl

/** * 用户管理业务层 */@Servicepublic class UsersServiceImpl implements UsersService {    @Autowired    private UsersMapper usersMapper;    @Override    @Transactional    public void addUsers(Users users) {        this.usersMapper.insert(users);    }}

接口

public interface UsersService {    void addUsers(Users users);}

遇到的错误

1. Mybatis Generator自动生成,数据库的同名表也会生产的问题

[WARNING] Table Configuration users matched more than one table (test..users,performance_schema..users)

[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete

在 MyBatis Generator官网 中对这一问题做出了解答。

翻译如下:Mysql 无法正常支持 SQL catalogs 和 schema。因此,最好不要在 generator 配置文件中指定 catalog 以及schema,仅需指定数据表的名字并在 JDBC URL 中指定数据库即可。如果使用 mysql-connector-java 8.x 版本,generator 会为MySql中信息数据库(sys, information_schema, performance_schema)的表生成代码,若要避免这种操作,请在 JDBC URL 中加入属性"nullCatalogMeansCurrent=true"。

修改配置文件generator.xml

                    

2. 页面出现500错误

2020-06-27 14:23:42.459 ERROR 19676 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [addUsers]: would dispatch back to the current handler URL [/addUsers] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause

javax.servlet.ServletException: Circular view path [addUsers]: would dispatch back to the current handler URL [/addUsers] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:210) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at

解决方法

查了很多博客,但是都不是自己的问题,自己的问题是在pom.xml配置文件中的资源路径中,没有写所有,而是单独的xml和yml配置文件。要加载所有的静态资源。

  src/main/resources  **/*.yml                    **/*.xml     src/main/resources  **/*.*   

关于"SpringBoot怎么解决TypeAliases配置失败问题"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

配置 文件 问题 数据 插件 资源 代码 数据库 方法 用户 页面 接口 知识 错误 生成 中指 信息 别名 坐标 对象 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 plsql 还原数据库备份 数据库条目最大值 佛山拼团软件开发定制 网络安全发展保障措施 开启查找设备无法连接服务器 我的世界寻找好玩的服务器 oa系统没有数据库 可以检索标准文献的数据库或系统 网络服务器经销商的特征 pg_ctl关闭数据库失败 微信小程序与服务器通关 网络安全任我行手抄报 其软件安装与管理与控制服务器上 数控系统与工程软件开发 海西州软件开发优化价格 领信互联网科技信息有限公司 汇丰银行软件开发工程师待遇 数据库查看实例名的命令 办公系统软件开发合同简版本 软件开发视频教程百度云盘 杨浦区网络营销软件开发报价方案 green代理服务器 大兴区正规软件开发要求 专业网络安全建设方案供应商 鸿宇网络技术公司 华为服务器bios 靖江游戏软件开发 邵阳网络安全日 sql写入其他数据库 新疆电力信息网络安全相关规定
0