SpringMVC+hibernate+mysql中要注意的问题——特别是注释的问题

来源:互联网 发布:网络id 编辑:程序博客网 时间:2024/05/21 00:53

SpringMVC里的model文件一般写法这样

package cn.com.cisec.hnjt.model;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToOne;import org.codehaus.jackson.annotate.JsonIgnoreProperties;@Entitypublic class WorkOrderAll {private int id;private Warning waring;private String  time;private int count;private String name;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}@OneToOne(fetch=FetchType.EAGER)public Warning getWaring() {return waring;}public void setWaring(Warning waring) {this.waring = waring;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}}
SpringMVC中的Dao一般这样写

package cn.com.cisec.hnjt.dao;import java.util.List;import cn.com.cisec.hnjt.model.WorkOrderAll;public interface WorkOrderAllDao {  List<WorkOrderAll> getWorkOrderAll();}

SpringMVC里的DaoImpl这样写

package cn.com.cisec.hnjt.dao.impl;import java.util.List;import javax.annotation.Resource;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.stereotype.Repository;import cn.com.cisec.hnjt.dao.WorkOrderAllDao;import cn.com.cisec.hnjt.model.WorkOrderAll;@Repositorypublic class WorkOrderAllDaoImpl implements WorkOrderAllDao {private SessionFactory sessionFactory;private Session session;public SessionFactory getSessionFactory() {return sessionFactory;}@Resourcepublic void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}@Overridepublic List<WorkOrderAll> getWorkOrderAll() {session=sessionFactory.openSession();List<WorkOrderAll> workOrderAll=session.createQuery("from WorkOrderAll").list();return workOrderAll;}}


SpringMVC里的Service这样写

package cn.com.cisec.hnjt.service;import java.util.List;import cn.com.cisec.hnjt.model.TrafficLight;import cn.com.cisec.hnjt.model.WorkOrderAll;public interface WorkOrderAllService {List<WorkOrderAll> getWorkOrderAll();}


SpringMVC里的ServiceImpl这样写

package cn.com.cisec.hnjt.service.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;import cn.com.cisec.hnjt.dao.WorkOrderAllDao;import cn.com.cisec.hnjt.model.WorkOrderAll;import cn.com.cisec.hnjt.service.WorkOrderAllService;@Servicepublic class WorkOrderAllServiceImpl implements WorkOrderAllService {private WorkOrderAllDao workOrderAllDao;public WorkOrderAllDao getWorkOrderAllDao() {return workOrderAllDao;}@Resourcepublic void setWorkOrderAllDao(WorkOrderAllDao workOrderAllDao) {this.workOrderAllDao = workOrderAllDao;}public List<WorkOrderAll> getWorkOrderAll(){return workOrderAllDao.getWorkOrderAll();}} 

SpringMVC里的controller这样写

