【问题标题】:How can I configure mybatis db connection in spring boot?如何在spring boot中配置mybatis db连接?
【发布时间】:2017-03-15 08:54:19
【问题描述】:

我有 myBatis xml 配置 SqlMapConfig.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>

    <!--<typeAliases>-->
        <!--<typeAlias alias = "class_alias_Name" type = "absolute_clas_Name"/>-->
    <!--</typeAliases>-->

    <environments default = "development">
        <environment id = "development">
            <transactionManager type = "JDBC"/>

            <dataSource type = "POOLED">
                <property name = "driver" value = "oracle.jdbc.driver.OracleDriver"/>
                <property name = "url" value = "jdbc:oracle:thin:@my_ip:port/dbname"/>
                <property name = "username" value = "username"/>
                <property name = "password" value = "password"/>
            </dataSource>

        </environment>
    </environments>

    <!--<mappers>-->
        <!--<mapper resource = "path of the configuration XML file"/>-->
    <!--</mappers>-->

</configuration>

我有依赖

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

我有仓库

@Mapper
public interface ImsiByMsisdnModelRepository {

    final String query = "....";

    @Select(query )
    @Results(value = {
            @Result(property = "msisdn", column = "MSISDN"),
            @Result(property = "terminalid", column = "TERMINALID"),
            @Result(property = "startDate", column = "START_DATE"),
            @Result(property = "endDate", column = "END_DATE"),
    })
    List<ImsiByMsisdnModel> getAll(@Param("msisdn") String msisdn);
}

但是当我尝试构建项目时,我得到了错误

说明:

无法确定数据库类型 NONE 的嵌入式数据库驱动程序类

行动:

如果您想要一个嵌入式数据库,请在 类路径。如果您有要从 您可能需要激活它的特定配置文件(没有配置文件是 目前处于活动状态)。

我可以设置SqlMapConfig.xml 吗?

我尝试写在application.properties

mybatis.config-location=

但是我不知道是哪个路径写的。 SqlMapConfig.xml放在资源里

【问题讨论】:

  • 您的其他 spring-boot 依赖项是什么? Alo,请向我们展示您的完整 application.properties
  • 我也有同样的问题,你知道如何添加主机吗?我有你相同的 SqlMapConfig.xml,在我的 application.properties 我有: spring.datasource.schema=import.sql #mybatis.configuration=mybatis-config.xml mybatis.config-location=mybatis-config.xml logging.level .root=WARN logging.level.sample.mybatis.mapper=TRACE

标签: spring-boot ibatis spring-mybatis


【解决方案1】:

当你有依赖时

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

spring boot 会自动配置 spring jdbc 和 mybatis both。它在 spring jdbc 中使用的唯一配置是spring.datasource.*,你错过它们,然后你得到的错误(这个错误发生是因为 auto配置spring jdbc失败)。

那么你应该怎么做? 添加spring.datasource.*,这样spring jdbc就可以自动配置成功了。

spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://host/table_name
spring.datasource.username=user
spring.datasource.password=password

那么你的mybatis就自动配置成功了。

【讨论】:

    【解决方案2】:

    我发现您所要做的就是将以下内容添加到您的 application.properties 文件中:

    spring.database.driverClassName=org.postgresql.Driver
    spring.datasource.url=jdbc:postgresql://host/table_name
    spring.datasource.username=user
    spring.datasource.password=password
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-24
      • 2016-06-07
      • 2019-04-13
      • 2019-05-13
      • 2019-01-06
      • 2014-11-16
      • 2018-10-29
      • 2020-05-10
      相关资源
      最近更新 更多