这篇文章主要介绍了SpringBoot整合Mybatis,解决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独特的虚拟文件系统,才能识别类路径
1 2 3 |
|
从以上代码看,其实是通过PathMatchingResourcePatternResolver实现资源的加载
修复该问题只需要在mybatis的配置类中,设置一下factory即可,
1 2 3 4 5 6 7 8 9 |
|
SpringBoot整合Mybatis及遇到的坑
1. 搭建项目环境
1.1 创建项目
1.2 修改POM文件,添加相关依赖
修改pom.xml文件,在其中添加下面依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
1.3 配置数据源
在application.yml文件中配置如下代码。
1 2 3 4 5 6 7 |
|
2. 配置Maven的generator插件
2.1 添加generator插件坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
2.2 添加generator配置文件
将文件命名为generator.xml,在src/main/resources中添加。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
2.3 添加generator配置文件的DTD文件
可以在工具栏中的File->Settings中添加,也可以直接在文件中按alt+shift自动添加。
2.4 运行generator插件生成代码
3. 配置资源拷贝插件
3.1 添加资源拷贝插件坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
3.2 修改启动类添加@MapperScan注解
1 2 3 4 5 6 7 8 9 10 11 |
|
4. 其他配置项
1 2 3 4 5 |
|
5. 添加用户功能
5.1 创建页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
5.2 创建Controller
5.2.1 PageController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
5.2.2 UsersController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
5.3 创建Service 接口实现类Impl
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
接口
1 2 3 |
|
遇到的错误
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
1 2 3 4 5 |
|
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
解决方法
查了很多博客,但是都不是www.1818xinwen.com自己的问题,自己的问题是在pom.xml配置文件中的资源路径中,没有写所有,而是单独的xml和yml配置文件。要加载所有的静态资源。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
以上为个人经验,希望能给大家一个参考