基于Maven的SpringBoot整合Mybatis+Mysql之疯狂吐槽(一)

       由于项目需要,尝试使用SpringBoot整合Mybatis使用,瞎折腾了两天,重要弄出了一个可以运行的demo,心中无数。。。飞过,趁着火气未消,特地写下以下注意事项,供众多新生代参考,避免像我一样深陷泥潭,浪费宝贵的时间使用在没有意义的配置文件上面,最后附上完整可运行代码。

一、千万不要使用eclipse、Myeclispe进行两者整合、万不要使用eclipse、Myeclispe进行两者整合、万不要使用eclipse、Myeclispe进行两者整合,重要的事一定要说三遍!!!

      不要跟我说什么技术、能力之类的鬼东西,使用myeclipse整合这项目,就两个字总结:窝火,使用myeclispe好几年了,从来没这么窝火过,折腾了一天了,项目就是跑不起来,见识了各种奇葩错误,后来下载IDEA之后,一会儿就搭好了可运行的整合项目,然后把代码、配置什么的往myeclipse一放,再见、还是报错,我都懒得解决了(也就说说而已)

二、非专业玩家不要使用最新版本的jar进行整合,容易出现常见的几个问题

      如果说很多框架版本更新只是让你飘飘欲仙,那么org.mybatis.spring.boot1.2.0相较于之前版本,那简直就是,只要998就让你爽到不能呼吸了,下面随便放几张错误进行说明:
1、当你什么都配置完之后,一运行,下面这个错误就会如期而至,不要以为这是因为你没配置驱动url之类的,想太多了,压根没有一毛钱关系。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-01 16:32:14.549 ERROR 10212 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

如果你拿到网上去百度,有一堆人会说,需要在启动类的@EnableAutoConfiguration或@SpringBootApplication中添加exclude= {DataSourceAutoConfiguration.class},还别说,如果你没加这句话,你就是压根不能往下运行了。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class MavendemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MavendemoApplication.class, args);

    }

}

2、加了之后,上面代码一运行,看起来风平浪静,一切那么的和谐,启动没报错,请求一发,没反应,结果出不来,后来一百度,呵呵,MapperScan注解没有配置(dao接口已经加了mapper配置的前提下),加载mapper问题什么的,结果就是访问不了数据库,之后也只有在配置加上了,一配置,新bug闪亮登场

基于Maven的SpringBoot整合Mybatis+Mysql之疯狂吐槽(一)_第1张图片

你没看错,java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required,这个错误准时就会出现了,接下来肯定就是一顿百度啊,然后网上就会出现一堆解决方案,发生的原因就是标题写的,新版本出现的问题,网上说缺各种jar包啊什么的,跳过就对了,压根不是缺jar包的问题,根本原因是mybatis-spring-1.2.0.jar,这个版本及以上的版本中对SqlSessionDaoSupport类中的'sqlSessionFactory'或'sqlSessionTemplate'注入方式进行了调整,解决办法主要有三个,一个就是如下这种:


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
 
 
public abstract class AbstractDao extends SqlSessionDaoSupport{
 
	/**
	 * Autowired 必须要有
	 */
	@Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
    	
        super.setSqlSessionFactory(sqlSessionFactory);
    }
}

不足的地方很明显,如果你的基类是纯接口的,这个你就不能用了。另一种本质上也是通过注入来解决问题,如果你的dao是纯接口写的,那么,据我本人操作,最好的拯救方法就是把版本降低,这才是最靠谱的解决方案,其它什么的都是浮云啊,然后我就这样做的,然后配置一改,版本就变成如下图所示了

基于Maven的SpringBoot整合Mybatis+Mysql之疯狂吐槽(一)_第2张图片

以为我改为之后就没bug了吗,young 年轻,没有bug怎么能显示我的窝火,代码一运行,bug再次降临

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

2019-03-01 16:57:16.567  INFO 13012 --- [           main] com.ljb.mavendemo.MavendemoApplication   : Starting MavendemoApplication on LiJianBin with PID 13012 (D:\Myeclipse2014workpace\springbootdemo\target\classes started by lijianbin in D:\Myeclipse2014workpace\springbootdemo)
2019-03-01 16:57:16.570  INFO 13012 --- [           main] com.ljb.mavendemo.MavendemoApplication   : No active profile set, falling back to default profiles: default
2019-03-01 16:57:16.642  INFO 13012 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@28c4711c: startup date [Fri Mar 01 16:57:16 CST 2019]; root of context hierarchy
2019-03-01 16:57:18.579  INFO 13012 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-03-01 16:57:18.597  INFO 13012 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-01 16:57:18.598  INFO 13012 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2019-03-01 16:57:18.842  INFO 13012 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-01 16:57:18.843  INFO 13012 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2203 ms
2019-03-01 16:57:19.093  INFO 13012 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-01 16:57:19.097  INFO 13012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-03-01 16:57:19.098  INFO 13012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-03-01 16:57:19.098  INFO 13012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-03-01 16:57:19.098  INFO 13012 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-03-01 16:57:19.129  WARN 13012 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2019-03-01 16:57:19.130  INFO 13012 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-03-01 16:57:19.159  INFO 13012 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-03-01 16:57:19.277 ERROR 13012 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
	- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
	- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans


Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

一看一堆这个那个什么什么的bug,什么'javax.sql.DataSource' that could not be found,一大堆,一看,反正我是不想处理了,然后我就下载了Intellij IEDA,然后把java和配置代码往上面一放,一跑,o了 我擦 没有做任何配置更改什么的,它就这样运行通过了,浏览器输入地址一访问,数据获取正常,一起没问题,然后,为了myeclipse最后的一点尊严,我又重新建了一个项目,把代码、配置拿回去一放、一跑,再见,不行的还是不行,不要抱期望了,至此,容我恢复血量,再在myeclipse上战个痛。

完整项目可运行代码请看之后一篇文章:

基于Maven的SpringBoot整合Mybatis+Mysql之IDEA代码分享(二)

 

你可能感兴趣的:(Maven,SpringBoot,MYSQL,Mybatis)