`
dinguangx
  • 浏览: 240406 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring的jdbc:initialize-database标签使用

 
阅读更多

在使用spring的jdbc:initialize-database标签进行单元测试时,发现这个标签与dataSource一同使用时,SQL脚本没有被正确执行,测试过程如下:

applicationContext-jdbc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
	default-lazy-init="true">

	<!-- Jdbc单数据源事务 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven proxy-target-class="true" />

	<context:property-placeholder
		ignore-resource-not-found="true"
		location="classpath*:/application.properties,
          			  classpath*:/application.test.properties" />

	<!-- DBCP连接池-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="defaultAutoCommit" value="false" />
	</bean>
	 
	<jdbc:initialize-database data-source="dataSource"
			ignore-failures="ALL">
			<jdbc:script location="classpath:sql/h2/schema.sql" />
			<jdbc:script location="classpath:sql/h2/import-data.sql"
				encoding="UTF-8" />
		</jdbc:initialize-database>
	<!-- 
	<jdbc:embedded-database id="dataSource" type="H2">
		<jdbc:script location="classpath:sql/h2/schema.sql" />
		<jdbc:script location="classpath:sql/h2/import-data.sql"
			encoding="UTF-8" />
	</jdbc:embedded-database>
	 -->
</beans>

 java测试代码为:

package com.shagri.showcase.data;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

@ContextConfiguration("classpath:jdbc/applicationContext-jdbc.xml")
public class DataInitTest extends AbstractTransactionalJUnit4SpringContextTests {
	@Test
	public void testData(){
		Assert.assertEquals(6,countRowsInTable("ss_user"));
	}
}

     这里的import-data.sql和schema.sql是从springside4里面取出来的两个文件,在import-data.sql对ss_user表有6个insert语句。所以这里单元测试的期望是表中数据条数为6,但是遗憾的是,这里的测试失败,实际得到的数据条数为0。

     但是如果将xml文件中的dataSource和jdbc:initialize-database注释掉,而启用jdbc:embedded-database标记时,单元测试结果正确 。

     想与各位探讨一下这两种使用方式的不同之处,以及为什么会出现这样的问题。

分享到:
评论

相关推荐

    Android代码-RetrofitUrlManager

    // When building OkHttpClient, the OkHttpClient.Builder() is passed to the with() method to initialize the configuration OkHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder...

    perl-date-holidays:Date :: Holidays-满足您所有假期需求的国定假日适配器聚合器

    日期::节假日 名称 Date :: Holidays-Date :: Holidays :: *满足您所有假期需求的适配器和聚合器 版 该文档介绍了Date :: Holidays...# Initialize a national holidays using the ISO 3361 country code my $dh = D

    dool:d-band工具和开发工具

    init Initialize project build Build project server Serve project Options: --version, -v Show version number --help, -h Show help $ dool build -h Usage: dool build [options] Options:

    程序员为什么还要刷题-orms-mapping-classes-to-database-tables:orms-mapping-classes

    程序员常刷题ORM:将 ...initialize ( name , album ) @name = name @album = album end end 这里我们有一个用于name和album的attr_accessor 。 为了将这个Song类“映射”到一个歌曲数据库表,我们需要

    mysql的安装压缩包

    第一步:将下载的压缩文件...第四步:mysqld --initialize --console 记住密码 第五步:net start mysql 启动服务 第六步:mysql -u root -p 第七步:alter user root@localhost identified by "root" 修改密码 配置环境变量

    spring-boot-reference.pdf

    Auto-configured JDBC Tests 43.3.13. Auto-configured jOOQ Tests 43.3.14. Auto-configured Data MongoDB Tests 43.3.15. Auto-configured Data Neo4j Tests 43.3.16. Auto-configured Data Redis Tests 43.3.17....

    mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz

    以管理员权限打开cmd,然后进入E:\study\mysql-5.7.15-winx64\bin文件夹输入以下命令:mysqld --initialize (用于初始化mysql,MySQL会帮你自动进行初始化,例如生成data文件夹) 再输入:mysqld install(安装...

    MySQL安装教程详细

    ):mysqld --initialize --console 6. 进入安装目录:黑窗口启动服务: net start mysql 7. 登陆旧密码登陆(第3步中的密9.修改密码 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '...

    Initialize-PSGallery:用于确保可以使用PowerShell库

    Initialize-PS图库 该功能将解决阻止PowerShell画廊访问的问题。 •• • 此功能将解决阻止使用PowerShell Gallery的常见问题。 安装 您可以在与PowerShell画廊进行交互之前简单地调用此脚本。 Invoke-...

    rpminstall_mysql.sh

    使用方法:1、上传至Centos服务器; 2、chmod赋予运行权限; 3、./rpminstall_msyql.sh运行自动安装mysql57 4、数据库初始化: 为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 ...

    mysql-5.7.20精简版

    Windows下mysql5.7.20精简版,体积小,可以本地测试用。 命令行安装服务: mysqld -install mysqld --initialize --initialize-insecure net start mysql

    Initialize-the-array.zip_array.initialize

    初始化数组,对于在VC++编译环境下,该代码是非常有帮助的,可以让读者快速学习数组的应用。

    initialize-engine:代码生成引擎

    安装$ npm install initialize-engine用法const initializeEngine = require ( 'initialize-engine' )const mkdirp = require ( 'mkdirp' )const path = require ( 'path' )initializeEngine ( { pre : [ createDir ...

    esotericpig:我的个人回购

    :necktie: 可供租用。 :open_mailbox_with_raised_... 正确执行initialize_copy :hot_beverage: -- :check_mark: Java utils( Robot包装器) :diamond_suit: -- :check_mark: 为YAML添加样式 :diamond_suit:

    bigint:JavaScript 的大整数对象 - 按位

    调试助手,bc 脚本:intex.bc _px32_init(): initialize. should be run first. it's already done. Functions: Note: argument (buf) below is index number of buffer: 1: buffer-d, 2: buffer-b, 3: buffer-e, 4...

    mysql5.7.19安装包64位rpm格式

    而使用 --initialize-insecure 命令则不使用安全模式,则不会为 root 用户生成一个密码。 这里演示使用的 --initialize 初始化的,会生成一个 root 账户密码,密码在log文件里,红色区域的就是自动生成的密码 复制...

    sheets-database:图书馆,以帮助使用Google表格作为数据库

    用于帮助将Google表格用作数据库(或CMS)的库 特征 简单直观的API 支持数据库中所需的大多数简单操作 多个身份验证选项-服务帐户,OAuth,访问令牌和API密钥 ...// Initialize the Database with doc ID (lo

    dh-400jdbc:AS400 JDBC模块

    JDBC包装器,用于JT400驱动程序使用JDBC连接到AS400。 用法 需要模块: const jdbc = require('dh-400jdbc'); 初始化连接: // build the config. let config = { host: 'String', libraries: , username: ,...

    数据库mysql8.021.rar

    win64位,8.0版本,免安装版,解压后cmd命令行:mysqld --initialize-insecure --user=mysql,mysqld --install,安装完毕即可启动

    mysql8.0 X64 安装文件和手动安装脚本

    D:\mysql-8.0.11-winx64\bin&gt;mysqld --initialize-insecure --console D:\mysql-8.0.11-winx64\bin&gt;mysqld --install mysql811 D:\mysql-8.0.11-winx64\bin&gt;net start mysql811 D:\mysql-8.0.11-winx64\bin&gt;mysql -u...

Global site tag (gtag.js) - Google Analytics