Home> Java> javaTutorial> body text

How SpringBoot solves the problem of TypeAliases configuration failure

WBOY
Release: 2023-05-16 12:22:11
forward
1614 people have browsed it

Problem Description

When applying MyBatis, use object-relational mapping to map objects and Aliase.

It is clearly written in the Mybatis documentation that if you do not clearly define the Aliase of the entity class, the framework will automatically use the Class Name as an alias.

Then the problem comes. When using java -jar xxx.jar& to start, the following error will be reported,

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

From the exception information, it is obvious that the alise corresponding cannot be retrieved locally class, and eventually caused the initialization failure of sqlSessionFactory and so on. Moreover, the hanging rail is that there is no problem if you start it directly in Idea. This problem will only occur when you start the jar package.

Solution

Refer to the article of blogger A_Beaver. It turns out that the facroty of mybatis needs Only by loading SpringBoot's unique virtual file system can the class path be identified.

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

From the above code, the resource loading is actually implemented through PathMatchingResourcePatternResolver.

To fix this problem, you only need to configure the mybatis configuration class. Just set up the 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"); ... }
Copy after login

SpringBoot integrates Mybatis and encounters pitfalls

1. Build the project environment

1.1 Create the project

How SpringBoot solves the problem of TypeAliases configuration failure

1.2 Modify the POM file and add relevant dependencies

Modify the pom.xml file and add the following dependencies.

  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 
Copy after login

1.3 Configure the data source

Configure the following code in the application.yml file.

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
Copy after login

2. Configure the Maven generator plug-in

2.1 Add the generator plug-in coordinates

  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  
Copy after login

2.2 Add the generator configuration file

Name the file generator.xml and add it to src/main/resources.

                                 
Copy after login

2.3 Add the DTD file of the generator configuration file

You can add it in File->Settings in the toolbar, or you can press alt shift directly in the file Automatically added.


How SpringBoot solves the problem of TypeAliases configuration failure

2.4 Run the generator plug-in to generate code

How SpringBoot solves the problem of TypeAliases configuration failure

How SpringBoot solves the problem of TypeAliases configuration failure

3. Configure the resource copy plug-in

3.1 Add the coordinates of the resource copy plug-in

   src/main/java  **/*.xml    src/main/resources  **/*.yml   
Copy after login

3.2 Modify the startup class to add the @MapperScan annotation

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); } }
Copy after login

4. Other configuration items

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

5. Add user functions

5.1 Create page

     测试SpringBoot连接PostgreSQL数据库 




Copy after login

5.2 Create 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 */ @Controller public class PageController { /** * 页面跳转方法 */ @RequestMapping("/{page}") public String showPage(@PathVariable String page){ return page; } }
Copy after login

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"; } }
Copy after login

5.3 Create Service interface implementation class Impl

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

Interface

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

Errors encountered

1. Mybatis Generator is automatically generated, and the database table with the same name will also be generated

[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

This question is answered on the MyBatis Generator official website.

How SpringBoot solves the problem of TypeAliases configuration failure

The translation is as follows: Mysql cannot properly support SQL catalogs and schema. Therefore, it is best not to specify catalog and schema in the generator configuration file. Just specify the name of the data table and specify the database in the JDBC URL. If you use the mysql-connector-java 8.x version, the generator will generate code for the tables of the information database (sys, information_schema, performance_schema) in MySql. To avoid this operation, please add the attribute "nullCatalogMeansCurrent=true" to the JDBC URL .

Modify the configuration file generator.xml

  
Copy after login

2. A 500 error appears on the page

How SpringBoot solves the problem of TypeAliases configuration failure

How SpringBoot solves the problem of TypeAliases configuration failure

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  **/*.*   
Copy after login

The above is the detailed content of How SpringBoot solves the problem of TypeAliases configuration failure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!