您的位置:首页 > 编程语言 > Java开发

2014年工作中遇到的20个问题:161-180

2015-05-03 00:00 351 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springmvc web Mybatis mysql