2016年工作中遇到的问题1-10:select-for-update锁表



1.select... for update锁表。
注意事项:
事务下使用,@Transactional
如果用主键,只锁住1行记录
如果不用主键,会锁住多条记录,mysql下测试,查询1条,锁住1行,查询2条,锁住2行。


网上不少文章说,没有用主键,会“锁表”,似乎不符合事实额。
比如,http://www.cnblogs.com/chenwenbiao/archive/2012/06/06/2537508.html


2.分页跳转的输入框,可以用html5中的input-number.
<input type="number" max="10" min="1"/>
右侧的“增加-减少”输入工具,可以+1或者-1。
如果用户手动输入,字符串等非数值是不允许的。


但是,存在1个问题。
用户输入的数字,不会检查是否满足 min 和 max属性的限制。


因此,需要额外写js事件去判断,blur失去焦点事件是可以的。


最后需要注意一点,jquery的attr获得是string类型,用户输入的页数 是否 满足min和max,需要先转换成int类型。


//解决分页输入,可能超过最大页数的问题。html5的input-number,不会检查用户的输入,是否满足min和max这2个属性
  $(function(){
 var toGoPage=$("#toGoPage");
 if(toGoPage){
  toGoPage.blur(function(){
 var page=parseInt(toGoPage.val());
 var maxPage=parseInt(toGoPage.attr("max"));
 var minPage=parseInt(toGoPage.attr("min"));
 console.log("page,"+page);
 console.log("maxPage,"+maxPage);
 console.log("minPage,"+minPage);
 if(page>maxPage){
 page=maxPage;
 }
 if(page<minPage){
 page=minPage;
 }
 toGoPage.val(page);
console.log("page,"+page);
  });
  console.log("bind2");
 }
  });
</script>
3.用字符串替换replace,而不是手动拼接字符串。
   var str ="<a href='{url}' target='_about'>{name}</a>";
   str=str.replace("{url}",url);
   str=str.replace("{name}",name);
   
   手动拼接字符串,太麻烦了,可读性很差。
4.jquery-easyui,格式化函数formatter.
注意事项:
a.formatter只需要填写函数的名字。
b.如果是格式化某个字段,field就是字段的名称。
c.如果格式化需要多个字段,field不能不写,同时不能写某个指定的字段,可以用个不存在的字段,比如“null”。
formatLink函数,就存在一定的技巧。field用不存在的“null”,挺好使的。


<#include "common/common.html"/>
<meta charset="UTF-8">
<table
    class="easyui-datagrid"   
    id="datagrid"  
         title="友情链接"  
         url="${base}/friendlink/list"
         toolbar="#toolbar" 
         rownumbers="true" 
         fitColumns="true"
         singleSelect="true"
         data-options="fit:false,border:false,pageSize:10,pageList:[5,10,15,20]" >  
    <thead>  
        <tr>
            <th field="id" width="5%">ID</th>  
            <th field="name" width="5%">名称</th>  
            <th field="url" width="35%">URL</th>
            <th field="remark" width="35%">备注</th>   
            <th field="sort" width="5%">排序</th> 
            <th field="null" width="10%" formatter="formatLink" width="5%">效果</th>    
        </tr>  
    </thead>  
</table> 
<script type="text/javascript">
function formatLink(val,row,index){
if(!row){console.error("The row is null,index="+index);return;};
   var name = row.name;
   var url = row.url;
   var str ="<a href='{url}' target='_about'>{name}</a>";
   str=str.replace("{url}",url);
   str=str.replace("{name}",name);
   return str;
   
}
</script>
5.电商系统,后端添加商品预览。
  后端再做一套“商品详细页面”,工作量巨大。
  解决方法:
    前端商品详细页面,改造下。
再增加一个url,service查询数据的时候,可以查询“未发布”的商品。
后端使用Iframe,嵌入前端新增的url。
“完全逼真”的预览效果。
6.Mybatis的“#{}”和"${}"
@Select("select * from ${tableName} where status =0 order by sort asc,id asc")
List<CommonCategory> listAll(@Param("tableName")String tableName);
这个地方的tableName只能用${},不会带“引号”。
str=abc, ${str}输出abc,#{str}输出 'abc'。


7.SpringMVC3的ResponseBody返回字符串乱码问题.
这个问题遇到很多次了,最近找不到那个配置了,网上找了一个。


引起乱码原因为spring mvc使用的默认处理字符串编码为ISO-8859-1,具体参考org.springframework.http.converter.StringHttpMessageConverter类中public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");


解决方法:


第一种方法:


对于需要返回字符串的方法添加注解,如下:


@RequestMapping(value="/getUsers", produces = "application/json; charset=utf-8")
 public String getAllUser() throws JsonGenerationException, JsonMappingException, IOException
 {
 List<User> users = userService.getAll();
 ObjectMapper om = new ObjectMapper();
 System.out.println(om.writeValueAsString(users));
 DataGrid dg = new DataGrid();
 dg.setData(users);
 return om.writeValueAsString(dg);
 }


此方法只针对单个调用方法起作用。


第二种方法:


在配置文件中加入


<mvc:annotation-driven>
     <mvc:message-converters register-defaults="true">
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
      <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
    </bean>
   </mvc:message-converters>
     </mvc:annotation-driven>
参考:http://www.cnblogs.com/dyllove98/p/3180158.html


网上也有配置了mappingJacksonHttpMessageConverter,不需要。
但是如果配置了这个,访问一个url,比如http://localhost/category/list?amw=w,Chrome出现的“下载”,而不是直接展示内容了。
<mvc:annotation-driven >  
  <mvc:message-converters>
           <bean class="org.springframework.http.converter.StringHttpMessageConverter" >    
            <property name = "supportedMediaTypes">  
                <list>  
                     <value>text/plain;charset=UTF-8</value>  
                </list>  
            </property>  
           </bean>    
         <!--   <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
            <property name="supportedMediaTypes">    
                <list>    
                    <value>applicaton/json;charset=UTF-8</value>    
                </list>    
            </property>    
        </bean>     -->
       </mvc:message-converters> 
</mvc:annotation-driven>
8.Mybatis,There is no getter for property named 'merchantId' in 'class java.lang.String'。


如果mybatis语句中,增加了<if test="merchantId != null">这个if判断,需要给dao方法中,手动增加@Param("merchantId")。
List<String> findByShopId(@Param("merchantId")String merchantId);
<select id="findByShopId" resultType="string">
select id from mall_brand where 1 =1
<if test="merchantId != null">
and merchantType=2 and merchantId=#{merchantId}
</if>
</select>

如果只是#{}取值,不需要手动@Param("merchantId")。
List<String> findByShopId(String merchantId);
<select id="findByShopId" resultType="string">
select id from mall_brand where 1 =1
and merchantType=2 and merchantId=#{merchantId}
</select>
9. Spring直接把配置文件中的变量,放到Java变量中,放到xml中有时候不够直接。
@Value("${mailFromAddress}")
private String mailFromAddress;

10.SpringMVC发送邮件,必须设置“from”参数。
在这个bean中设置from参数不行,因为没有from这个参数。
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>${mailServerHost}</value>
</property>
<property name="port">
<value>${mailServerPort}</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="username">
<value>${mailUserName}</value> <!-- 发送者用户名 -->
</property>
<property name="password">
<value>${mailPassword}</value> <!-- 发送者密码 -->
</property>
<!-- <property name="from">
 <value>${mailFromAddress}</value>
</property> -->
</bean>


@Resource
private JavaMailSender mailSender;

@Value("${mailFromAddress}")
private String mailFromAddress;

//在发送的时候,必须设置from
public void send(String subject,String content,String to){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
simpleMailMessage.setFrom(mailFromAddress);
simpleMailMessage.setTo(to);
mailSender.send(simpleMailMessage);
}

#配置参数
mailServerHost=
mailServerPort=25
mailUserName=
mailPassword=
mailFromAddress=
shopHost=


需要特别注意,userName是用来连接服务器的,from参数是可以手动设置的。
from和userName可以不同。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值