实习总结

本文探讨了服务层设计的最佳实践,包括参数传递方式、非空验证、公共方法的灵活性改进、异常处理策略等,并涉及数据库查询技巧及JSON与集合转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、页面传值给service传三个对象,必须字段对象(字段或集合[多个对象])、可选字段对象、来源字段对象(来源ip,来源id等),不同service方法可新建不同的这三个对象类。service返回同一个对象(是否成功字段、Object字段、message字段,还可加上分页时的总记录数等)。
2、然后在service层中,可通过BeanUtils.copyProperties(requiredParam, payCart);BeanUtils.copyProperties(optionParam, payCart);同时向某对象复制传值。
3、写公用方法时,比如getCart(),传值不要写死成getCart(userId,productId,cartDao),而是写成getCart(requiredParam,optionParam,originParam);就灵活多了!出现问题时可throw new  RuntimeException("字段为空");
4、非空判断可写在公用方法!也可使用spring自定义注解,详见。。。。。。。。。。。。。。。。
5、html中的img有onerror属性,图片加载失败时可触发事件。
6、数据库查询数据,可使用函数,比如统计url点击次数,停留时长,可用select dealedTable.Url,COUNT(dealedTable.Url) from (select substring_index(event_url,'?',1) as Url from url_param_2014_09 ) as dealedTable GROUP BY dealedTable.Url;

7、json与List等集合互相转化,String q=JsonSerializableUtil.toJson(list);List<PageAccessResult> test=JsonSerializableUtil.fromJson(q, new TypeToken<List<PageAccessResult>>(){});但一般采取把集合作为一个对象的一个属性,把对象进行json互相转化即可;或:
public static <T> T[] jsonStringToList(String json, Class<T> clazz) {
   JSONArray jSONArray=JSONArray.fromObject(json);
   return (T[]) JSONArray.toArray(jSONArray, clazz);
}

8、之前用httpClient测试http服务,最近发现后台服务访问远程rest接口的需求,可采用spring 框架中的RestTemplate类实现。

9、ab测试

10、所有的service方法按顺序写好注释,便于以后维护:比如:
public WebResult addTopic(TopicUpdateRequiredParam requiredParam,TopicUpdateOptionParam optionParam,TopicOriginParam originParam) {
        String method = "TopicServiceImpl.addTopic"; 
        //1打印开始调用日志
        LogInfoUtil.logInfoBegin(requiredParam, optionParam, originParam, method, logger);              
        //2验证必填参数
        WebResult result=(WebResult) VerifyUtil.verifyVoluationObject(requiredParam, "TopicUpdateRequiredParam", method, new WebResult(), logger);
        if(result.getCode()==ResultCode.FAIL)
            return result;
        result=(WebResult) VerifyUtil.verifyVoluationObject(optionParam, "TopicUpdateOptionParam", method, result, logger);
        if(result.getCode()==ResultCode.FAIL)
            return result;
        //3复制参数
        Topic topic=new Topic();
        try {
            BeanUtils.copyProperties(topic, requiredParam);
            BeanUtils.copyProperties(topic, requiredParam);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //4执行保存
        topicDao.addTopic(topic);    
        //5打印结束调用日志
        LogInfoUtil.logInfoEnd(result, method, logger);
        return result;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值