2014年工作中遇到的20個問題:161-180,2014年161-180
161.Mybatis的Dao找不到xml中的映射配置,可能的一個原因是:xml配置中的namespace不對。
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.p2p.user.dal.dao.MemberPointDao.selectForUpdateByUserId
<mapper namespace="com.p2p.user.dal.dao.MemberPointDao">這個地方的名字要正確。
162.Mybatis的ResultType映射,可以使用註解@ResultMap。
@ResultMap("MemberPointResultMap")
@Select("select "+columns+" from "+tableName+" where user_id=#{userId} for update")
MemberPoint selectForUpdateByUserId(String userId);
<mapper namespace="com.p2p.user.dal.dao.MemberPoint">
<resultMap id="MemberPointResultMap" type="MemberPoint">
<result column="user_id" property="userId" />
<result column="produce_cumulative" property="produceCumulative" />
<result column="consume_cumulative" property="consumeCumulative" />
<result column="level" property="level" />
</resultMap>
</mapper>
163.mysql資料庫修改密碼。
登入伺服器Linux,想登入mysql,提示/tmp/socket不能串連。
在本地,通過dos串連遠程mysql,可以正常登入和查看,但是沒有mysql資料庫的許可權。非root使用者,沒許可權修改自己的密碼。
奇怪的是,線上mysql使用的socket是自己安裝目錄下的,為什麼不能正常登入mysql呢?
只能讓root去修改密碼了。
164.非同步搜尋和表格,回顯使用者的查詢條件比較麻煩,還有高亮等樣式。
同步開啟新頁面的方式,方便使用者搜藏當前連結,利於搜尋引擎最佳化。
如果是後台管理系統或者內部使用的系統,AJAX的方式更加方便友好。
165.把Freemarker、Java-EL運算式和Mybatis的運算式搞混淆了。
<#assign queryUrl="${base}/loan/list.html?time=${page.time}&term=${page.term}"/>
這裡只能使用${},取的是Java背景變數。
#只有Freemarker內部和Mybatis設定檔中支援。
166.不使用left-join等多表關聯查詢,只用單表查詢和Java程式,簡便實現“多表查詢”效果,
局限性:單表查詢,不適合做多表同盟搜尋,並且需要分頁的時候。
167.分頁的URL有2種情況。
如果傳入的url包含有參數,含有“?”後面的參數,就不需要再加上?了。
<#if url?contains('?') == -1 >
<#assign pageUrl = "${url}?pageNum=${pageNum}&pageSize=${data.pageSize}">
<#else>
<#assign pageUrl = "${url}&pageNum=${pageNum}&pageSize=${data.pageSize}">
</#if>
這個問題也可以說明,分頁url開啟新的頁面,並且使用Freemarker產生,維護參數比較麻煩。
如果使用AJAX的分頁組件,不重新整理頁面,JS中維護分頁查詢參數就簡單很多了。
168.JQuery POST發送參數有要求。
$.post(
"/article/comment/list.json",{
"params[article_id]":${article.id},
pageNo:pageNo
}
正確的寫法:article_id,"params.article_id","params[article_id]"
如果參數名稱包含“()[]”,需要用引號括起來。
169.Mybatis屬性必須存在。
傳入的對象,必須存在maxDate等屬性,否則就會報錯。
為什麼會出現這種問題呢?
searchListPage(PageVo);
searchListPage(LoanPageVo);
LoanPageVo有maxDate屬性,而PageVo沒有。
帶搜尋的分頁,可能會接收更多的條件,普通的其它地方的查詢可能不需要maxDate等欄位,想把參數設定得更為通用,
能用PageVo就不用LoanPageVo,但是最後卻遇到了上面的問題。
做法是:把2個dao分開,簡化處理。
<select id="searchListPage" resultType="java.util.Map">
select t.*
from p2p_transfer t left join p2p_loan_info l on t.lid =
l.lid
where 1=1
<if test="maxDate != null">
and #{maxDate} >= t.exptime
</if>
<if test="minDeadline != null">
and deadline >= #{minDeadline}
</if>
<if test="maxDeadline != null">
and deadline <= #{maxDeadline}
</if>
order by id desc
</select>
170.Freemarker中的“大於”和“小於”。
用“gt”和“lt”。
<#if category.count gt 0 >
科普一下:
gt :great than
lt : less than
171.SpringMVC2種訪問方式,的動態渲染與JSON格式同時支援。
http://fansunion.cn/case.json
http://fansunion.cn/case.html
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
<bean id="marshallingView"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean id="xStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true" />
</bean>
</property>
<property name="contentType" value="application/xml" />
</bean>
</list>
</property>
</bean>
這種方式的好處,如果想由動態渲染轉換為JSON格式,很方便。
172.SpringMVC的@ModelAttribute。
@ModelAttribute
protected void menuColumnList(Model model) {
List<Map<String, Object>> menuColumnList = initMenuColumnList();
model.addAttribute("menuColumnList", menuColumnList);
currentColumn(model, 0);
}
在BaseController定義了上述方法,ArticleController繼承BaseController,那麼ArticleController的每個方法響應的時候,都會
執行menuColumnList方法,從而查詢菜單資料。
但是,在後端管理系統中,菜單是固定的,不是動態,因此不需要再查詢。
解決辦法是,建立AdminBaseController,所有Controller共用的方法才放到Controller,原來的BaseController可以改名為
FrontController。
173.AJAX方法報錯。
某Web前端同事封裝了AJAX方法,結果在使用過程中報錯了。
為什麼這麼說呢?同樣的參數,$.ajax和$.post都是可以的,只要使用了ptp.ajax就不正常,
對比一下,就知道對錯了。
174.substring包頭不包尾。
java.lang.String.substring public String substring(int beginIndex,int endIndex)返回一個新字串,它是此字串的一個子字串。
該子字串從指定的 beginIndex 處開始,直到索引 endIndex - 1 處的字元。
因此,該子字串的長度為 endIndex-beginIndex。
175.SpringMVC攔截器排除某些路徑。
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<mvc:exclude-mapping path="/resource/**"/>
<bean class="com.xinlong.cms.front.interceptor.CmsFrontInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
到此還有個問題mvc:exclude-mapping 標籤不被spring-mvc-3.0.xsd支援,
該配置在spring-mvc-3.2.xsd中,可以通過http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd擷取,
SpringMVC頭部引入
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
使用spring-mvc-3.2.xsd。
176.replace into 跟 insert 功能類似,不同點在於:replace into 首先嘗試插入資料到表中,
1. 如果發現表中已經有此行資料(根據主鍵或者唯一索引判斷)則先刪除此行資料,然後插入新的資料。
2. 否則,直接插入新資料。
要注意的是:插入資料的表必須有主鍵或者是唯一索引!否則的話,replace into 會直接插入資料,這將導致表中出現重複的資料。
表有3個欄位:uid,name,value
主索引:可以是uid,或者 uid和uname
replace into p2p_user_info(user_id,info_name,info_value) values(#{uid},'borrower_auth',#{auth}
177.資料庫與Map大小寫。
查詢資料庫結果儲存在map中,資料庫的欄位是大寫的,比如USER_INFO,Map取資料的時候,用的是小寫“user_info”。
把資料庫的統一改為小寫。
178.Html中重複定義相同元素,會導致很多奇葩的問題。
比如定義2個title,<title></title>;<title>1</title>頁面中的標題為空白,瀏覽器將其預設為當前訪問的url。
定義2個相同的id,js取值會取錯。
2個同名name的input框,表單提交都會提交,但是後端接收的時候,可能會是第1個,可能不是想要的。
179.sql查詢排序不穩定。
select * from user order by id desc;
主鍵id是唯一的,排序也是唯一的。
select * from user order by name desc;
如果遇到2個一樣的name,比如“boy”,那麼查詢結果就是不穩定的。
我用mysql-front查詢,發現結果都一樣。
但是換一個資料庫查詢工具,還有web介面中看到的,每次是不一樣的。
排序不穩定,給人的感覺不穩定。
180.SpringMVC安全問題。
@RequestMapping("list")
public void list(PageVo form, Model model) {
form.handleSearch();
PageVo page = withdrawService.listPage(form);
form.resetSearch();
model.addAttribute("res", 1);
model.addAttribute("data", PageUtils.data(page));
}
list.json直接響應json格式的請求。
這裡面存在一個問題,如果model.addAttribute裡放置的屬性有多餘的,那麼通過這種方式,前端獲得的json
會存放有多餘的資訊,比如當前登入的使用者資訊。
model.addAttribute("user",user);
手動響應JSON格式的請求,只發送必要的欄位,比較好。
原文首發:http://fansunion.cn/article/detail/559.html