zoukankan      html  css  js  c++  java
  • 零基础IDEA整合SpringBoot + Mybatis项目,及常见问题详细解答

    开发环境介绍: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>
    View Code

     和完整的最简的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
    View Code

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

    Tips:

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

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

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

  • 相关阅读:
    自增自减
    字符串处理函数
    指针总结指向const的指针、const指针、指向const指针的const指针
    Jenkins基础篇 系列之-—04 节点配置
    soapUI系列之—-07 调用JIRA Rest API接口【例】
    测试人员转正考核方向
    linux系列之-—03 常见问题
    Jenkins基础篇 系列之-—03 配置用户和权限控制
    linux系列之-—01 shell编程笔记
    Jenkins基础篇 系列之-—02 修改Jenkins用户的密码
  • 原文地址:https://www.cnblogs.com/zz-3m23d-begining/p/10761888.html
Copyright © 2011-2022 走看看