開發(fā)常用操作

Mapper常用操作
  1. 使用include代替大量重復的條件判斷代碼:[XXXQueryInBo、XXXQueryInOtherBo、SortBizBaseObject],如:
<!-- 后臺考試管理分頁查詢,帶排序規(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常用操作
  1. 獲取當前用戶ID
AppContext.getCurrentUserId();
  1. 獲取當前request、response
AppContext.getRequest();
AppContext.getResponse();
  1. 從Spring中獲取Bean
ExamUser examUser = AppContext.getBean(ExamUser.class);
  1. 獲取application.yml中的配置
 @Value("${subSystem:}")
 private String subSystem;
    
 @Value("${subNode:'默認值'}")
 private String subNode;
Controller層常用操作

職能:

  1. 數(shù)據(jù)校驗
  2. 數(shù)據(jù)封裝
  3. 將合法的數(shù)據(jù)傳遞到Service層,并接收Service層返回結(jié)果
  4. 封裝響應數(shù)據(jù)
  5. 返回響應數(shù)據(jù)
  1. 常用注解解析:
@CheckToken : 用于token驗證,防止CSRF攻擊,也有防止重復提交的作用
@ResponseBody : 用于返回Json數(shù)據(jù)
@Authorization : 用于權(quán)限驗證,說明調(diào)用該方法需要驗證權(quán)限
@LogMark(memo="添加保存") : 用于記錄日志,可以用于修飾方法,也可用于修飾類,若類和方法上均有修飾,則系統(tǒng)會以方法上的為準
@RequestMapping(value = "/insert") : 用于URL映射
  1. 合理使用RestControllerResponseBody
  • 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");
        }
}
```
  1. 文件用MultipartFileMultipartFile[]接收:
    @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();
    }
    
    
    關(guān)于MultipartFile的更多操作,詳見:官方API文檔
  2. 調(diào)用Service層需使用getSerive()
  3. Restful URL傳參Demo:
    @RequestMapping(value = "/delete/{id:[\\d]+}")
    @ResponseBody
    public AjaxResponse delete(@PathVariable Integer id){
        return super.delete(id);
    }
    
    
  4. 不要輕易將帶有“,”符號的參數(shù)傳到Controller,詳見:原因
HTML常用操作
  1. 頁面讀取枚舉
${resourceBundle("Role."+item.role)}
  1. 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}"/>

常用配置

  1. 刪除約束配置(component.deleteRestrictXml配置)
  2. 在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>
  1. 在application.yml中加入如下配置:
component:
         deleteRestrictXml: spring-relationRef.xml
  1. 后臺參數(shù)驗證配置,詳見:基于Hibernate Validate的后端驗證
  2. 可在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> 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • 如果人生是一場馬拉松,又何必在乎跑得多快,找到你跑道的目標并想著前進才是價值,急什么……
    小鬼丹閱讀 154評論 0 0
  • 每當聽到上街人聲鼎沸, 我渴望與人說話交流, 但她們很熱情親切對我, 看一幕被我感動了。 我轉(zhuǎn)過頭看雪花飄舞, 聽...
    芝苑閱讀 218評論 0 0
  • 金風熏染銀杏醉,玉露調(diào)醅桂蕊香。青鹿難尋黃鶴杳,獨憑闌干待斜陽。
    江南煙雨閱讀 412評論 0 14

友情鏈接更多精彩內(nèi)容