数据从数据库到java代码再到前台页面展示流程

数据从数据库到java代码再到前台页面展示流程

(以线索管理模块为例)

somao_api >>>APP 端
somao_common >>>工具类
somao_domain >>>实体类
somao_mybatis >>>dao impl service
somao_web >>>{java–>controller}、{resources—>xml}、{webapp—>html css..}

1.实体类domain

public class Clue{
    ...
}

2.接口dao

public interface ClueDao{
    ...
}

3.mapper xml 文件

ClueMapper.xml--->sql语句

4.实现类 daoImpl

@Repository     //用于标注数据访问组件,即DAO组件
@SuppressWarnings("unchecked")      //作用是给编译器一条指令,告诉它对呗批注的代码元素内部的某些警告保持静默

public class ClueDaoImpl implements ClueDao{

  @Autowired        //自动注入
  private DaoSupport dao;

  @Override
  public List findAll(Page page) {
    return (List) dao.findForList("ClueMapper.listPageaClue", page);
    //ClueMapper xml 文件的 listPageaClue方法的sql 语句到数据库查询
  }
}

5.服务类 service

@Service    //告知spring 创建一个实现类的实例(有了这个可以不用再spring.xml 文件中配置bean)
@Transactional      //给类添加事物 表示该类里面的所有方法的事物由spring 处理 
//来保证事物的原子性 对数据库进行操作失败spring负责回滚操作 成功提交操作
public class ClueService{

  @Autowired
  private ClueDao clueDao;

  ...
}

6.控制器 Controller

@Controller     //定义一个控制器类
@RequestMapping("/crm/clue")    //处理请求地址映射的注解 用于类上 表示勒种所有响应请求的方法都是以
//改地址作为父路径  /crm/clue/..
public class ClueController extends BaseController{

  @Autowired
  public ClueDao clueDao;

  @RequestMapping("search")-->/crm/clue/search
  @ResponseBody
  JSONObject search(HttpServletRequest request, HttpServletResponse response, Page page) {
        JSONObject json = new JSONObject();
        System.out.println();
        String keyword = request.getParameter("keyword");
        String pageNo = request.getParameter("page");
        String size = request.getParameter("size");
        User user = (User) request.getSession().getAttribute("fuser");
        String accountsArr=request.getParameter("accountsArr");
        String contactTypeArr=request.getParameter("contactTypeArr");
        String levelArr=request.getParameter("levelArr");
        List str=null;
        List contactStr=null;
        List levelStr=null;
        List pageresult = null;
        if (!StringUtils.isEmpty(page) && !StringUtils.isEmpty(size)) {
            PageData pd = new PageData();
            page.setCurrentPage(Integer.parseInt(pageNo));
            page.setShowCount(Integer.parseInt(size));
            pd.put("userId", user.getId());
            pd.put("userCmpyId", user.getCmpyId());

            if(keyword.equals("待联系")){
                pd.put("progress", 0);
            }else if(keyword.equals("联系中")){
                pd.put("progress", 1);
            }else if(keyword.equals("转客跟进")){
                pd.put("progress", 2);
            }else if(keyword.equals("成单")){
                pd.put("progress", 3);
            }else if(keyword.equals("放弃")){
                pd.put("progress", 4);
            }else if(keyword.equals("搜猫推荐")){
                pd.put("type", 1);
            }else if(keyword.equals("用户自建")){
                pd.put("type", 2);
            }else{
                pd.put("keyword", keyword);

            }

            pd.put("userType", user.getUserType());
            pd.put("userGroupId", user.getUserGroupId());

            //封装负责人集合
            if((accountsArr!="")&&(accountsArr!=null)){
                if(accountsArr.indexOf(',')==-1){
                    str=new ArrayList<>();
                    str.add(accountsArr);
                }else{
                    String[] arr=accountsArr.split(",");
                    str=Arrays.asList(arr);
                }
                //
            }

            //封装等级集合
            if((levelArr!="")&&(levelArr!=null)){
                if(levelArr.indexOf(',')==-1){
                    levelStr=new ArrayList<>();
                    levelStr.add(levelArr);
                }else{
                    String[] arr=levelArr.split(",");
                    levelStr=Arrays.asList(arr);
                }
            }

            //封装联系状态集合
            if((contactTypeArr!="")&&(contactTypeArr!=null)){
                if(contactTypeArr.indexOf(',')==-1){
                    contactStr=new ArrayList<>();
                    contactStr.add(contactTypeArr);
                }else{
                    String[] arr=contactTypeArr.split(",");
                    contactStr=Arrays.asList(arr);
                }
            }
            pd.put("levelStr", levelStr);
            pd.put("contactStr", contactStr);
            pd.put("ids", str);
            page.setPd(pd);
            pageresult = clueDao.findAll(page);

        }

        json.put("content", pageresult);
        json.put("page", page);
        return json;
    }
}

