发表于: 2017-10-30 22:19:07

1 678


今天完成的事情:

1:修改了一波,理解了部分内容,部分加上了注释,修改了部分代码和bug.

2:看书补基础,关于集合方面

package com.ptteng.controller.invest;
import java.math.BigInteger;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.ptteng.playboy.invest.model.Product;
import com.ptteng.playboy.invest.model.User;
import com.ptteng.playboy.invest.service.ProductService;
import com.ptteng.playboy.invest.service.TransactService;
import com.ptteng.playboy.invest.service.UserService;
import com.ptteng.util.TransactUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ptteng.playboy.invest.model.Transact;


/**
* Transact  crud
*
* @author magenm
* @Date 2014-4-16 13:43
*
*/
@Controller
public class TransactController {
private static final Log log = LogFactory.getLog(TransactController.class);

   @Autowired
   private TransactService transactService;


   @Autowired
   private UserService userService;

   @Autowired
   private ProductService productservice;





   /**
    * @param
    * @return
    * @throws ServiceException
    * @throws ServiceDaoException
    */
   /**交易记录接口
   *根据接口文档定义参数*/
   @RequestMapping(value = "/a/transact/{uid}/search", method = RequestMethod.GET)

/**HttpServletRequest和HttpServletResponse是拦截器的使用
   *后面跟接口文档中的字段
    */
   public String getTransact(HttpServletRequest request,
                             HttpServletResponse response, ModelMap model, Integer page, Integer size,
                             @PathVariable Long uid, String mobile, String productName, Long startAt, Long endAt,
                             String name, Long scene, Long status) throws Exception {

/**打印日志输出这些数据*/
       log.info("get transact list page=" + page + "size=" + size + "uid=" + uid + "mobile=" + mobile + "productName=" + productName +
"startAt=" + startAt + "endAt=" + endAt + "name=" + name + "scene=" + scene + "status=" + status);

       /**页数和每页数值判空*/
       if (page == null) {
page = 1;
       }
if (size == null) {
size = 10;
       }
int start = (page - 1) * size;
       if (start < 0) {
start = 0;
       }


/**输出共有多少条信息和每一页的个数*/
       log.info("pageList : page =" + start + ",size=" + size);

       try {
User user = userService.getObjectById(uid);

           /**调用一个自己写的对应接口的工具类,新建一个名为map的Map数组*/
           Map<String, Object> map = TransactUtil.getTransactUtil(mobile, productName, startAt, endAt,
                   name, scene, status,uid, false);
           /**打印此map数组*/
           log.info(" map=" + map);

           /**调用TransactService接口的父类接口,getIdsByDynamicCondition方法,new一个只装Long类型的list集合,命名为transactIds.
            *传入四个值,第一个是class类型,第二个是Map类型,第三个和第四个是integer类型,具体用途不知,据说偏底层,可以不用管
            */
           List<Long> ids = transactService.getIdsByDynamicCondition(Transact.class, map, start, size);

           /**调用TransactService接口的getObjectsByIds方法,该方法参数为一个list集合,把刚刚new的transactIds集合放进去,取名为transacts.
            *有什么用不知道,只知道集合里的装的泛型从Long变成transact(user)类
            */
           List<Transact> transactList = new ArrayList<>();

           /**新建一个名为userMap的hashmap
           /*hashmap对多线程来说不安全,但是可以以null为key值
            */
           Map<Long, Product> productHashMap = new HashMap<>();

           /**这个isEmpty方法会返回一个boolean的数值,如果为空就返回true*/
           if (CollectionUtils.isEmpty(ids)) {
log.info("transactIds size=0");
           } else {
/**如果不为空,打出这个transactIds这个集合的长度*/
               log.info("transactIds size=" + ids.size());

               /**给集合transacts赋值,调用getObjectByIds方法,传的参数是transactIds这个集合
                * 原方法里显示ids,看不懂这种ids是什么意思
                * */
               transactList= transactService.getObjectsByIds(ids);

               /**new一个ArrayList集合*/
               List<Long> productIds = new ArrayList<>();
               /**高级for循环
                * 遍历交易表的所有userId
                * */
               for (Transact transact : transactList) {
productIds.add(transact.getProductId());
               }

/**搞一个users的list集合*/
               if(productIds.size()>0){
List<Product> products = productservice.getObjectsByIds(productIds);
               for (Product product : products) {
/**调用hashmap的put方法,循环遍历users集合
                    *hashmap的put方法映射关联的指定键和值
                    *分别是user里的id和user一整个类?
                    *  */
                   productHashMap.put(product.getId(), product);
               }
}
}
map = TransactUtil.getTransactUtil(mobile, productName, startAt, endAt, name, scene,
                   status, uid,true);
           BigInteger total = (BigInteger) transactService.getObjectByDynamicCondition(Transact.class, map
, 0, Integer.MAX_VALUE);
           log.info("total=" + total);
           int totalPage = 1;
           if (total.intValue() > 0) {
totalPage = (((total.intValue() - 1)) / (size)) + 1;
           }
log.info("totalPage=" + totalPage);

           model.addAttribute("code", 0);
           model.addAttribute("page", page);
           model.addAttribute("size", size);
           model.addAttribute("total", total);
           model.addAttribute("totalPage", totalPage);
           model.addAttribute("productHashMap", productHashMap);
           model.addAttribute("user", user);

       } catch (Throwable t) {
t.printStackTrace();
           log.error(t.getMessage());
           log.error("get feedBack list error,page is" + start + ",size" + size);
           model.addAttribute("code", -1);
       }

return "/playboy-invest-service/transact/json/transactListJson";
   }
}


明天计划的事情:

还未完整完成,明天完成

尽量开始测试


遇到的问题:

list,map集合的具体概念,已看书得到解决


收获:

1:/**

*/

可以把注释变成原谅色,更显眼,不过这样很容易划分开代码,所以我打算把这个交易记录作为范例,其他的就不这么搞了


2:ArrayList

ArrayList是线程不安全的,如果有超过一个线程修改了ArrayList集合,则程序必须手动保证该集合的同步性,但vector集合则是线程安全的
同理,arraylist的性能较高
和hashmap不同的是,即使需要保证list集合线程安全,也不要用vector,因为会有一个collections工具类,将arraylist变成线程安全的


3:hashmap

hashmap是map接口的经典实现
1:hashmap相对线程不安全,但性能比较高.如多线程,用hashtable会好一点
2:hashmap允许使用null作为key或value



4:mybatis-plus

看了一会,公司并没有使用spring,所以只是简单知道了有这么个东西,传说中的先马后看

http://www.cnblogs.com/xuyatao/p/6962680.html


返回列表 返回列表
评论

    分享到