当前位置: 首页 > news >正文

谷歌seo技巧/郑州seo外包顾问

谷歌seo技巧,郑州seo外包顾问,登录手机网站模板html,门户网站建设使用语言背景说明 环境说明:Java、Eclipse、Maven、SpringMVC、MyBatis、MySQL、H2。 在写DAO层的单元测试时,我们往往会遇到一个问题,测试用例所依赖的数据库数据被修改或删除了,或者在一个新的环境下所依赖的数据库不存在,导…

背景说明

环境说明:Java、Eclipse、Maven、SpringMVC、MyBatis、MySQL、H2。

在写DAO层的单元测试时,我们往往会遇到一个问题,测试用例所依赖的数据库数据被修改或删除了,或者在一个新的环境下所依赖的数据库不存在,导致单元测试无法通过,进而构建失败。

在这种情况下,使用H2内存数据库来模拟数据库环境是一个很好的解决方案。官网链接如下:http://www.h2database.com/html/main.html。目前我研究的是模拟MySQL环境,对于各个数据库的兼容性可以查看如下链接:http://www.h2database.com/html/features.html#compatibility。

使用步骤

(注意下面的步骤基于前文提到的环境说明)

1) 在pom.xml中添加h2database的依赖

<dependency><groupId>com.h2database</groupId ><artifactId>h2</ artifactId><version>1.4.192</ version>
</dependency>

2) 修改jdbc.properties的数据库驱动和url

#h2
jdbc.driverClassName = org.h2.Driver
jdbc.url= jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_DELAY=-1
jdbc.username =root
jdbc.password =123456

对于jdbc.properties文件,这里有一个技巧。首先项目正式运行的配置文件是放在src/main/resources目录的conf目录下。因为单元测试的jdbc配置和正式运行环境的配置不一致,我们只需要在单元测试的包src/test/java下配置一份相同目录的conf/jdbc.properties。这样在运行单元测试时,最近的配置会覆盖掉原来的配置。注意,这里是整个文件覆盖,而不是文件中的属性覆盖。如果你在test包下的jdbc.properties少配置了什么内容,并不会去resources目录下读取,会引起报错。

3) 配置数据库初始化SQL

在test包的conf下面新建sql文件夹,用于存放初始化数据库的SQL,也就是单元测试需要依赖的表结构及数据。一般我们可以将数据库初始化分为表结构初始化schema.sql和数据初始化data.sql两部分。但这并不是强制要求,你可以根据你的业务逻辑将SQL语句的存放进行划分,便于管理。

4) 编写一个BaseDaoTest基类

该类用于初始化H2数据库。其他单元测试类需要继承自该基类。

