spring boot problems encountered in the project to build

  Build yourself about spring boot of the project, halfway we encountered a few problems, here to record it!

One of the problems with regard to the design table in the database

1, two fields created design table when we must add the updated create time and update time these two fields is a must
2, design database table field must be standardized, because if a paging assistant, your table field design non-standard, there will be a lot of questions
such as: my book_info table field has a hump is pubInfo nomenclature, but others are book_name, img_url underlined nomenclature, resulting in time and paging using a generic mapper assistant, can not find cause pub_info
this column, I guess inside his plug is underlined nomenclature used in the fields of it, such things must be taken into account before the design, specification is an underscore that named method
... this can be their own settings. In the entity classes with annotations way to set the table name and the name of the column, which led me to re-design a table, however, the best bind themselves to comply with norms

3, the database design field table must be set when the character set, is the best general utf-8, to avoid distortion of the problem in the future when the data into the database

Second, build the project in question

Integrated paging assistant at Universal mapper and data pool and to optimize database connection to access it, as well as paging assistant general mapper can simplify a lot of code to improve development efficiency

1, Paging Assistant version problem

Correct maven dependencies in each version:

 1     <!--通用mapper -->
 2     <dependency>
 3         <groupId>tk.mybatis</groupId>
 4         <artifactId>mapper-spring-boot-starter</artifactId>
 5         <version>1.1.0</version>
 6     </dependency>
 7     <!--pagehelper分页助手 -->
 8     <dependency>
 9         <groupId>com.github.pagehelper</groupId>
10         <artifactId>pagehelper-spring-boot-starter</artifactId>
11         <version>1.2.10</version>
12     </dependency>
13     <!- Ali Baba Data Source ->
15     <dependency>
14         <groupId>com.alibaba</groupId>
16         <artifactId>druid</artifactId>
17         <version>1.0.25</version>
18     </dependency>

There should be noted that the paging assistant, that previously used is 1.1.0, but springboot 2.1.7 and the latest integrated return the wrong time, to reproduce a bit error message:

 1 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration': Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
 2     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:570)
 3     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
 4     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
 5     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 6     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
 7     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
 8     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
 9     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
10     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
11     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
12     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
13     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
14     at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
15     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
16     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
17     at com.example.demo.web.DemoWebApplication.main(DemoWebApplication.java:18)
18 Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
19     at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:760)
20     at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:692)
21     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:365)
22     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:350)
23     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:298)
24     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1077)
25     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:567)
26     ... 15 common frames omitted
27 Caused by: java.lang.NoClassDefFoundError: Lorg/springframework/boot/bind/RelaxedPropertyResolver;
28     at java.lang.Class.getDeclaredFields0(Native Method)
29     at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
30     at java.lang.Class.getDeclaredFields(Class.java:1916)
31     at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:755)
32     ... 21 common frames omitted
33 Caused by: java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver
34     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
35     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
36     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
37     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
38     ... 25 common frames omitted

This error message seems to be watching and AppClassLoader related class loader loads the class RelaxedPropertyResolver less than the required, specific interior is how to solve this problem, do not know, through the online help is for the version with a jar of 1.2.10 version is OK

2, issue application.properties file configuration

The problem here is the issue of universal mapper: look at the correct configuration

1  #通用folders配置
 2 mapper.mappers = com.example.demo.dao.BaseMapper
 3 mapper.not-empty = false 
4 mapper.identity = MYSQL

And there are a configuration Note: mapper.mappers this configuration, this configuration is BaseMapper need to configure the full path of the mapper class that you write infrastructure code:

1 /**
2  * @program: demo
3  * @description: 基础的mapper
4  * @author: ssc
5  * @create: 2019/8/20 10:21
6  **/
7 public interface BaseMapper<T> extends Mapper<T>,MySqlMapper<T>{
8 
9 }

If this is not configured correctly, the start time will be given a specific error message:

1 Description:
2 Failed to bind properties under 'mapper.mappers' to java.util.List<java.lang.Class<?>>:
3     Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.List<java.lang.Class<?>>]

Add: There is a problem that can BaseMapper and business issues in the mapper classes into a package, this has not been verified, anyway, now it is not put into a package that is not a problem

TODO: This validation tomorrow at
August 21, 2019 09:46:30 today's verify this, this seems to be not enough, due to the additional @MapperScan ( "com.example.demo.dao.mapper" in the startup category ) notes, can not let the scan to start classes this BaseMapper and @MapperScan not put the same package the following annotation, or will be error, failed to create

 1 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.pagehelper.autoconfigure.MapperAutoConfiguration': Invocation of init method failed; nested exception is java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
 2     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
 3     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414)
 4     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
 5     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
 6     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
 7     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
 8     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 9     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
