XML
- 書寫規(guī)范:非生成的SQL一律使用大寫,縮進、對齊必須工整
- 注釋規(guī)范
<!-- 描述說明 作者 時間-->
例:
<!-- 后臺考試管理分頁查詢,帶排序規(guī)則 zhaoj 2016年12月6日 17:17:57-->
<select id="relationQueryByPageWithSort" resultMap="result">
SELECT
EXAM_SCOPE.*
FROM
EXAM_SCOPE
<where>
<include refid="ExamScopeQueryInBo" />
</where>
</select>
通用Java規(guī)范
- 編碼前,務(wù)必熟讀Java代碼規(guī)范,規(guī)范文檔地址詳見SVN http://218.17.157.105:8055/repos/vcomponent/Document/0.項目管理/0.項目規(guī)范/JAVA規(guī)范.doc;
- 編碼前,務(wù)必導(dǎo)入eclipse注釋模板,模版地址詳見SVN http://218.17.157.105:8055/repos/vcomponent/Document/0.項目管理/0.項目規(guī)范/codetemplates.xml;
- 接口和實現(xiàn)類均要寫上注釋;
- Java中對屬性、方法、類進行修改時,請加上自己的名字,如:
你(userName)修改前的代碼如下:
/**
* 切面方法體環(huán)繞
*
* @date 2016年11月25日 上午10:11:33
* @author zhaoj
* @since V1.0.3
* @return
*/
private Object aroundAdvice() {
return null;
}
修改后應(yīng)加入注釋@author userName,
/**
* 切面方法體環(huán)繞
*
* @date 2016年11月25日 上午10:11:33
* @author zhaoj
* @author userName
* @since V1.0.3
* @return
*/
private Object aroundAdvice() {
return null;
}
Bo(Bean Object)
- 字段名規(guī)范
- 不隨意增加基礎(chǔ)類型字段,如:ExamUser對象有屬性名為
id的屬性,則切勿再添加屬性名為ids、id1、id2、idNotEqual一類的重復(fù)屬性,一律使用id屬性; - 在擴展對象類型字段時,新增的業(yè)務(wù)實體屬性名與業(yè)務(wù)實體名一致,避免重復(fù)定義,且只能做聲明,不能初始化,如:
public class ExamUser extends AbstractExamUser{
private ExamCourse examCourse;
private ExamPolicy examPolicy;
private ExamType examType;
private ExamStudentAnswer examStudentAnswer;
//下面的初始化是極其不推薦的,切勿使用
//private ExamStudentAnswer examStudentAnswer = new ExamStudentAnswer ();
}
- Bo需重寫
toString()方法;
Dao(Data Access Object)
- BaseDao實現(xiàn)類中已經(jīng)封裝了非常豐富的表操作,在寫新的接口之前,要先思考一個問題:在不影響功能和性能的情況下,使用生成的代碼能否滿足需求;
Service
- 統(tǒng)一通過
getDao()的方式調(diào)用Dao層代碼,如:
public class ExamScopeServiceImpl extends BizBaseServiceImpl<ExamScope> implements ExamScopeService{
@Resource
private ExamQuestionDao examQuestionDao;
public List<ExamScope> studentQuery(ExamScope bo) {
return getDao().studentQuery(bo);
}
public Integer queryCount(ExamScope bo) {
return examQuestionDao.queryCount(bo);
}
}
- 涉及到事務(wù)的操作,務(wù)必方法上加上
@Transactional注解; - Service層盡量只注入Dao,不推薦注入Service,避免形成閉回環(huán),如:
public class AService{
//不推薦
@Resource
private BService bService ;
}
public class BService{
//不推薦
@Resource
private AService aService ;
}
- BaseService實現(xiàn)類中已經(jīng)封裝了非常豐富的表操作,在寫新的接口之前,要先思考一個問題:在不影響功能和性能的情況下,使用生成的代碼能否滿足需求?
Controller
- 統(tǒng)一通過
getService()的方式調(diào)用Service層代碼,如:
public class ExamUserController extends BizBaseController<ExamUser> {
@Override
public ModelAndView showMain(){
Paginator<ExamUser> paginator = new Paginator<ExamUser>();
List<ExamUser> list = getService().queryByPageWithSort(paginator, new SortData(Direction.ASC,properties,"EXAM_USER.ID"));
ModelAndView mv = getModelAndView(getClassRequestMapping() + "/showMain");
mv.addObject("resultList", list)
mv.addObject("paginator",paginator)
mv.addObject("sortData",sortData);
return mv;
}
}
- Controller層盡量只注入Service,不推薦注入Dao
-
@RequestMapping中的value值與函數(shù)名需保持一致
public class ExamUserController extends BizBaseController<ExamUser> {
@RequestMapping(value = "/showMain")
public ModelAndView showMain(){
Paginator<ExamUser> paginator = new Paginator<ExamUser>();
List<ExamUser> list = getService().queryByPageWithSort(paginator, new SortData(Direction.ASC,properties,"EXAM_USER.ID"));
ModelAndView mv = getModelAndView(getClassRequestMapping() + "/showMain");
mv.addObject("resultList", list)
mv.addObject("paginator",paginator)
mv.addObject("sortData",sortData);
return mv;
}
}
- 后臺驗證的問題必須引起注意:入?yún)⒑戏ㄐ耘袛啵颂幍暮戏ㄐ园ǎ狠斎胍?guī)則合法(例如正則表達式后臺驗證)及數(shù)據(jù)沖突合法性(例如雙瀏覽器操作)
- 確定如下函數(shù)名均不能滿足你,再去定義一個新的函數(shù):
//主頁面
showMain()
//帶條件的顯示主頁面,主要用于“返回”按鈕
showMain(T, Paginator<T>, SortData)
//搜索、分頁查詢
list(T, Paginator<T>, SortData)
//添加頁面
add()
//編輯
edit(Integer)
//保存
save(T)
//單個刪除
delete(Integer)
//查看
show(Integer)
//批量刪除
batchDelete(Integer[])
//批量更新
batchUpdate(Integer[], T)
//批量插入
batchInsert(T[])
- 統(tǒng)一調(diào)用
getModelAndView(String modelName)方法構(gòu)建視圖,如:
getModelAndView(String)
- 統(tǒng)一調(diào)用
addObject(String attributeName, Object attributeValue)方法組裝模版數(shù)據(jù),如:
ModelAndView mv = getModelAndView(getClassRequestMapping() + "/showMain");
mv.addObject("resultList", list);
- 推薦使用如下方式獲取Bean
//推薦使用
ExamUser examUser1 = AppContext..getBean(ExamUser.class);
//不推薦使用
//ExamUser examUser2 = (ExamUser)AppContext.getBean("examUser");
- 統(tǒng)一使用如下方式獲取當前用戶
//Controller、Service、Dao層通用
AppContext.getCurrentUserId();
- 在合適的場景下,使用
queryCount(T bo)去代替query(T bo) - Servive注入時,加
@Resource注解即可,無需@Resource(name="vslObjectServiceImpl"),Service注入Dao同理:
@Controller
@RequestMapping("/admin/vslObject")
public class VslObjectController extends BizBaseController<VslObject> {
//@Resource(name="vslObjectServiceImpl")
@Resource
VslObjectService service;
}
- 每個帶有
@RequestMapping注解的方法都應(yīng)加上@LogMark(memo="備注"),用于記錄日志;
HTML
- 標簽必須對齊
- 學會用Google瀏覽器中調(diào)整簡單樣式
- Freemarker在頁面上的常用操作:
//格式化數(shù)字輸出
<#setting number_format="#">
//獲取枚舉國際化propertis文件中鍵為“Bool.YES”的值
${resourceBundle("Bool.YES")}
//---------------------------------------宏及常用宏的使用---------------------------------------
//引入宏
<#import "/common/tag.htm" as tag>
//截取字符串
<@tag.substr content="${item.examQuestion.content!}
//根據(jù)枚舉生成select下拉框
<@enum.select id="bo.result" type="com.vnetoo.common.enums.Bool" header="true" default=""/>
//根據(jù)枚舉生成radio單選框
<@enum.radio id="bo.type" type="com.vnetoo.common.enums.Bool" default="${bo.type}"/>
//根據(jù)枚舉生成checkbox復(fù)選框
<@enum.checkbox id="bo.type" type="com.vnetoo.common.enums.Bool" default="${bo.type}"/>
JS
- 學會用
console.log() - 學會用Google瀏覽器 F12中打斷點,可以利用
console.log()來打斷點 - 學會在Google瀏覽器 F12查看變量
- 學會用Google瀏覽器 改樣式
- 選擇器要重點熟悉