项目开发-DAY7

开发流程

大气监测数据管理

jsp + controller + service + dao
建议
从业务入手,先写业务处理层的接口,确定页面有哪些业务
1.查询业务:根据检测日期查询大气的监测数据,查找 b_airmonitoring条件为deleteflag='N'
2.新增业务:保存b_airmonitoring数据
3.修改业务:通过id查询b_airmonitoring数据,现实在修改页面
4.修改保存:b_airmonitoring更新操作
5.删除:批量删除

分析Dao应该有哪些方法,也就是说对b_airmonitoring有哪些操作

1.实现b_airmonitoring的Dao层操作

1.通过逆向工程。生成b_airmonitoring的实体类、映射文件、绑定接口
在intelagr工程中配置generatorConfig.xml配置连接的数据、实体类的包名、映射文件、绑定接口的包名、生成的表名
2.运行GeneratorSqlmap.java类 将生成的文件导入 intelager项目中

2.编写大气监测业务接口

public interface IAirmoniService {
    /**
     * 
     * @param pageModel 封装了分页信息,包括当前的页数,每页记录数、总页数、查询结果list集合
     * @param map 封装了查询条件
     */
    public void query(PageModel pageModel,Map map);
    public void save(BAirmonitoring airmoni);
    public BAirmonitoring queryById(Integer id);
    public void update(BAirmonitoring airmoni);
    public void delete(int[] ids);
}

3.实现大气监测业务类

BAirmonitoringMapper.xml

  
   delete from b_airmonitoring where id in
   
   #{item}
   
  

BAirmonitoringMapper.java

  void deleteBatch(int[] ids);

AirmoniServiceImpl.java

package com.oracle.intelagr.service.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oracle.intelagr.common.PageModel;
import com.oracle.intelagr.entity.BAirmonitoring;
import com.oracle.intelagr.entity.BAirmonitoringExample;
import com.oracle.intelagr.entity.BAirmonitoringExample.Criteria;
import com.oracle.intelagr.mapper.BAirmonitoringMapper;
import com.oracle.intelagr.service.IAirmoniService;
@Service
public class AirmoniServiceImpl implements IAirmoniService {
    @Autowired
    private BAirmonitoringMapper airmoniMapper;
    @Override
    public void query(PageModel pageModel, Map map) {
        //按条件查询 监测日期的区间 两个值存储在map参数中
        BAirmonitoringExample example = new BAirmonitoringExample();
        Criteria criteria = example.createCriteria();
        //判断map中是否有开始时间的条件,如果有拼在criteria中
        if(map.get("startMonitorDate")!=null) {
            criteria.andMonitordateGreaterThanOrEqualTo((Date)map.get("startMonitorDate"));
        }
        if(map.get("endMonitorDate")!=null) {
            criteria.andMonitordateLessThanOrEqualTo((Date)map.get("endMonitorDate"));
        }
        //开启分页拦截
        PageHelper.startPage(pageModel.getPage(),pageModel.getPageSize());
        //查询获得了集合
        List list = airmoniMapper.selectByExample(example);
        //将集合、总记录数、总页数封装在pageModel对象中
        PageInfo pageInfo = new PageInfo(list);
        pageModel.setResult(list);
        pageModel.setTotalCount((int)pageInfo.getTotal());
    }

    @Override
    public void save(BAirmonitoring airmoni) {
        airmoniMapper.insert(airmoni);
    }

    @Override
    public BAirmonitoring queryById(Integer id) {
        return airmoniMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(BAirmonitoring airmoni) {
        airmoniMapper.updateByPrimaryKey(airmoni);
    }

    @Override
    public void delete(int[] ids) {
//      if(ids!=null) {
//          for(int id:ids) {
//              airmoniMapper.deleteByPrimaryKey(id);
//          }
//      }
        airmoniMapper.deleteBatch(ids);
    }


}

添加pageHelper插件的依赖

            
     
            com.github.pagehelper
            pagehelper
            5.1.4
                          
  

applicationContext中配置分页插件

   
        
        
        
        
            
                
                    
                        
                        helperDialect=mysql
                        reasonable=true
                        autoRuntimeDialect=true
                        
                    
                
            
        
    

4.编写业务类的单元测试

1.导入Spring-test和junit(4.12)的依赖
2.在/src/test/java中编写一个测试类AirmoniServiceImplTest.java

package com.oracle.intelagr.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.oracle.intelagr.common.PageModel;
import com.oracle.intelagr.entity.BAirmonitoring;

//Spring框架的Junit单元测试类,将这个类注册在Spring中的IOC容器中
//Spring和Junit集成
@RunWith(SpringJUnit4ClassRunner.class)
//将该类注册在Spring的IOC容器中
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AirmoniServiceImplTest {
   //注入AirmoniService
    @Autowired
    private IAirmoniService airmoniService;
    //编写IAirmoniServic接口的save方法的测试方法
    @Test
    public void saveTest() {
        //编写测试用例的数据
        BAirmonitoring air = new BAirmonitoring();
        air.setMonitordate(new Date());
        air.setMonitorpointcode("7");
        air.setTsp(1.3f);
        air.setSo2(1.4f);
        air.setNo2(1.5f);
        air.setDeleteflag("N");
        air.setCreatedate(new Date());
        air.setCreateuserid("10");
        air.setUpdatedate(new Date());
        air.setUpdateuserid("10");
        airmoniService.save(air);
    }
    @Test
    public void updateTest() {
        BAirmonitoring air = new BAirmonitoring();
        air.setId(1);
        air.setMonitordate(new Date());
        air.setMonitorpointcode("7");
        air.setTsp(2.3f);
        air.setSo2(1.4f);
        air.setNo2(1.5f);
        air.setDeleteflag("N");
        air.setCreatedate(new Date());
        air.setCreateuserid("10");
        air.setUpdatedate(new Date());
        air.setUpdateuserid("10");
        airmoniService.update(air);
    }
    @Test
    public void queryByIdTest() {
        BAirmonitoring air = airmoniService.queryById(1);
        System.out.println(air.getMonitordate());
        System.out.println(air.getNo2());
        System.out.println(air.getMonitorpointcode());
    }
    @Test 
    public void deleteBatchTest() {
        int[] ids=new int[] {1,2};
        airmoniService.delete(ids);
    }
    @Test
    public void queryTest() {
        Map map = new HashMap();
        map.put("startMonitorDate", new Date(new Date().getTime()-1000*60*60));
        map.put("endMonitorDate", new Date());
        
        PageModel pageModel = new PageModel();
        pageModel.setPage(1);
        pageModel.setPageSize(4);
        
        airmoniService.query(pageModel,map);
        List list = (List)pageModel.getResult();
        for(BAirmonitoring air:list) {
            System.out.println(air.getId());
        }
        System.out.println("总页数: " + pageModel.getTotalPage());
    }
}

5.编写Controller和jsp

你可能感兴趣的:(项目开发-DAY7)