package cn.com.cisec.hnjt.control;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFPalette;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.portlet.bind.annotation.ResourceMapping;import cn.com.cisec.hnjt.model.Manager;import cn.com.cisec.hnjt.model.TrafficLight;import cn.com.cisec.hnjt.model.Warning;import cn.com.cisec.hnjt.model.WorkOrderAll;import cn.com.cisec.hnjt.service.WorkOrderAllService;@Controller@RequestMapping("/gdxq")public class WorkOrderAllController {private WorkOrderAllService workOrderAllService;private SessionFactory sessionFactory;private static String filename="导出Excel";public SessionFactory getSessionFactory() {return sessionFactory;}@Resourcepublic void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}public WorkOrderAllService getWorkOrderAllService() {return workOrderAllService;}@Resource(name="workOrderAllServiceImpl")public void setWorkOrderAllService(WorkOrderAllService workOrderAllService) {this.workOrderAllService = workOrderAllService;}@ResponseBody@RequestMapping("/getworkOrderAll")public Map<String,Object> getWorkOrderAll(){Map<String, Object> map = new HashMap<String, Object>();List<WorkOrderAll> list = workOrderAllService.getWorkOrderAll();List<TempWorkOrderAll> tempList = new ArrayList<TempWorkOrderAll>(); Iterator i=list.iterator(); while(i.hasNext()){ WorkOrderAll workOrderAll=(WorkOrderAll) i.next(); TempWorkOrderAll temp = new TempWorkOrderAll(); temp.setId(workOrderAll.getId()); temp.setCount(workOrderAll.getCount()); temp.setTime(workOrderAll.getTime());    TrafficLight t=workOrderAll.getWaring().getTrafficLight();       String address=t.getAddress();       int number =t.getSerialNumber();       temp.setAddress(address) ;       temp.setNumber(number);       String name="";     Iterator il= t.getManager().iterator();     while(il.hasNext()){     Manager manager=(Manager) il.next();     name=name+manager.getName()+",";     }     temp.setName(name);     tempList.add(temp); } //把 id,number,name,count,time,address封装到list里map.put("workOrderAll",tempList);return map;}@ResponseBody@RequestMapping("/exportExcel")public String exportExcel(String ids,HttpServletRequest request) throws Exception{String hql="FROM WorkOrderAll WHERE id IN ('"+ids+"')";Session session=sessionFactory.openSession(); List list=session.createQuery(hql).list();  Iterator i=list.iterator();  Workbook wb=new HSSFWorkbook();Sheet sheet=wb.createSheet("故障表单");// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheetRow row2=sheet.createRow(0);Row row =sheet.createRow((int)1);//第二部在sheet中添加表头的第1行,注意老版本的poi对Excel的行数列数有限制sheet.setColumnWidth((short)1,10 * 256);//设置第2列的列宽sheet.setColumnWidth((short)2,30 * 256);//设置第3列的列宽sheet.setColumnWidth((short)3,30 * 256);//设置第4列的列宽sheet.setColumnWidth((short)4,30 * 256);//设置第5列的列宽sheet.setColumnWidth((short)5,30 * 256);//设置第6列的列宽row2.setHeight((short) (3*255));//第一行的高度row.setHeight((short) (2*255));//第二行的高度CellStyle style=wb.createCellStyle();//第四部,创建单元格,并设置表头居中CellStyle style1=wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//创建一个居中的格式style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//创建一个居中的格式style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));/* * 定制所需要的excel样式 * 不要的话可以删除 * 设置单元格字体 */Font headerFont=wb.createFont();headerFont.setFontHeightInPoints((short) 10);headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);headerFont.setColor(HSSFColor.GREEN.index);//绿色的列标题headerFont.setFontName("宋体");//字体的样式为宋体Font font=wb.createFont();font.setFontHeightInPoints((short) 12);  //字体的大小为12号字体font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体增粗style1.setFont(font);style1.setWrapText(true);style.setFont(headerFont);style.setWrapText(true);//设置单元格边框及颜色style.setBorderBottom((short)1);style.setBorderLeft((short)1);style.setBorderRight((short)1);style.setBorderTop((short)1);style.setWrapText(true);HSSFPalette cellPalette = ((HSSFWorkbook) wb).getCustomPalette(); // 创建颜色 这里创建的是绿色边框cellPalette.setColorAtIndex(HSSFColor.GREEN.index, (byte)102, (byte)205, (byte)170); // 设置 RGBstyle.setLeftBorderColor(HSSFColor.GREEN.index); // 设置边框颜色style.setRightBorderColor(HSSFColor.GREEN.index);style.setTopBorderColor(HSSFColor.GREEN.index);style.setBottomBorderColor(HSSFColor.GREEN.index);//---------------------------------------------------Cell cell1=row2.createCell(0);cell1.setCellValue("导出Excel");cell1.setCellStyle(style1);Cell cell=row.createCell(0);cell.setCellValue("序号");cell.setCellStyle(style);cell=row.createCell(1);cell.setCellValue("编号");cell.setCellStyle(style);cell=row.createCell(2);cell.setCellValue("地点");cell.setCellStyle(style);cell=row.createCell(3);cell.setCellValue("时间");cell.setCellStyle(style);cell=row.createCell(4);cell.setCellValue("完成时间");cell.setCellStyle(style);cell=row.createCell(5);cell.setCellValue("维修人");cell.setCellStyle(style);int m=0;int n=0;  while(i.hasNext()){  WorkOrderAll workOrderAll=(WorkOrderAll) i.next();   Date currentTime=new Date();   SimpleDateFormat formatter=new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");String dateString=formatter.format(currentTime);Row row1=sheet.createRow(m+2);row1.setHeight((short) (2*255));//第三行的高度row1.createCell(0).setCellValue(workOrderAll.getId());   //序号        Warning w=workOrderAll.getWaring();                      //TrafficLight t=w.getTrafficLight();int number=t.getSerialNumber();String address=t.getAddress();row1.createCell(1).setCellValue(number);                   //序号row1.createCell(2).setCellValue(address);                  //地址row1.createCell(3).setCellValue(dateString);               //时间row1.createCell(4).setCellValue(workOrderAll.getTime());   //完工时间Iterator il=t.getManager().iterator();String name="";while(il.hasNext()){Manager manager=(Manager) il.next();name=manager.getName();}row1.createCell(5).setCellValue(name);                    //维修人员m++;  }  String fileName=System.currentTimeMillis()+filename+".xls"; String path=request.getSession().getServletContext().getRealPath("/")+"file"+"\\"+fileName;// 导出到服务器的位置FileOutputStream fileOut=new FileOutputStream(path);wb.write(fileOut);fileOut.close();System.out.println(path);return "success";}}class TempWorkOrderAll{int id;int count;String time;String address;int number;String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}}


0 0
原创粉丝点击