Mapper常用操作
- 使用
include代替大量重復的條件判斷代碼:[XXXQueryInBo、XXXQueryInOtherBo、Sort、BizBaseObject],如:
<!-- 后臺考試管理分頁查詢,帶排序規(guī)則 zhaoj 2016年12月12日 14:19:25-->
<select id="relationQueryByPageWithSort" resultMap="result3">
SELECT
EXAM_SCOPE.*,EXAM_COURSE.NAME AS COURSE_NAME,EXAM_TYPE.NAME AS TYPE_NAME,EXAM_POLICY.TYPE AS POLICY_TYPE
FROM
EXAM_SCOPE EXAM_SCOPE,
EXAM_POLICY EXAM_POLICY,
EXAM_COURSE EXAM_COURSE,
EXAM_TYPE EXAM_TYPE
<where>
EXAM_COURSE.ID = EXAM_SCOPE.COURSE_ID
AND EXAM_SCOPE.ID = EXAM_POLICY.SCOPE_ID
AND EXAM_TYPE.ID = EXAM_SCOPE.TYPE_ID
<include refid="ExamScopeQueryInBo" />
<include refid="ExamCourse.ExamCourseQueryInOtherBo" />
<include refid="ExamPolicy.ExamPolicyQueryInOtherBo" />
<include refid="ExamType.ExamTypeQueryInOtherBo" />
</where>
<include refid="Sort.orderBy" />
</select>
Java常用操作
- 獲取當前用戶ID
AppContext.getCurrentUserId();
- 獲取當前
request、response
AppContext.getRequest();
AppContext.getResponse();
- 從Spring中獲取Bean
ExamUser examUser = AppContext.getBean(ExamUser.class);
- 獲取application.yml中的配置
@Value("${subSystem:}")
private String subSystem;
@Value("${subNode:'默認值'}")
private String subNode;
Controller層常用操作
職能:
- 數(shù)據(jù)校驗
- 數(shù)據(jù)封裝
- 將合法的數(shù)據(jù)傳遞到
Service層,并接收Service層返回結(jié)果- 封裝響應數(shù)據(jù)
- 返回響應數(shù)據(jù)
- 常用注解解析:
@CheckToken : 用于token驗證,防止CSRF攻擊,也有防止重復提交的作用
@ResponseBody : 用于返回Json數(shù)據(jù)
@Authorization : 用于權(quán)限驗證,說明調(diào)用該方法需要驗證權(quán)限
@LogMark(memo="添加保存") : 用于記錄日志,可以用于修飾方法,也可用于修飾類,若類和方法上均有修飾,則系統(tǒng)會以方法上的為準
@RequestMapping(value = "/insert") : 用于URL映射
- 合理使用
RestController和ResponseBody:
-
RestController用于修飾Controller類,意味著整個Controller的所有接口,返回的均為JSON數(shù)據(jù) -
ResponseBody用于修飾Method方法,意味著方法返回的是JSON數(shù)據(jù) - 例如,下列兩個例子達到的效果是一樣的:
``` java
@RestController
@RequestMapping("/mobile")
public class MobileController {
@RequestMapping("/test")
public Object test() {
return new String("test");
}
}
@RequestMapping("/mobile")
public class MobileController {
@RequestMapping("/test")
@ResponseBody
public Object test() {
return new String("test");
}
}
```
- 文件用
MultipartFile和MultipartFile[]接收:
關(guān)于@RequestMapping(value = "/upload") public ModelAndView upload(MultipartFile file) throws Exception{ //判空 if(!file.isEmpty()){ //存儲路徑,如:D:\test\demo.txt String path = "D:\\test\\"+file.getOriginalFilename(); //使用apache.common提供的工具類寫入磁盤 FileOutputStream fos = FileUtils.openOutputStream(new File(path)); IOUtils.copy(file.getInputStream(), fos); } return new ModelAndView(); }MultipartFile的更多操作,詳見:官方API文檔 - 調(diào)用Service層需使用
getSerive() - Restful URL傳參Demo:
@RequestMapping(value = "/delete/{id:[\\d]+}") @ResponseBody public AjaxResponse delete(@PathVariable Integer id){ return super.delete(id); } - 不要輕易將帶有“,”符號的參數(shù)傳到Controller,詳見:原因
HTML常用操作
- 頁面讀取枚舉
${resourceBundle("Role."+item.role)}
- 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復選框
<@enum.checkbox id="bo.type" type="com.vnetoo.common.enums.Bool" default="${bo.type}"/>
常用配置
- 刪除約束配置(component.deleteRestrictXml配置)
- 在src/main/resources目錄下新建約束XML文件,如:spring-relationRef.xml(文件名可自定義,格式需是xml),中間表不可配置中間約束。
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="com.vnetoo.common.bo.BizRelationObject">
<property name="id" value="1001" />
<property name="operationObjectName" value="com.vnetoo.vcomponent.exam.admin.examKeyPoint.bo.ExamKeyPoint" />
<property name="refObjectName" value="com.vnetoo.vcomponent.exam.admin.examQuestion.bo.ExamQuestion" />
<property name="refField" value="keyPointId" />
<property name="refDaoName" value="examQuestionDaoImpl" />
<property name="msg" value="數(shù)據(jù)被題目引用,不能刪除!" />
<property name="deleteFlag" value="false" />
</bean>
<bean class="com.vnetoo.common.bo.BizRelationObject">
<property name="id" value="1002" />
<property name="operationObjectName" value="com.vnetoo.vcomponent.exam.admin.examCourse.bo.ExamCourse" />
<property name="refObjectName" value="com.vnetoo.vcomponent.exam.admin.examKeyPoint.bo.ExamKeyPoint" />
<property name="refField" value="courseId" />
<property name="refDaoName" value="examKeyPointDaoImpl" />
<property name="msg" value="數(shù)據(jù)被知識點引用,不能刪除!" />
<property name="deleteFlag" value="false" />
</bean>
</beans>
- 在application.yml中加入如下配置:
component:
deleteRestrictXml: spring-relationRef.xml
- 后臺參數(shù)驗證配置,詳見:基于Hibernate Validate的后端驗證
- 可在spring.xml中加載外部配置文件:
<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<!-- 忽略未找到的配置文件路徑 -->
<property name="ignoreResourceNotFound" value="true" />
<!-- file: 物理文件路徑 可以使用相對路徑 同名配置項后面覆蓋前面 -->
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>file:config.properties</value>
<value>classpath:cas.properties</value>
<value>file:cas.properties</value>
<value>classpath:memcache.properties</value>
<value>file:memcache.properties</value>
</list>
</property>
</bean>