在springboot中,利用xml配置mybatis的方法

前置步骤参考博客:SpringBoot配置Mybatis的两种方式(通过XML配置和通过YML配置文件配置)_Darren i的博客-CSDN博客

但按照博客中的关键代码和步骤创建的项目无法启动

启动报错:

Description:
Failed to configure a DataSource: 'url’attribute is not specif:
Reason: Failed to determine a suitable driver class

因此经过参考其他资料,尝试出了不会报错的步骤

1. 创建springboot工程

2. 在resource目录下,创建mybatis的配置文件mybatis-config.xml和映射文件目录mapper,之后的各类映射文件都放在里面

  mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- #开启mybatis驼峰式命名规则自动转换 -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
        <typeAlias alias="Blog" type="com.example.mybatisstudy.pojo.Blog"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--                数据库连接信息-->
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///homework1027?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

</configuration>

3. 在application.yml文件中配置如下信息

mybatis:
  #标注mybatis配置文件的位置
  config-location: classpath:mybatis-config.xml
  #标注待解析的mapper的xml文件位置
  mapper-locations: classpath:mapper/*.xml
  #标注实体类位置
  type-aliases-package: com.example.mybatisstudy.pojo

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///homework1027?userSSL=false
    username: root
    password: 123456

注意,application.yml和mybatis-config.xml中都要配置数据库(驱动、url等)

这个是避免报错的措施

4. 最后测试一下

        创建代码结构:

        

        编写测试类,运行:

        

        成功查出需要的数据库内容。

 

 


版权声明:本文为qq_53336526原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。