持续集成实践之数据库篇
上一篇 / 下一篇 2012-07-10 10:05:07 / 个人分类:杂谈
前不久在公司内部刊物上写的,借论坛和大家分享!51Testing软件测试网/]
Hf*p[
水平有限,欢迎拍砖指正!51Testing软件测试网W7w AQ`0ZL7t
bS0NSxJU0 持续集成实践之数据库篇51Testing软件测试网4UK*XO/X~
51Testing软件测试网YO H y+Ef!Y
互联网世界中虚虚实实让人难以分辨!技术圈子里虽说好一些,但也有不乏只为炒作不务实的”流行风”!但”持续集成”风的盛行绝不仅是因其提出者Martin Fowler大名和努力推广,更重要的是其思想的启发性和精准性!它使我们看到了软件开发的瓶颈何在,更言简意赅地告诉了解决之道,可谓字字珠玑!持续集成涵盖多方面,其中数据库的持续集成是不得不说的。所有的软件业务都是为了处理数据,软件开发过程中难免会对数据结构及其数据做修改。将数据库集成引入持续集成,业界给了一个专用名词:Continuous Database Integration通常被缩写为CDBI。此类集成通常是由开发人员负责的,期间若是遇到数据库方面难题则可以向专业DBA咨询。为了更清楚的阐述,,我们会以一个例子给大家说明。51Testing软件测试网#? d-X;RS'`
示例环境如下:51Testing软件测试网2|:U'b0ljB
实例工程: Appfuse example,使用appfuse命令创建标准的MVC工程(具体操作略)。51Testing软件测试网2l5uL#ep^ D_Ap+^O
数据库: Mysql 5.5.1051Testing软件测试网a*jb x\.E$su1m0d
JDK: 1.6.0_23" 51Testing软件测试网5? bK\ p X!?pBc
Maven: 3.0.4
ez:QENf0注:所有操作步骤都以maven、jdk、mysql,CI服务器等环境正常安装完成为前提。下面会分别介绍通过两种方案实现CDBI。
4FR$uCEb-AurD v0一、 使用dbunit完成CDBI51Testing软件测试网5Uu0QY#ta
我们使用的是Appfus原始生成的代码。由于Appfuse自动生成的代码中包含了使用dbunit完成数据库持续集成方案,首先让我们一起来学习这种解决方案:51Testing软件测试网i%wW1N9b6a6~4j
(一) 为了自动生成数据库结构,首先在Hibernate配置文件hibernate.cfg.xml定义所要创建数据库表:51Testing软件测试网5[T ByGp3X
<hibernate-configuration>51Testing软件测试网6|5SZKRF
<session-factory>
oIF{)LT0 <mapping class="com.mycompany.model.User"/>
\2B5a5F!S"E2l_8e#~2Ff0 <mapping class="com.mycompany.model.Role"/>51Testing软件测试网ay G&sx-E%k
</session-factory>51Testing软件测试网#Qz}C:l[j
</hibernate-configuration>51Testing软件测试网7W G,Dsb;n2a8x a$p
7|9Ym"F$w&~e0(二) 如何执行和什么时候执行都是在项目的pom中定义。我们使用hibernate maven插件到达:
3C:~MBp Dy5ag8Z0
]lL*R}KB1sR0 <plugin>
P7u+`?NV9Q*kF;E^y0 <groupId>org.codehaus.mojo</groupId>
3h1ic.^4t~_u0 <artifactId>hibernate3-maven-plugin</artifactId>51Testing软件测试网&nNdo+uB)Y[
<version>2.2</version>51Testing软件测试网iMU3U~ L(A&VI
<configuration>
;rW6V xnTY0 <components>
"XxcsNE$Ry0 <component>
,A&f1tfF0 <name>hbm2ddl</name>o
qiy5Q7Q `O0<implementation>annotationconfiguration</implementation>51Testing软件测试网n\,ctZ'c }M6C
<!--自动生成schema-->51Testing软件测试网7~E;T6SgN3K
</component>51Testing软件测试网_#vI GH-mrS
</components>51Testing软件测试网gp}nf/OP
<componentProperties>
!C y0Q6JNs0 <drop>true</drop>
jQ5|T:C0 <jdk5>true</jdk5>51Testing软件测试网 pr!dy X0?:Pw
<propertyfile>target/classes/jdbc.properties</propertyfile>
*j Q qBp!Uo3m8uN0<!—数据库连接-->51Testing软件测试网\a,Sc dD~qw
<skip>false</skip>
"wA}X-Kj b0 </componentProperties>
8w2X4lJ:R0 </configuration>51Testing软件测试网7|K2}U@J)]
<executions>51Testing软件测试网S#e(~;PSN#j$Q y(H
<execution>51Testing软件测试网.fW+S ?E4J$_*JKF;P
<phase>process-test-resources</phase>51Testing软件测试网/\2c6^HyQO&X
<goals>51Testing软件测试网l,oJ-ht,q
<goal>hbm2ddl</goal>
%Y b$m"n,uG!kb J]*`*O0 </goals>
%G7J }p@0io0 </execution>
v` `/}x/B0K0 </executions>51Testing软件测试网(I%U'ak,C'hX
<dependencies>51Testing软件测试网;Jk(QRnDV*FV,z
<!—jdbc连接所需要的jar依赖-->51Testing软件测试网,}Ts+io&|n
<dependency>51Testing软件测试网Xf8G3Lhq.~%mq
<groupId>mysql</groupId>
;v0CQ-ErS9k0 <artifactId>mysql-connector-java</artifactId>
!d'NW(jg{*V0 <version>5.1.14</version>
GzU[na9tEr0 </dependency>51Testing软件测试网Pu$VwF8g/F%e
</dependencies>51Testing软件测试网 Nj.aQ"D
</plugin>51Testing软件测试网$bz @'TL"M wb
对以上配置的解释:
oX X0uEhFrz0当maven运行到process-test-resources阶段是程序会自动生成数据库表结构。51Testing软件测试网wqTmcMP6_
hibernate3-maven-plugin另外功能的介绍:51Testing软件测试网Sdh6yopU s Pe
hibernate3-maven-plugin的功能非常强大,还可以自动把项目中的module转化成初始化表用的sql语句。只需如下配置(当然本次演示中不需要这个功能!):
w4]{1|b c$R051Testing软件测试网V)MT%t w_%R
<groupId>org.codehaus.mojo</groupId>51Testing软件测试网-n.vH.@OY#Ln
<artifactId>hibernate3-maven-plugin</artifactId>51Testing软件测试网.^h/bGQn0~ f
<version>2.2</version>
P ]pH,DI `N2D*WS0 <configuration>
H o#b"~)YO'htU0 <componentProperties>
"V!W(L?p?0 <outputfilename>schema.sql</outputfilename>
$K,g6fHM Fb0 </componentProperties>51Testing软件测试网 ?CJ"Qy dK ~`/y
</configuration>
6m~ K0mBG(a0 </plugin>
.] ?-\;[2uOq0c9B0(三) 有了表结构,我们还需要使用maven的Dbunit插件完成数据初始化,其连接配置:
%z4hU4W1Z&Z8p|]0Dbunit在这里主要是用来初始化数据51Testing软件测试网:Ns3N]Y%a!^@
<plugin>
/P8|+g9M9\4]2p_"D0 <groupId>org.codehaus.mojo</groupId>51Testing软件测试网8|C4pw@5gkzZ
<artifactId>dbunit-maven-plugin</artifactId>
4f5bPww{ ^5?0 <version>1.0-beta-3</version>
hz`+ko0 <configuration>
x%F+zA1DY t9Fr~0n0 <dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>
9G7?7ahzj0 <driver>com.mysql.jdbc.Driver</driver>51Testing软件测试网/}fU-{0`"SHa9Z#I
<username>root</username>51Testing软件测试网%c+e|r.fY
<password>password</password>
G7m"o8o/W9jA0 <url>jdbc:mysql://localhost/${db.name}?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true</url>51Testing软件测试网$N_ZTR0HkDJ
<src>src/test/resources/sample-data.xml</src>51Testing软件测试网l(W}An#k!x f%K
<type>CLEAN_INSERT</type>
{baR'm;OT0 <!-type表示对数据库的操作,这是dbunit约定的,可以看参考dbunit网站。这里是指删除数据后执行insert--->51Testing软件测试网x(Cjc'f|iM0{M
<transaction>true</transaction>
p{bV$N;O R0<!-transaction等于true表示在操作是在transaction中执行-->51Testing软件测试网!JU}Lf?9rE
解析:“<src>src/test/resources/sample-data.xml</src>”是我们是使用的Dataset的数据文件。其他都是常用的mysql jdbc连接属性,这里就不用解释了。51Testing软件测试网!J|%kx2T"rY
51Testing软件测试网L5{Gn5~Iyr
(四) 执行阶段的设定:
Br"k^:W3q} fM0 <execution>51Testing软件测试网B&kXvf:o#a
<id>test-compile</id>
0p1Q.u^(~7M3m0 <phase>test-compile</phase>
9QNzm#tg t{0 <goals>51Testing软件测试网I0R9t6b{ F8@M
<goal>operation</goal>51Testing软件测试网&Ij OK@!gO#NQ l
</goals>
a#nI"p0}pZ6bz9p0 </execution>51Testing软件测试网.vz`&hmfOY+S
<execution>
%c-ic m0x'{(X"M0 <id>test</id>51Testing软件测试网n^.~3FQEh;\
<phase>test</phase>
%CW*?P fR0 <goals>
1]k:]rYU0 <goal>operation</goal>
*[R,]%y#O5F*A0 </goals>
+D Q6|(d&sO/q0</execution>51Testing软件测试网4| x1xj[B+c
解析:以上配置也是maven插件常见的形势,不同生命周期执行不同的goal。<phase>test-compile</phase>和 <phase>test</phase>分别代表每个goals执行的阶段,这使插件运用相当灵活。能在那个阶段执行,是有插件开发者所编写的代码决定的。51Testing软件测试网P3wo^6PjA
sU7_3Nbgny0(五) Dbunit dataset xml文件格式介绍:
r(t2m[p!o0前文中已经讲到使用dbunit-maven-pluginge定义了src使用的数据为:src/test/resources/sample-data.xml进行初始化数据库:
wPjk%yqs}0 第一行为数据库表名:51Testing软件测试网GV ^0@N
<table name="app_user"> <!-此段为table name的定义-->
_4C4G.c$e0|s0 接下来是对表的字段的定义:
+XMYWP,f a0column代表字段名,row代表要初始化的数据。
w.A1M So9l AVb0 <column>id</column>51Testing软件测试网5jl\0~C4oh-|_m
<column>username</column>51Testing软件测试网2^[#Gk#u
<column>password</column>
+U)O"|N5Ay]0 。。。。。。。51Testing软件测试网w+]wTay
<column>credentials_expired</column>
IS jV1EdG&N|0 <row>51Testing软件测试网+N\W+P^m
<value description="id">-1</value>51Testing软件测试网BKS\T
<value description="username">user</value>
2^e8H!mm)_V H&j0 。。。。。。51Testing软件测试网^;E}iw%bo+B(M
<value description="credentials_expired">0</value>
4`(s2G)M,hQi0p0 </row>51Testing软件测试网N,t e W r:{X7y)i X
到此我们完成了数据库表结构的创建和初始数据的插入。这种方式是开发者比较喜欢的方式,或许他们是不需要自已写sql语句。这样只需确认对应关系,编写相应的xml文件即可!
水平有限,欢迎拍砖指正!51Testing软件测试网W7w AQ`0ZL7t
bS0NSxJU0 持续集成实践之数据库篇51Testing软件测试网4UK*XO/X~
51Testing软件测试网YO H y+Ef!Y
互联网世界中虚虚实实让人难以分辨!技术圈子里虽说好一些,但也有不乏只为炒作不务实的”流行风”!但”持续集成”风的盛行绝不仅是因其提出者Martin Fowler大名和努力推广,更重要的是其思想的启发性和精准性!它使我们看到了软件开发的瓶颈何在,更言简意赅地告诉了解决之道,可谓字字珠玑!持续集成涵盖多方面,其中数据库的持续集成是不得不说的。所有的软件业务都是为了处理数据,软件开发过程中难免会对数据结构及其数据做修改。将数据库集成引入持续集成,业界给了一个专用名词:Continuous Database Integration通常被缩写为CDBI。此类集成通常是由开发人员负责的,期间若是遇到数据库方面难题则可以向专业DBA咨询。为了更清楚的阐述,,我们会以一个例子给大家说明。51Testing软件测试网#? d-X;RS'`
示例环境如下:51Testing软件测试网2|:U'b0ljB
实例工程: Appfuse example,使用appfuse命令创建标准的MVC工程(具体操作略)。51Testing软件测试网2l5uL#ep^ D_Ap+^O
数据库: Mysql 5.5.1051Testing软件测试网a*jb x\.E$su1m0d
JDK: 1.6.0_23" 51Testing软件测试网5? bK\ p X!?pBc
Maven: 3.0.4
ez:QENf0注:所有操作步骤都以maven、jdk、mysql,CI服务器等环境正常安装完成为前提。下面会分别介绍通过两种方案实现CDBI。
4FR$uCEb-AurD v0一、 使用dbunit完成CDBI51Testing软件测试网5Uu0QY#ta
我们使用的是Appfus原始生成的代码。由于Appfuse自动生成的代码中包含了使用dbunit完成数据库持续集成方案,首先让我们一起来学习这种解决方案:51Testing软件测试网i%wW1N9b6a6~4j
(一) 为了自动生成数据库结构,首先在Hibernate配置文件hibernate.cfg.xml定义所要创建数据库表:51Testing软件测试网5[T ByGp3X
<hibernate-configuration>51Testing软件测试网6|5SZKRF
<session-factory>
oIF{)LT0 <mapping class="com.mycompany.model.User"/>
\2B5a5F!S"E2l_8e#~2Ff0 <mapping class="com.mycompany.model.Role"/>51Testing软件测试网ay G&sx-E%k
</session-factory>51Testing软件测试网#Qz}C:l[j
</hibernate-configuration>51Testing软件测试网7W G,Dsb;n2a8x a$p
7|9Ym"F$w&~e0(二) 如何执行和什么时候执行都是在项目的pom中定义。我们使用hibernate maven插件到达:
3C:~MBp Dy5ag8Z0
]lL*R}KB1sR0 <plugin>
P7u+`?NV9Q*kF;E^y0 <groupId>org.codehaus.mojo</groupId>
3h1ic.^4t~_u0 <artifactId>hibernate3-maven-plugin</artifactId>51Testing软件测试网&nNdo+uB)Y[
<version>2.2</version>51Testing软件测试网iMU3U~ L(A&VI
<configuration>
;rW6V xnTY0 <components>
"XxcsNE$Ry0 <component>
,A&f1tfF0 <name>hbm2ddl</name>o
qiy5Q7Q `O0<implementation>annotationconfiguration</implementation>51Testing软件测试网n\,ctZ'c }M6C
<!--自动生成schema-->51Testing软件测试网7~E;T6SgN3K
</component>51Testing软件测试网_#vI GH-mrS
</components>51Testing软件测试网gp}nf/OP
<componentProperties>
!C y0Q6JNs0 <drop>true</drop>
jQ5|T:C0 <jdk5>true</jdk5>51Testing软件测试网 pr!dy X0?:Pw
<propertyfile>target/classes/jdbc.properties</propertyfile>
*j Q qBp!Uo3m8uN0<!—数据库连接-->51Testing软件测试网\a,Sc dD~qw
<skip>false</skip>
"wA}X-Kj b0 </componentProperties>
8w2X4lJ:R0 </configuration>51Testing软件测试网7|K2}U@J)]
<executions>51Testing软件测试网S#e(~;PSN#j$Q y(H
<execution>51Testing软件测试网.fW+S ?E4J$_*JKF;P
<phase>process-test-resources</phase>51Testing软件测试网/\2c6^HyQO&X
<goals>51Testing软件测试网l,oJ-ht,q
<goal>hbm2ddl</goal>
%Y b$m"n,uG!kb J]*`*O0 </goals>
%G7J }p@0io0 </execution>
v` `/}x/B0K0 </executions>51Testing软件测试网(I%U'ak,C'hX
<dependencies>51Testing软件测试网;Jk(QRnDV*FV,z
<!—jdbc连接所需要的jar依赖-->51Testing软件测试网,}Ts+io&|n
<dependency>51Testing软件测试网Xf8G3Lhq.~%mq
<groupId>mysql</groupId>
;v0CQ-ErS9k0 <artifactId>mysql-connector-java</artifactId>
!d'NW(jg{*V0 <version>5.1.14</version>
GzU[na9tEr0 </dependency>51Testing软件测试网Pu$VwF8g/F%e
</dependencies>51Testing软件测试网 Nj.aQ"D
</plugin>51Testing软件测试网$bz @'TL"M wb
对以上配置的解释:
oX X0uEhFrz0当maven运行到process-test-resources阶段是程序会自动生成数据库表结构。51Testing软件测试网wqTmcMP6_
hibernate3-maven-plugin另外功能的介绍:51Testing软件测试网Sdh6yopU s Pe
hibernate3-maven-plugin的功能非常强大,还可以自动把项目中的module转化成初始化表用的sql语句。只需如下配置(当然本次演示中不需要这个功能!):
w4]{1|b c$R051Testing软件测试网V)MT%t w_%R
<groupId>org.codehaus.mojo</groupId>51Testing软件测试网-n.vH.@OY#Ln
<artifactId>hibernate3-maven-plugin</artifactId>51Testing软件测试网.^h/bGQn0~ f
<version>2.2</version>
P ]pH,DI `N2D*WS0 <configuration>
H o#b"~)YO'htU0 <componentProperties>
"V!W(L?p?0 <outputfilename>schema.sql</outputfilename>
$K,g6fHM Fb0 </componentProperties>51Testing软件测试网 ?CJ"Qy dK ~`/y
</configuration>
6m~ K0mBG(a0 </plugin>
.] ?-\;[2uOq0c9B0(三) 有了表结构,我们还需要使用maven的Dbunit插件完成数据初始化,其连接配置:
%z4hU4W1Z&Z8p|]0Dbunit在这里主要是用来初始化数据51Testing软件测试网:Ns3N]Y%a!^@
<plugin>
/P8|+g9M9\4]2p_"D0 <groupId>org.codehaus.mojo</groupId>51Testing软件测试网8|C4pw@5gkzZ
<artifactId>dbunit-maven-plugin</artifactId>
4f5bPww{ ^5?0 <version>1.0-beta-3</version>
hz`+ko0 <configuration>
x%F+zA1DY t9Fr~0n0 <dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>
9G7?7ahzj0 <driver>com.mysql.jdbc.Driver</driver>51Testing软件测试网/}fU-{0`"SHa9Z#I
<username>root</username>51Testing软件测试网%c+e|r.fY
<password>password</password>
G7m"o8o/W9jA0 <url>jdbc:mysql://localhost/${db.name}?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true</url>51Testing软件测试网$N_ZTR0HkDJ
<src>src/test/resources/sample-data.xml</src>51Testing软件测试网l(W}An#k!x f%K
<type>CLEAN_INSERT</type>
{baR'm;OT0 <!-type表示对数据库的操作,这是dbunit约定的,可以看参考dbunit网站。这里是指删除数据后执行insert--->51Testing软件测试网x(Cjc'f|iM0{M
<transaction>true</transaction>
p{bV$N;O R0<!-transaction等于true表示在操作是在transaction中执行-->51Testing软件测试网!JU}Lf?9rE
解析:“<src>src/test/resources/sample-data.xml</src>”是我们是使用的Dataset的数据文件。其他都是常用的mysql jdbc连接属性,这里就不用解释了。51Testing软件测试网!J|%kx2T"rY
51Testing软件测试网L5{Gn5~Iyr
(四) 执行阶段的设定:
Br"k^:W3q} fM0 <execution>51Testing软件测试网B&kXvf:o#a
<id>test-compile</id>
0p1Q.u^(~7M3m0 <phase>test-compile</phase>
9QNzm#tg t{0 <goals>51Testing软件测试网I0R9t6b{ F8@M
<goal>operation</goal>51Testing软件测试网&Ij OK@!gO#NQ l
</goals>
a#nI"p0}pZ6bz9p0 </execution>51Testing软件测试网.vz`&hmfOY+S
<execution>
%c-ic m0x'{(X"M0 <id>test</id>51Testing软件测试网n^.~3FQEh;\
<phase>test</phase>
%CW*?P fR0 <goals>
1]k:]rYU0 <goal>operation</goal>
*[R,]%y#O5F*A0 </goals>
+D Q6|(d&sO/q0</execution>51Testing软件测试网4| x1xj[B+c
解析:以上配置也是maven插件常见的形势,不同生命周期执行不同的goal。<phase>test-compile</phase>和 <phase>test</phase>分别代表每个goals执行的阶段,这使插件运用相当灵活。能在那个阶段执行,是有插件开发者所编写的代码决定的。51Testing软件测试网P3wo^6PjA
sU7_3Nbgny0(五) Dbunit dataset xml文件格式介绍:
r(t2m[p!o0前文中已经讲到使用dbunit-maven-pluginge定义了src使用的数据为:src/test/resources/sample-data.xml进行初始化数据库:
wPjk%yqs}0 第一行为数据库表名:51Testing软件测试网GV ^0@N
<table name="app_user"> <!-此段为table name的定义-->
_4C4G.c$e0|s0 接下来是对表的字段的定义:
+XMYWP,f a0column代表字段名,row代表要初始化的数据。
w.A1M So9l AVb0 <column>id</column>51Testing软件测试网5jl\0~C4oh-|_m
<column>username</column>51Testing软件测试网2^[#Gk#u
<column>password</column>
+U)O"|N5Ay]0 。。。。。。。51Testing软件测试网w+]wTay
<column>credentials_expired</column>
IS jV1EdG&N|0 <row>51Testing软件测试网+N\W+P^m
<value description="id">-1</value>51Testing软件测试网BKS\T
<value description="username">user</value>
2^e8H!mm)_V H&j0 。。。。。。51Testing软件测试网^;E}iw%bo+B(M
<value description="credentials_expired">0</value>
4`(s2G)M,hQi0p0 </row>51Testing软件测试网N,t e W r:{X7y)i X
到此我们完成了数据库表结构的创建和初始数据的插入。这种方式是开发者比较喜欢的方式,或许他们是不需要自已写sql语句。这样只需确认对应关系,编写相应的xml文件即可!