开发环境介绍:IDEA + maven + springboot2.1.4

1、用IDEA搭建SpringBoot项目:File - New - Project - Spring Initializr,(在选引用功能界面时,什么都不选)再一直Next即可,最后生成的项目结构如下:(首先记得在File - Settings - 搜索maven,将maven路径改成你本地配置的)

然后我们在SpringBoot启动文件Sb001Application下,启动项目,出现 Started Sb001Application in 0.602 seconds (JVM running for 1.117) 说明项目启动成功!

当然,现在我们项目几乎是什么功能都没有的,我们和springMVC一样,搭建service、serviceImpl、dao、xml文件等,也是可以正常启动的,

但是:当你尝试在比如在serviceImpl 用@Autowired 注入dao层接口时,就会报错:

Field accountDao in com.example.demo.service.impl.AccountServiceImpl required a bean of type 'com.example.demo.dao.AccountDao' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

正常情况下,这时你的dao层都是接口interface,项目启动时,它是没有被实现的,被注入到别的bean时,自然就会报上面的错,如果不在别的类里面用@Autowired 注入dao层有关接口时,启动也不会报错!

现在我们通过整合mybatis来实现dao层的接口,来实现数据库的连接:

第一步:引入jia包

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>

Tips:如果尝试在引入mybatis后,不做任何配置,也就是不在yml文件配置数据库连接信息,直接启动项目,发现会报错:

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

Reason: Failed to determine a suitable driver class

后来通过百度终于找到答案:spring boot启动时默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类,DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。

解决办法:

(1)将@SpringBootApplication注解改成@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}),这种办法可以解决上面的报错,但是会跟我们想配置mybatis的原意走远,不推荐【但是,提一句,在配置多数据源时,也是这样配置的】

(2)第二种办法就是配置数据库了,这里首先将系统resources下默认生成的properties文件删掉,新建application.yml文件(别问我为什么要用yml,因为的确好用^_^),并且在里面加入数据源的配置

spring:
datasource:
url: jdbc:mysql://172.0.0.0:3306/你的数据库名称?serverTimezone=GMT%2B8
username: 账号
password: 密码
#driver-class-name: com.mysql.cj.jdbc.Driver #根据mysql版本选择,一般都是可以的
driver-class-name: com.mysql.jdbc.Driver

配置了数据源,接下来就是连接数据库了,那么就要引入连接数据库的Jar包:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

配置到这个时候,项目是可以正常启动的,但是当你再次尝试在serviceImpl通过@Autowired 注入dao层的接口时,它又会报bean of type 'com.example.demo.dao.AccountDao' that could not be found.同样的错,原因上面也解释过,接口无法实现,,,

那么如何配置让mybatis实现我们dao层的接口呢?

那就是在启动文件新加 @MapperScan("com.example.demo.dao"),里面是项目dao包的路径,一定不能错!!!,也可以@MapperScan("com.example.demo.*.dao")这种;现在我们在serviceImpl通过@Autowired 注入dao层的接口时,项目启动就正常了!

现在我们去test目录下,创建一个测试类,写一个最简单的方法getById,当然mybatis是需要在xml里面写自己sql的,xml的存放路径我们不会用系统默认的,现在我们指定放在resources下的mybatis包下,新建完mybatis包后,还要在yml文件配置

mybatis:
mapper-locations: classpath:mybatis/**/*.xml

到这里,测试类就可以正常运行,可以连接数据库读取数据了!自此,最简单的配置spring boot + mybatis 就整合完成了!最后我们对照开始的图对比一下

最后,附上项目完整的pom文件配置(是最简版的,能保证连接数据库):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sb001</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 上面两个是 零配置 springboot 都会自带生成的 --> <!-- 整合mybatis的最简配置,web都是可以不用的 -->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>--> <!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency> <!-- 连接Mysql数据库,别的数据库的要对应改 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

和完整的最简的yml文件配置:

spring:
datasource:
url: jdbc:mysql://127.0.0.0:3306/testdb?serverTimezone=GMT%2B8
username: admin
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver mybatis:
mapper-locations: classpath:mybatis/**/*.xml

虽然用SpringBoot项目很长时间了,当同事“乞丐式”配置一个最简项目时,遇到各种报错,解决的过程中还是可以学到很多点的,特意记录下来,希望可以帮到一些新手,特别是里面两个错误的主要原因,估计很多人都不一定清楚,如果笔者理解有不正确的地方,也请指正!

Tips:

(1)会的不难,不会就难

(2)学就会,不学就不会

笔于2019-04-24,月底将结束毕业后工作长达两年的第一家公司WW,特此记录

