In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how SpringBoot solves the problem of TypeAliases configuration failure". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope that this article "how to solve the problem of TypeAliases configuration failure with SpringBoot" can help you solve the problem.
Problem description
When applying MyBatis, object-relational mapping is used to map objects and Aliase.
The documentation in Mybatis clearly states that if you do not clearly define the Aliase of the entity class, the framework will automatically use Class Name as an alias.
So here's the problem. When starting with java-jar xxx.jar&, the following error is reported
Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias "XXXXX" .cause: java.lang.ClassNotFoundException: Cannot find class: XXXXX
Judging from the exception information, it is obvious that the corresponding class of alise cannot be retrieved locally, which eventually leads to the failure of initialization such as sqlSessionFactory. And the hanging rail is that there is no problem to start directly in Idea, and this problem will only occur when starting the jar package.
Solution method
Referring to the article by the blogger A_Beaver, it turns out that the facroty of mybatis needs to load SpringBoot's unique virtual file system to identify the classpath.
Public SpringBootVFS () {this.resourceResolver = new PathMatchingResourcePatternResolver (getClass () .getClassLoader ());}
Judging from the above code, the resource loading is actually realized through PathMatchingResourcePatternResolver.
To fix this problem, you only need to set factory in the configuration class of mybatis
@ Bean (name = "masterSqlSessionFactory") @ Primary public SqlSessionFactory sqlSessionFactory (@ Qualifier ("masterDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean.setDataSource (dataSource); bean.setVfs (SpringBootVFS.class); / / set SpringBootVFS bean.setTypeAliasesPackage ("com.fulan.domain.red");.} SpringBoot integrates Mybatis and pit 1 encountered. Set up the project environment
1.1 create a project
1.2 modify the POM file to add related dependencies
Modify the pom.xml file to 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
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.DruidDataSource2. Configure the generator plug-in for Maven
2.1 add 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
2.2 add generator profile
Name the file generator.xml and add it to src/main/resources.
2.3 add a DTD file for the generator profile
You can add it in File- > Settings in the toolbar, or you can automatically add it by pressing alt+shift directly in the file.
2.4 run the generator plug-in to generate code
3. Configure the resource copy plug-in
3.1 add resource copy plug-in coordinates
Src/main/java * * / * .xml src/main/resources * * / .yml
3.2 modify startup class and add @ 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") / / specifies the package name of the scanning interface and mapping configuration file public class DemoApplication {public static void main (String [] args) {SpringApplication.run (DemoApplication.class, args);}} 4. Other configuration items mybatis: # scan the mapping configuration file under the mapper directory in classpath. Mapper-locations: classpath:/mapper/*.xml # defines the package alias for the mapping file in the resources directory. When using pojo, you can directly use the type name of pojo instead of the package name type-aliases-package: com.example.springbootmybatis.pojo5. Add user featur
5.1 create a page
Test SpringBoot connection to PostgreSQL database
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;/** * Page Jump Controller * / @ Controllerpublic class PageController {/ * Page Jump method * / @ 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;/** * user Management Controller * / @ RestController@RequestMapping ("/ user") public class UsersController {@ Autowired private UsersService usersService / * * add user * / @ PostMapping ("/ addUser") public String addUsers (Users users) {try {this.usersService.addUsers (users);} catch (Exception e) {e.printStackTrace (); return "error";} return "redirect:/ok";}}
5.3 create the Service interface implementation class Impl
/ * user management business layer * / @ Servicepublic class UsersServiceImpl implements UsersService {@ Autowired private UsersMapper usersMapper; @ Override @ Transactional public void addUsers (Users users) {this.usersMapper.insert (users);}}
Interface
Error encountered by public interface UsersService {void addUsers (Users users);}
1. The problem of automatic generation of Mybatis Generator and production of tables with the same name in the database
[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
The answer to this question is given on MyBatis Generator's official website.
Mysql does not support SQL catalogs and schema properly. 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 mysql-connector-java 8.x, generator generates code for the table of the information database (sys, information_schema, performance_schema) in MySql. To avoid this, add the attribute "nullCatalogMeansCurrent=true" to the JDBC URL.
Modify the configuration file generator.xml
two。 500 error occurred on the page
2020-06-27 14 o.a.c.c.C 2315 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
Solution method
Checked a lot of blogs, but not their own problem, their own problem is in the pom.xml configuration file in the resource path, did not write all, but a separate xml and yml configuration files. To load all static resources.
Src/main/resources * * / * .yml * * / * .xml src/main/resources * * / *. This is the end of the introduction on "how to solve the problem of TypeAliases configuration failure by SpringBoot". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.