📄 articleservicehibernateimpl.java
字号:
package com.jsfabc.jsh.model.service.impl;
import com.jsfabc.jsh.model.exception.DbException;
import com.jsfabc.jsh.model.exception.UserException;
import com.jsfabc.jsh.model.service.ArticleService;
import org.springframework.dao.DataAccessException;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import com.jsfabc.jsh.model.bo.Article;
import com.jsfabc.jsh.model.bo.BuyBill;
import com.jsfabc.jsh.model.bo.Category;
import com.jsfabc.jsh.model.bo.Requisition;
import com.jsfabc.jsh.model.dao.ArticleDao;
import com.jsfabc.jsh.model.dao.BuyBillDao;
import com.jsfabc.jsh.model.dao.CategoryDao;
import com.jsfabc.jsh.model.dao.RequisitionDao;
import com.jsfabc.jsh.model.dao.RatifyDao;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class ArticleServiceHibernateImpl implements ArticleService{
//数据访问对象
private CategoryDao categoryDao;
private BuyBillDao buyBillDao;
private RequisitionDao requisitionDao;
private RatifyDao ratifyDao;
private ArticleDao articleDao;
//发送邮件
//声明MailSerder变量
private MailSender mailSender;
//声明SimpleMailMessage变量
private SimpleMailMessage message;
//注入MailSender
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
//注入SimpleMailMessage
public void setMessage(SimpleMailMessage message) {
this.message = message;
}
public void setCategoryDao(CategoryDao newValue){
this.categoryDao=newValue;
}
public void setBuyBillDao(BuyBillDao newValue){
this.buyBillDao=newValue;
}
public void setRequisitionDao(RequisitionDao newValue){
this.requisitionDao=newValue;
}
public void setRatifyDao(RatifyDao newValue){
this.ratifyDao=newValue;
}
public void setArticleDao(ArticleDao newValue){
this.articleDao=newValue;
}
//构造函数
public ArticleServiceHibernateImpl() {
}
public List findAllCategorys() throws UserException {
try {
List categorys=(List)categoryDao.findAllCategorys();
return categorys;
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public List findCategoryArticles(Category category) throws UserException {
try {
List categoryArticles=(List)categoryDao.findCategoryArticles(category);
return categoryArticles;
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public void saveRequisition(Requisition requisition,List buyBills) throws UserException{
try{
Integer requisitionId=requisitionDao.saveRequisition(requisition);
Requisition req=new Requisition();
req.setRequisitionId(requisitionId);
for(int i=0;i<buyBills.size();i++){
BuyBill buyBill=new BuyBill();
buyBill=(BuyBill)buyBills.get(i);
buyBill.setRequisition(req);
}
buyBillDao.saveBuyBills(buyBills);
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public List findRequisitionsForRatify() throws UserException{
try {
List requisitions=(List)requisitionDao.findRequisitionsForRatify();
return requisitions;
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public List findBuyArticles(Integer requisitionId) throws UserException{
try {
List buyArticles=(List)requisitionDao.findBuyArticles(requisitionId);
return buyArticles;
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public List findAllRatifys() throws UserException {
try {
List ratifys=(List)ratifyDao.findAllRatifys();
return ratifys;
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public void updateRequisitionRatify(Requisition requisition)throws UserException{
requisitionDao.updateRequisitionRatify(requisition);
}
public List findAllRequisitions() throws UserException {
try {
List requisitions=(List)requisitionDao.findAllRequisitions();
return requisitions;
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public Integer saveArticle(Article article)throws UserException {
try {
Integer articleId=(Integer)articleDao.saveArticle(article);
return articleId;
}
catch(DataAccessException daoe){
daoe.printStackTrace();
throw new DbException(daoe.getMessage());
}
}
public void sendMail(String email,String txt){
//声明一个简单邮件消息变量
SimpleMailMessage mailMsg = new SimpleMailMessage(this.message);
//为邮件消息设置邮件接收信箱
mailMsg.setTo(email);
//为邮件消息设置邮件正文
mailMsg.setText(txt);
try{
//发送邮件
mailSender.send(mailMsg);
}
//处理邮件发送异常
catch(MailException ex) {
//log it and go on
System.err.println(ex.getMessage());
}
}
public void uploadFile(String targetFile,byte[] bytes)
throws IOException{
//声明一个输出流变量,用于写上传目标文件
OutputStream bos;
try {
//将输出流示例化为一个文件输出流
bos = new FileOutputStream(targetFile);
//输出流写目标文件数据
bos.write(bytes);
//关闭输出流
bos.close();
}
//处理文件上传异常
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -