JAVA高亮代码测试

package edu.whut.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.whut.dao.IStationDAO;
import edu.whut.model.Station;

/**
 * StationDAOImpl类(数据访问层),实现IStationDAO接口提供的各种方法
 * @author WangWei
 * @since 2012/07/23 v1.0
 */
@Transactional
@Repository(value = "stationDAO")
public class StationDAOImpl implements IStationDAO {
	@Autowired
	private SessionFactory sessionFactory;
	/**
	 * 根据监测站id查询监测站
	 * @param id	监测站id
	 * @return
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Station findById(String id){
		Query query = sessionFactory.getCurrentSession().createQuery("from Station where id='"+id+"'");
		List<Station> stationList =  query.list();
		if(stationList.size()==0)
			return null;
		Station rsStation = stationList.get(0);
		return rsStation;
	}
	/**
	 * 查询所有分汽缸出口监测站
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Station> findMainStation(){
		Query query = sessionFactory.getCurrentSession().createQuery("from Station where inPipe=1 order by tstamp");
		List<Station> stationList =  query.list();
		return stationList;
	}
	/**
	 * 根据监测站编号查询监测站
	 * @param number	监测站编号
	 * @return
	 */
	
	@Override
	@SuppressWarnings("unchecked")
	public Station findByNumber(String number){
		Query query = sessionFactory.getCurrentSession().createQuery("from Station where number='" + number + "'");
		List<Station> stationList =  query.list();
		if(stationList.size()==0)
			return null;
		Station station = stationList.get(0);
		return station;
	}
	/**
	 * 根据父监测站id查询所有子监测站
	 * @param parentId	父监测站id
	 * @return
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<Station> findChildByParent(String parentId){
		Query query = sessionFactory.getCurrentSession().createQuery("from Station where parentID='"+parentId+"' order by number");
		List<Station> stationList =  query.list();
		return stationList;
	}
	/**
	 * 保存Station对象
	 * @param station
	 */
	@Override
	public void save(Station station){
		sessionFactory.getCurrentSession().saveOrUpdate(station);
	}
	
	/**
	 * 删除Station对象
	 * @param number String类型,监测站Id
	 */
	@Override
	public void delete(String stationId) {
		/*不从数据库中真正删除,仅仅将state字段设置为0,同时删除实时表中该测站的相应数据*/
		Station station = (Station)sessionFactory.getCurrentSession().get(Station.class, stationId);
		station.setState((short)0); //将监测站表中state字段设置为0
		String HQL = "delete from Realinfo where wellId = '" + station.getNumber() + "'";
		sessionFactory.getCurrentSession().createQuery(HQL).executeUpdate(); //删除实时表中数据
	}
	
	/**
	 * 分页查询监测单位
	 * @param pageSize 每页显示数据条数
	 * @param startRow 该页的起始位置
	 * @param HQL 执行的HQL语句
	 * @return stationList List<Station>对象,监测站列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<HashMap<String, Object>> queryStation(int pageSize, int startRow, String HQL) {
		List<HashMap<String,Object>> mapList = new ArrayList<HashMap<String,Object>>();
		Query query = sessionFactory.getCurrentSession().createQuery(HQL);
		query.setMaxResults(pageSize);
		query.setFirstResult(startRow);
		List list = query.list();
		for(int i = 0;i < list.size();i++){
			Station station = new Station();
			String unitName = null;
			HashMap<String, Object> map = new HashMap<String,Object>();
		    Object[] obj =(Object[])list.get(i);
		    station =(Station)obj[0];
		    unitName = (String)obj[1];
		    map.put("station", station);
		    map.put("unitName", unitName);
		    mapList.add(map);
		}
		return mapList;
	}
	
	/**
	 * 更新监测站
	 */
	@Override
	public void update(Station station) {
	     sessionFactory.getCurrentSession().update(station);
	}
	
	 /**
     * 查询数据个数
     * @param HQL  查询的HQL语句
     * @return count  查询数据个数
     */
	@Override
	public int getRows(String HQL) {
		String[] HQLPart = HQL.split("from");
		String[] HQLPart2 = HQLPart[1].split("order by");
		String countHQL = "select count(*) from "+HQLPart2[0];
		Query query = sessionFactory.getCurrentSession().createQuery(countHQL);
		int count = ((Long)query.uniqueResult()).intValue();
		return count;
	}
	
	/**
	 * 根据监测站名称查找监测站
	 * @param name 监测站名称
	 * @return station Station对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Station queryByName(String name) {
		String HQL = "from Station where name = :name";
		Query query = sessionFactory.getCurrentSession().createQuery(HQL);
		query.setString("name", name);
		List<Station> stationList = query.list();
		if(stationList.size() == 0)
			return null;
		return stationList.get(0);
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> queryAll() {
		String HQL = "select s.id,s.name from Station as s";
		Query query = sessionFactory.getCurrentSession().createQuery(HQL);
		List<Object[]> stationList = query.list();
		if(stationList.size() > 0)
			return stationList;
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Object> queryValue(String HQL) {
		Query query = sessionFactory.getCurrentSession().createQuery(HQL);
		List<Object> list = query.list();
		if(list.size() > 0)
			return list;
		return null;
	}
	/**
	 * 查询所有监测站的详细信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Station> queryAllStation() {
		String HQL = "from Station order by number asc, gx desc";//select new Station(id,parnetid,number,name,cpuModuleId,trandModuleId,commMode,gx) 
		Query query = sessionFactory.getCurrentSession().createQuery(HQL);
		List<Station> list = query.list();
		if(list.size() > 0)
			return list;
		return null;
	}
	
}

你可能感兴趣的:(JAVA高亮代码测试)