零基础IDEA整合SpringBoot + Mybatis项目,及常见问题详细解答的更多相关文章

  1. 零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)

    作者各必备工具的版本如下: Tomcat:apache-tomcat-7.0.63 (下载链接) Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载 ...

  2. SpringBoot Mybatis项目中的多数据源支持

    1.概述 有时项目里里需要抽取不同系统中的数据源,需要访问不同的数据库,本文介绍在Springboot+Mybatis项目中如何支持多数据源操作. 有需要的同学可以下载 示例代码 项目结构如下: 2. ...

  3. 基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

    SpringBoot 整合 Mybatis 有两种常用的方式,一种就是我们常见的 xml 的方式 ,还有一种是全注解的方式.我觉得这两者没有谁比谁好,在 SQL 语句不太长的情况下,我觉得全注解的方式 ...

  4. 使用RESTful风格整合springboot+mybatis

    说明: 本文是springboot和mybatis的整合,Controller层使用的是RESTful风格,数据连接池使用的是c3p0,通过postman进行测试 项目结构如下: 1.引入pom.xm ...

  5. idea从零搭建简单的springboot+Mybatis

    需用到的sql /* Navicat MySQL Data Transfer Source Server : localhost root Source Server Version : 80012 ...

  6. springboot+mybatis项目自动生成

    springboot_data_access_demo基于rapid,根据自定义模版生成的基于mybatis+mysql的数据库访问示例项目.简单配置数据库信息,配置不同的生成策略生成可以直接运行访问 ...

  7. 零基础学习java------36---------xml,MyBatis,入门程序,CURD练习(#{}和${}区别,模糊查询,添加本地约束文件) 全局配置文件中常用属性 动态Sql(掌握)

    一. xml  1. 文档的声明 2. 文档的约束,规定了当前文件中有的标签(属性),并且规定了标签层级关系 其叫html文档而言,语法要求更严格,标签成对出现(不是的话会报错) 3. 作用:数据格式 ...

  8. springboot mybatis 项目框架源码 shiro 集成代码生成器 ehcache缓存

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  9. SpringBoot+MyBatis项目Dao层最简单写法

    前言 DAO(Data Access Object) 是数据访问层,说白了就是跟数据库打交道的,而数据库都有哪几种操作呢?没错,就是增删改查.这就意味着Dao层要提供增删改查操作. 不知道大家是怎么写 ...

随机推荐

  1. dao层、service和action的运用和区别

    DAO层叫数据访问层,全称为data access object,属于一种比较底层,比较基础的操作,对于数据库的操作,具体到对于某个表的增删改查, 也就是说某个DAO一定是和数据库的某一张表一一对应的 ...

  2. CentOS设置服务开机启动的两种方法

    一.通过服务的方式设置自启动 1.  在/etc/init.d 下建立相关程序的启动脚本 2.  chkconfig --add mysqld(添加服务到chkconfig列表中) chkconfig ...

  3. 用JS更好的实现响应式布局

    响应式布局更加高效的方法: 代码实现 <script> $(function() { (function(){ var $html = $('html'); var $window = $ ...

  4. Date中before和after方法的使用

    Date1.after(Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false: Date1.before(Date2),当Date1小于Date2时,返回TRUE,当 ...

  5. 【笔记】Rancher2.1容器云平台新特性

    2018年10月6日,Rancher2.1版本正式发布.相比Rancher2.0版本,提供了许多新的特性: 1.支持集群和项目级别的硬件资源配额管理:2.支持3个节点的Rancher Server的管 ...

  6. PyQt5——高级控件

    PyQt5高级控件使用方法详见:https://blog.csdn.net/jia666666/article/list/4?t=1& PyQt5高级控件汇总: 1.QTableView 2. ...

  7. LoRa无线技术介绍

    什么是LoRa LoRa是semtech公司创建的低功耗局域网无线标准,低功耗一般很难覆盖远距离,远距离一般功耗高,要想马儿不吃草还要跑得远,好像难以办到.LoRa的名字就是远距离无线电(Long R ...

  8. selenium、UA池、ip池、scrapy-redis的综合应用案例

    案例: 网易新闻的爬取: https://news.163.com/ 爬取的内容为一下4大板块中的新闻内容 爬取: 特点: 动态加载数据  ,用 selenium 爬虫 1. 创建项目 scrapy ...

  9. php无限极分类的实现

    //指定根层级的树状图 function generateTree($list, $root = 0, $pk = 'id', $pid = 'pid', $child = '_child') { $ ...

  10. HDOJ-2039

    #include<iostream> #include<cstdio> using namespace std; int main(){ int m,flag; float x ...