package com.szyciov.dao;import java.sql.Connection;
import java.sql.Statement;import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:conf/spring.xml" , "classpath:conf/spring-mybatis.xml" })
public class BaseDaoTest extends AbstractTransactionalJUnit4SpringContextTests {@Beforepublic void setUp() throws Exception {String appfunctionSql = getClass().getResource("/conf/sql/appfunction.sql" ).toURI().toString().substring(6);String areaSql = getClass().getResource("/conf/sql/ddc_area.sql" ).toURI().toString().substring(6);String ddcSql = getClass().getResource("/conf/sql/ddc_all.sql" ).toURI().toString().substring(6);String dataSql = getClass().getResource("/conf/sql/data.sql" ).toURI().toString().substring(6);// System.out.println(appfunctionSql);// System.out.println(areaSql);// System.out.println(ddcSql);// System.out.println(dataSql);BasicDataSource dataSource = (BasicDataSource) applicationContext.getBean("MyDataSource" );// System.out.println(dataSource.getUrl());Connection conn = dataSource.getConnection();Statement st = conn.createStatement();st.execute( "drop all objects;");// 这一句可以不要st.execute( "runscript from '" + appfunctionSql + "'");st.execute( "runscript from '" + areaSql + "'" );st.execute( "runscript from '" + ddcSql + "'" );st.execute( "runscript from '" + dataSql + "'" );st.close();conn.close();}@Testpublic void test_1() {}
}

注意BaseDaoTest基类声明处有@RunWith、@ContextConfiguration以及继承自AbstractTransactionalJUnit4SpringContextTests,这些信息在子类中无须再次编写,如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:conf/spring.xml" , "classpath:conf/spring-mybatis.xml" })
public class BaseDaoTest extends AbstractTransactionalJUnit4SpringContextTests {

也就是说,在没继承这个基类之前,我们的每个Test类的声明都如BaseDaoTest的声明,在继承BaseDaoTest之后反而变得简单了(不需要再注解)。

public class FloatRatioDaoTest extends BaseDaoTest {

5)编写具体的单元测试类,进行测试

下面是我配置好之后的项目目录结构:

2016070501

H2对MySQL的兼容性问题

1) 不支持表级别的Comment

有表SQL如下:

CREATE TABLE `ddc_line` (`Id` varchar(36) NOT NULL COMMENT '序号',`StartArea` int(11) DEFAULT NULL COMMENT '出发区域',`ArrivalArea` int(11) DEFAULT NULL COMMENT '目的区域',`Updater` varchar(36) DEFAULT NULL COMMENT '更新人',`UpdateTime` datetime DEFAULT NULL COMMENT '更新时间' ,`Status` int(11) DEFAULT NULL COMMENT '是否删除'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT= '区域路线信息列表' ;

列名后面的COMMENT是支持的,但是最后面) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT= '区域路线信息列表' ; 中的COMMENT不支持。删掉后面的COMMENT即可。

2) 插入语句的单引号中的\'不支持

有如下SQL,其中一个字段存的就是另一个SQL,里面带有单引号:

INSERT INTO `dataauthorityconfig` VALUES ( '1', '部门权限', 'select d.UserId, a.RoleId,b.Id DynamicId,b.DeptName DynamicName,c.ConfigName,c.ConfigType,a.RootDynamicId\n  from RoleDataAuthority a\n left join Dept b on a.DynamicId=b.Id\n left join DataAuthorityConfig c on a.DataAuthorityConfigId=c.Id\n left join RoleUser d on d.RoleId=a.RoleId\n left join `User` e on d.UserId=e.Id\n where a.`Status`=1 and b.`Status`=1 and d.`Status`=1 and e.`Status`=1\n and c.Id={0} and e.LoginName=\'{1}\'', '1', '2', null, null , '2016-05-27 14:30:49' , '1' , '1' , null, '1');

MySQL支持双引号包含字符串,可以把内容中包含的单引号改为双引号,但其他情况可能会涉及到业务调整。另外,不能将包含字符串的单引号改为双引号,H2会把双引号中的内容当做列名处理。

3) H2 UNIQUE KEY是数据库级别的

H2 UNIQUE KEY不是表级别的,MySQL是表级别的,转为H2后容易出现UNIQUE KEY重复。删掉UNIQUE KEY或者修改KEY的名称即可。

4) 无法执行多个Update语句

如下SQL配置可以在MySQL中执行多次Update,但是H2执行多条就会报错,说parameterIndex有问题,执行一条没有问题。这个问题暂时没有替代解决方案,我的单元测试就只测试了插入一条数据。

</update ><update id="deleteByParam" parameterType="com.szyciov.entity.chargerule.FloatRatio" >update ddc_float_ratio set status = 2 where status = 1<if test="type != 0">and type = ${type}</if ><if test="year != 0">and year = ${year}</if ><if test="month != 0">and month = ${month}</if ></update >

5) 列别名无法用于子查询

如下SQL可以在MySQL中执行,但是不能再H2中执行,这里把查询出来的StartAreaCity字段作为StartAreaCityText字段的子查询使用

  <sql id="Base_Column_List" >Id, StartArea, ArrivalArea, Updater, UpdateTime, Status, (select pid from ddc_area where id = (select pid from ddc_area where id = ddc_line.StartArea)) StartAreaCity, (select area from ddc_area where id =  StartAreaCity) StartAreaCityText</sql >

