如何在spring boot中配置mybatis数据库连接?

时间:2016-11-02 05:49:47

标签: spring-boot ibatis spring-mybatis

我有myBatis xml config 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);
}

但是当我尝试构建priject时,我收到了错误

  

说明

     

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

     

动作:

     

如果您想要一个嵌入式数据库,请将支持的数据库放在   类路径。如果您要从a加载数据库设置   您可能需要激活它的特定配置文件(没有配置文件   目前有效。)

我可以设置SqlMapConfig.xml吗?

我尝试写入application.properties

mybatis.config-location=

但我不知道哪条路写。 SqlMapConfig.xml放置在资源中

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

有依赖性时

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

spring boot将自动配置spring jdbc和mybatis 两者。它在spring jdbc中使用的唯一配置是spring.datasource.*,您会错过它们,然后得到错误(发生此错误因为自动配置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将自动成功配置。

相关问题