如何使用Spring Boot + Mybatis连接池

时间:2018-04-14 15:33:54

标签: java spring spring-boot mybatis

我正在学习Spring Boot,我创建了一个简单的应用程序。

我使用以下maven依赖项:

  • 弹簧引导起动父:2.0.1.RELEASE
  • MyBatis的弹簧引导起动:1.3.2

我想使用连接池来提高应用程序的速度,但我不知道如何为此用例配置Spring Boot和MyBatis。

在此之前,我使用了Java EE +应用程序服务器,并通过mybatis.xml文件配置mybatis以使用JNDI数据源。连接池由应用程序服务器提供。

但是现在,我对于放置数据库连接参数的位置有点困惑,因为有两种可能的候选者:application.propertiesmybatis.xml

此环境中这两个配置文件之间的连接是什么?

我知道Spring Boot应用程序在Tomcat Web容器上运行,因此我无法在其中创建JNDI数据源和连接池。

使用Spring Boot with Mybatis时,指定数据库连接+连接池的正确方法是什么?

目前我还将连接参数添加到mybatis.xml和application.xml文件中:

application.properties

spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driver-class-name=...

mybatis.xml

<configuration>
    <environments default="jdbc">
        <environment id="jdbc">
            <transactionManager type="JDBC" />
            <datasource type="POOLED">
                <property name="driver" value="..." />
                <property name="url" value="..." />
                <property name="username" value="..." />
                <property name="password" value="..." />
        </environment>
    <environments>
</configuration>

1 个答案:

答案 0 :(得分:1)

如果您使用的是library(data.table) #Add a column to represent row number in current table. This will be used #later to sort data.table to find correct `lag` dt[,rn:=.I] #Use melt to transfer values in long format. value1 contains "items" and #value2 contains "values" dt<-melt(dt, id=c("iteration","rn"), measure=list(1:2,c("value1","value2")), value.name = c("item","value")) #The order in original table is row number, item1 and item2. The same can be #achieved by sorting on "rn" and "variable" dt[order(rn,variable), lag := shift(value), by = item] dt[variable==1,][dt[variable==2,],.(item1 = item, item2 = i.item, value1 = value, value2=i.value, iteration, lag1 = lag, lag2 = i.lag), on=("rn")] # item1 item2 value1 value2 iteration lag1 lag2 # 1: d i 0.4464375 6.491179 1 NA NA # 2: b j 6.5148245 5.665638 1 NA NA # 3: c f 3.9031889 2.751919 1 NA NA # 4: a g 5.3450990 3.587738 1 NA NA # 5: e h 6.1257061 3.544912 1 NA NA # 6: d i 8.0236359 1.331371 2 0.4464375 6.491179 # 7: b j 6.3180503 4.184624 2 6.5148245 5.665638 # 8: c f 7.2440561 5.053722 2 3.9031889 2.751919 # 9: a g 3.4307173 6.823257 2 5.3450990 3.587738 # 10: e h 4.1486154 8.268693 2 6.1257061 3.544912 # 11: j a 5.7859952 5.121371 3 4.1846241 3.430717 # 12: f c 5.0735143 8.695145 3 5.0537224 7.244056 # 13: i e 2.9358327 5.160250 3 1.3313712 4.148615 # 14: g d 2.4702771 7.837112 3 6.8232573 8.023636 # 15: h b 4.5460694 7.917232 3 8.2686930 6.318050 ,则无需使用mybatis-spring-boot-started来指定数据源参数。它不用于从spring发起的数据库访问。

为了添加连接池,您需要通过spring-boot向任何连接池supported添加依赖关系。最简单的方法是向mybatis.xml添加依赖项。如果你有它,连接已经合并。

相关问题