10     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
11     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
12     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
13     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
14     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
15     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
16     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
17     at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
18     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
19     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
20     at com.example.demo.web.DemoWebApplication.main(DemoWebApplication.java:18)
21 Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
22     at tk.mybatis.mapper.mapperhelper.MapperHelper.setSqlSource(MapperHelper.java:247)
23     at tk.mybatis.mapper.mapperhelper.MapperHelper.processConfiguration(MapperHelper.java:310)
24     at tk.mybatis.mapper.mapperhelper.MapperHelper.processConfiguration(MapperHelper.java:289)
25     at com.github.pagehelper.autoconfigure.MapperAutoConfiguration.addPageInterceptor(MapperAutoConfiguration.java:69)
26     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
27     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
28     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
29     at java.lang.reflect.Method.invoke(Method.java:498)
30     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
31     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
32     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
33     ... 18 common frames omitted
34 Caused by: java.lang.RuntimeException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
35     at tk.mybatis.mapper.mapperhelper.MapperTemplate.setSqlSource(MapperTemplate.java:246)
36     at tk.mybatis.mapper.mapperhelper.MapperHelper.setSqlSource(MapperHelper.java:244)
37     ... 28 common frames omitted
38 Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
39     at tk.mybatis.mapper.mapperhelper.MapperTemplate.getEntityClass(MapperTemplate.java:278)
40     at tk.mybatis.mapper.provider.base.BaseSelectProvider.selectCount(BaseSelectProvider.java:111)
41     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
42     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
43     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
44     at java.lang.reflect.Method.invoke(Method.java:498)
45     at tk.mybatis.mapper.mapperhelper.MapperTemplate.setSqlSource(MapperTemplate.java:234)
46     ... 29 common frames omitted

3, with the corresponding database table entity class problem

This is some of the fields and the entity class properties, and the corresponding table name issue with the class name, and this is very disgusting, is not very familiar with, so always being given, when creating an entity class, the best in the class name plus the @Table (name = "name table") and plus @Column (name = "field name") such as annotation to appear in the associated error prevention properties

4, BaseService packaging problems

Look at the error message:

 1 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'tk.mybatis.mapper.common.Mapper<?>' available: expected single matching bean but found 2: bookInfoMapper,bookMapper
 2     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324)
 3     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
 4     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
 5     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
 6     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
 7     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 8     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
 9     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
10     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
11     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
12     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
13     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
14     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
15     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
16     at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
17     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
18     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
19     at com.example.demo.web.DemoWebApplication.main(DemoWebApplication.java:18)
20 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'tk.mybatis.mapper.common.Mapper<?>' available: expected single matching bean but found 2: bookInfoMapper,bookMapper
21     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324)
22     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
23     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
24     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
25     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
26     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
27     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
28     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
29     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:452)
30     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:526)
31     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
32     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:636)
33     at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)
34     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
35     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321)
36     ... 17 common frames omitted
37 Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'tk.mybatis.mapper.common.Mapper<?>' available: expected single matching bean but found 2: bookInfoMapper,bookMapper
38     at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:221)
39     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1229)
40     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171)
41     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:520)
42     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
43     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:636)
44     at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)
45     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
46     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321)
47     ... 31 common frames omitted

The root cause of the error is in the common service package, because that is BaseService this class, I began to write is used
@Autowired
Private Mapper <T> Mapper;

Why write that mistake will happen? Analyze can know when the service layer class inherits BaseService of a newly created time, BaseService need to automatically inject Mapper class, but this time Mapper discovered spring container has multiple class implements Mapper, which in the end is an implementation class to use it he himself confused, so he throws an exception, and found a number of implementation class, how to solve this problem before now can not remember, but you can take a step back, do not automatically injected Mapper class in BaseService I clearly tell you you need to realize that Mapper type of class.

Specific solution is:

I created an abstract method, subclass inherits me when I need to give a specific category
public abstract Mapper <T> getMapper ( );

For example: subclass BookService direct you to achieve BookMapper to the parent on the line, so there is not that I do not know what the situation subclass implements:

 1 @Service
 2 public class BookService extends BaseService<BookInfo>{
 3 
 4     private Logger logger = LoggerFactory.getLogger(BookService.class);
 5 
 6     @Autowired
 7     private BookMapper bookMapper;
 8 
 9     @Override
10     public Mapper<BookInfo> getMapper() {
11         return bookMapper;
12     }
13 }

 

Guess you like

Origin www.cnblogs.com/ssh-html/p/11407034.html