2016年工作中遇到的問題21-30

小雷FansUnion發表於2016-06-04
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;
}

相關文章