2016年工作中遇到的问题21-30

21.Junit 设置默认的jvm参数方法
问题:我们的工程中运行junit时,要为每个testcase设置一下jvm的参数,并且jvm的参数都是要一样的?有没有方法设置所有testcase的jvm参数?这样就不用每个testcase手动设置jvm了。


Eclipse中选择Window=>Preferences=>Java=>Installed JRES=>选中安装的jdk或者jre并进行编辑=》在Default VM Arguments中输入需要设置的jvm参数=》点击Finish完成设置。


参考资料:http://www.iteye.com/problems/86519


22.单元测试没有自动回滚。
//单元测试的mysql数据库,最好是单独的一套库,没有任何数据。如果开发和单元测试共用数据库,listAll之类的方法会有影响。
//单元测试:1.构造数据,2.执行操作,3.断言,4.回滚
//设置自动回滚
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)  
@Transactional  
@ContextConfiguration(locations={"classpath*:spring-dataSource.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class BaseDaoTest;


日志也打印出来,回滚了
2016-04-27 10:06:02 INFO  [main] (TransactionContext.java:136) - Rolled back transaction for test context [DefaultTestContext@7caa999 testClass = AddressDaoTest, testInstance = com.buoumall.webservice.test.dao.AddressDaoTest@a447fce, testMethod = testListAll@AddressDaoTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1722b2a6 testClass = AddressDaoTest, locations = '{classpath*:spring-dataSource.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
2016-04-27 10:06:02 INFO  [Thread-1] (AbstractApplicationContext.java:862) - Closing org.springframework.context.support.GenericApplicationContext@67099f71: startup date [Wed Apr 27 10:05:59 CST 2016]; root of context hierarchy
2016-04-27 10:06:02 WARN  [Thread-1] (DisposableBeanAdapter.java:360) - Invocation of destroy method 'close' failed on bean with name 'sqlSessionTemplate': java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession
2016-04-27 10:06:02 INFO  [Thread-1] (DruidDataSource.java:1385) - {dataSource-1} closed


Brand品牌,相关测试,自动回滚了,但是Address收获地址的却没有回滚。
因为都是模版生成的代码,按说都没有问题才对。


最后换了个思路,会不会是表结构的问题,果然,发现了“坑”。
CREATE TABLE `address` (
  `id` varchar(50) NOT NULL DEFAULT '',
  `memberId` varchar(50) DEFAULT NULL,
  `addressee` varchar(20) DEFAULT NULL COMMENT '收件人',
  `areaId` int(11) DEFAULT NULL COMMENT '地址区域信息',
  `detailed` varchar(50) DEFAULT NULL COMMENT '详细地址:街道',
  `mobile` varchar(20) DEFAULT NULL COMMENT '收件人电话',
  `phone` varchar(20) DEFAULT NULL,
  `email` varchar(30) DEFAULT NULL COMMENT '收件人邮箱',
  `type` tinyint(4) DEFAULT NULL COMMENT '地址类型:0 家庭 1:工作 2:其他',
  `isDefault` tinyint(4) DEFAULT '0' COMMENT '是否s是默认地址',
  `alias` varchar(50) DEFAULT NULL COMMENT '地址别名:默认(收件人 省份)',
  `createTime` datetime DEFAULT NULL,
  `updateTime` datetime DEFAULT NULL,
  `isDelete` tinyint(4) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


数据库引擎是“MyISAM”,不支持事务。
之前看文章,有提到过,还好有点印象,不然根本发现不了。


23.给1个表添加多个字段
  alter table productkind 
  add column
  `updateTime1` datetime DEFAULT NULL,
  add column
  `isDelete1` int(11) DEFAULT NULL;
  
  或者
    alter table order_shop_sale_count 
    add column
( `createTime` datetime DEFAULT NULL,


  `updateTime` datetime DEFAULT NULL);
  
24.数据库驱动和打印sql日志2种实现方式.
方式1:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>


jdbc.url=jdbc:mysql://192.168.1.254:3306/dev?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8


jdbc.driverClassName=com.mysql.jdbc.Driver


log4j日志级别,设置为debug。

打印出来的sql,不清晰。
insert into(*,*)之类的。
sql本身和值,分2个部分展示。


方式2:
<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>

jdbc.url=jdbc:log4jdbc:mysql://192.168.1.254:3306/dev?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 
jdbc.driverClassName=net.sf.log4jdbc.DriverSpy
   自动打印sql,“select id,name,logo,createTime,updateTime,isDelete,isOpen,sort,enname,description from goods_brand where id = 1 ”  
  sql结构更清晰,直接复制到mysql控制台可执行。
   
25.Maven的jar包冲突。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.mall</groupId>
<artifactId>goods-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
项目中的spring配置是4.x的,dubbo默认依赖spring的是2.5的。
单元测试执行的时候,用的2.5的。

手动“排除”。

26.Mybatis的别名冲突。
    出现了2个Tag。
方法1: 定义多个typeAliases会报错。
<typeAliases></typeAliases><typeAliases></typeAliases>,

方法2:同时定义package和typeAlias也会报错
<package name="com.mall.user.model" />
    <typeAlias alias="UserEntity" type="com.dy.entity.User"/>

方法3:全部都定义成typeApias,单个定义,太繁琐

方法4:分项目,2个Tag分到2个项目中。由于我们今后才会分2个项目,暂时不用。

方法5:Mybatis的Mapper文件,使用全路径。


27.装了个teamview for linux被外网攻击,变成肉鸡了,以后服务器运维的小伙伴也注意下,ssh不要用密码登录。
某人犯了个大错误,怪不得内网经常断网,连接不正常。
现在还不敢肯定,但至少是原因之一。


SSH登录,一般不就是第1次输入密码么,后面直接用key就行了。


28.Mybatis的foreach语法!
这个地方的“collection”是表明集合的类型,还是集合变量的名称?


Java Mapper文件
List<Rule> listAllTimeout(List<Long> customIdList); 对应的 foreach collection="list" √ 正确
List<Rule> listAllTimeout(@param("customIdList")List<Long> customIdList); 对应的 foreach collection="customIdList" √ 正确
List<Rule> listAllTimeout(List<Long> customIdList); 对应的 foreach collection="customIdList" × 错误


<select id="list" resultType="Rule">
select *
<if test="customIdList != null">
where customId in
<foreach collection="customIdList" index="index" item="item"
open="(" separator="," close=")">
#{item}
</foreach>
</if>
order by updateTime desc
</select>

29.图片上传,临时图片,垃圾图片。
用户上传图片的生活,很多是临时的,比如上传了一个头像,但是最终没有保存。
比如,一件衣服上传5个图片,又删除了2个。


一种办法是,在每一次图片上传的地方,判断图片“增加、删除、不变”,处理url的保存,同时负责物理图片的删除等操作。
另外一种办法是,后台只负责图片存储和url的保存。但是,不负责图片的删除。写1个单独的图片服务,负责查询哪些图片是没有用的,可以手动删除。
这样,就不用再每一个图片上传的地方,去操心,有没有图片是已经没有用的,但是没有被删除。


30.SpringMVC异步数据。
Web前端获得数据,有异步json的,也有同步渲染的。
异步json格式,前端可以看到数据格式,自己可以循环等进行处理。
而如果是后端的,Web前端人员看不到数据格式。
他们学会Freemarker等模版语言,自己就可以进行展示控制。


因此,对于部分同步页面,为了方便前端自己渲染,而不是后端人员,告诉他模型Model中有哪些数据,数据的格式,
可以写一个有同样数据的异步的请求,等他们处理完成后,再删除。


    @RequestMapping("/customBegin")
public String customBegin(Model model, Long productId) {
doCustomBegin(model, productId);
return "custom/customBegin";
}


private void doCustomBegin(Model model, Long productId) {
model.addAttribute("productDetailBeanList", productDetailBeanList);
}


//方面Web前端看到数据的格式,自己写模版
@RequestMapping("/customBeginJson")
@ResponseBody
public Map<String, Object> customBeginJson(Model model, Long productId) {
doCustomBegin(model,productId);
return model.asMap();
}

//特别注意,SpringMVC中的Model,model.asMap不能直接返回,会报错。
//需要手动新建map,把model中的数据放进去。
@RequestMapping("/customBeginJson")
@ResponseBody
public Map<String, Object> customBeginJson(Model model, Long productId) {
doCustomBegin(model,productId);
Map<String, Object> map = Maps.newHashMap();
map.putAll(model.asMap());
return map;
}

你可能感兴趣的:(maven,mybatis)