只得修改成如下:

  <sql id="Base_Column_List" >Id, StartArea, ArrivalArea, Updater, UpdateTime, Status, (select pid from ddc_area where id = (select pid from ddc_area where id = ddc_line.StartArea)) StartAreaCity, (select area from ddc_area where id = (select pid from ddc_area where id = (select pid from ddc_area where id = ddc_line.StartArea))) StartAreaCityText</sql >

6) @:语法不支持

在MySQL中实现取行号时,采用了如下方法:

  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.szyciov.entity.chargerule.PriceRuleExample" >select<if test="distinct" >distinct</if ><include refid="Base_Column_List" />, (@rownum := @rownum + 1) as RowNumfrom ddc_price_rule, (select @rownum := #{page.begin} ) r<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if ><if test="orderByClause != null" >order by ${orderByClause}</if ><if test="page != null" >limit #{page.begin} , #{page.length}</if ></select >

其中@rownum的写法H2不支持,我只能采用了程序的方式来实现行号。

但是H2支持@,参见http://www.h2database.com/html/grammar.html#set__。

参考链接

H2官网:http://www.h2database.com/html/main.html

H2 兼容性:http://www.h2database.com/html/features.html#compatibility

H2 SET@:http://www.h2database.com/html/grammar.html#set__

轻量级数据库比较:http://www.oschina.net/question/12_60371?fromerr=pdqVuV2O

利用h2database和easymock轻松不依赖环境单元测试:http://www.54chen.com/java-ee/h2database-easymock-unit-test.html


http://www.alanzeng.cn/2016/07/unit-test-h2-database/

http://www.lbrq.cn/news/745237.html

相关文章:

  • 贾汪区人民政府门户网站建设/淘宝关键词指数
  • 网页模板下载网站10/线下引流的八种推广方式
  • 扬州外贸网站建设/app平台搭建
  • 关于设计的网站/西安百度推广排名
  • 常熟网络推广/seo是对网站进行什么优化
  • 湖北专业网站建设市面价/网络营销工具与方法
  • 建设通网站查询单位/百度认证营销推广师
  • wordpress插件直播/搜索引擎关键词优化有哪些技巧
  • 青岛微网站开发/有没有专门帮人推广的公司
  • 北京城乡建设官方网站/公司网站设计制作
  • 珠海做网站哪家专业/百度云搜索
  • 培训网络营销的机构/北京seo优化wyhseo
  • 响应式网站设计案例/b站视频推广网站400
  • 网站的在线客服系统/网站建设平台哪家好
  • 中央疫情二十条措施最新/爱站网seo查询
  • 青岛市建设厅网站/简短的软文范例
  • wordpress博客类似/seo推广有哪些
  • 自己做行程的网站/宁波seo排名外包公司
  • 奇米网怎么做网站/被国家禁止访问的网站怎么打开
  • 做网站备案要多久/长沙今日头条新闻
  • p2p网站如何做测试/东莞疫情最新消息今天
  • 网站默认图片素材/电工培训技术学校
  • 怎么给网站做链接/互联网广告平台有哪些
  • 面试问你如何快速优化网站/财经新闻最新消息
  • 建设银行网站登录首页/本地免费发布信息网站
  • 网站推广 昆明/网站推广途径
  • 河南网站建设外贸/长春疫情最新情况
  • 做微信网站公司/长沙官网网站推广优化
  • 室内设计师简介/孝感seo
  • 专注网站建站/网络营销怎么做推广
  • 数据结构:利用旋转在AVL树中维持平衡(Inserting in AVL with Rotation)
  • 算法——质数筛法
  • Kubernetes 简介
  • GaussDB 中 alter default privileges 的使用示例
  • Python 面向对象三大特性详解(与 C++ 对比)
  • linux下查看 UDP Server 端口的启用情况