============

CrmMainController.java

@RequestMapping("cluemanage")
public ModelAndView cluemanage(HttpServletRequest request,HttpServletResponse response) {
    ModelAndView mv=this.getModelAndView("cluemanage", JModelAndView.CRM_PATH, request, response);
        return mv;
    }

7.页面 html

cluemanage.html
<td colspan="11">没有记录,请更换查询条件,<button class='btn' ng-click="init();">重置搜索条件button>td>

8.JavaScript js

ClueController.js

4.  $scope.getcluelist = function(page,size,srckeyword){
        $scope.keyword = srckeyword;
5.      var url = 'crm/clue/search?';  [*//进入ClueController  crm/clue/search 这个方法*]
        var t_url = url + 'page=' + page + '&size=' + size+'&keyword=' +  
        ($scope.keyword||""+'&accountsArr='+ 
        ($scope.accountsArr))+'&contactTypeArr='+ 
        ($scope.contactTypeArr)+'&levelArr='+ 
        ($scope.levelArr);

        t_url=encodeURI(t_url); 

        $http.get(t_url).success(function (data) {

            console.log(data);

            if(data.content.length>0){//有数据直接展示
                $scope.clues = data.content;
                $scope.page = data;
                $scope.page.size = data.page.showCount;
                $scope.page.url = url;
                $scope.page.first = data.page.first;
                $scope.page.last = data.page.last;
                $scope.page.number = data.page.currentPage;
                $scope.page.numberOfElements = data.content.length;
                $scope.page.totalElements = data.page.totalResult;
                $scope.page.totalPages = data.page.totalPage;
                $scope.currentPage=data.page.currentPage;
            }else{//查询无数据,但总页数大于零,单页删除最后一条情况,返回上一页,尤其在查询时候需要
                if(data.page.totalPage>0){
                    $scope.page.pagefunction($scope.page.number-1,$scope.page.size);
                }else{//显示无数据
                    $scope.clues = data.content;
                    $scope.page = data;
                    $scope.page.size = data.page.showCount;
                    $scope.page.url = url;
                    $scope.page.first = data.page.first;
                    $scope.page.last = data.page.last;
                    $scope.page.number = data.page.currentPage;
                    $scope.page.numberOfElements = data.content.length;
                    $scope.page.totalElements = data.page.totalResult;
                    $scope.page.totalPages = data.page.totalPage;
                    $scope.currentPage=data.page.currentPage;
                }
            }
            $scope.page.pagefunction = function(page, size){
                var deferred = $q.defer();
                $http({
                    method: "GET", 
                    'url': url + 'page=' + page + '&size=' + size+ '&keyword=' +  
                    ($scope.keyword||"")+ 
                    '&accountsArr='+ 
                    ($scope.accountsArr)+'&contactTypeArr='+  
                    ($scope.contactTypeArr)+'&levelArr='+ 
                    ($scope.levelArr)
                })
                .success(function(data, status) {
                    deferred.resolve(data);
                });
                deferred.promise.then(function (data) {
                    $scope.clues = data.content;
                    $scope.page.size = data.page.showCount;
                    $scope.page.url = url;
                    $scope.page.first = data.page.first;
                    $scope.page.last = data.page.last;
                    $scope.page.number = data.page.currentPage;
                    $scope.page.numberOfElements = data.content.length;
                    $scope.page.totalElements = data.page.totalResult;
                    $scope.page.totalPages = data.page.totalPage;
                    $scope.currentPage=data.page.currentPage;
                }, function (data) {
                    console.log(data);
                });
            }

        });
    }

2.  $scope.init = function(){
        $scope.accountsArr='';
        $scope.contactTypeArr='';
        $scope.levelArr='';
3.      $scope.getcluelist(0,10,'');
        $http.get('crm/potentuser/findProvince').success(function (data) {
            $scope.provinces=data;
        });
        $("#searchContactType").find("ul li").each(function(){
            $(this).removeClass("checked");
        });
    }
1.  $scope.init();

你可能感兴趣的:(交互)