com,jsj,service,Impl====新闻发布系统(9)

该层从Web层获取数据,对DAO层进行操作,获取操作结果,最后再将结果反馈到Web层

AdminServiceImpl

 1 package com.jsj.service.Impl;
 2 
 3 import com.jsj.dao.AdminDao;
 4 import com.jsj.entity.Admin;
 5 import com.jsj.factory.DaoFactory;
 6 import com.jsj.service.AdminService;
 7 
 8 public class AdminServiceImpl implements AdminService {
 9     //
10     private AdminDao adminDao = (AdminDao) DaoFactory.getDao("AdminDao");
11 
12 
13 
14     @Override
15     public Admin login(String username, String password) {
16         try {
17             return adminDao.getAdminByPassword(username, password);
18         } catch (Exception e) {
19             return null;
20         }
21     }
22 }
View Code

NewCateServiceImpl

  1 package com.jsj.service.Impl;
  2 
  3 import com.jsj.dao.NewsCateDao;
  4 import com.jsj.dao.NewsDao;
  5 import com.jsj.entity.News;
  6 import com.jsj.entity.NewsCateVo;
  7 import com.jsj.entity.NewsCate;
  8 import com.jsj.factory.DaoFactory;
  9 import com.jsj.service.NewsCateService;
 10 import com.jsj.utils.JdbcUtils;
 11 
 12 import java.sql.SQLException;
 13 import java.util.ArrayList;
 14 import java.util.List;
 15 
 16 public class NewsCateServiceImpl implements NewsCateService {
 17 
 18     private NewsCateDao newsCateDao = (NewsCateDao) DaoFactory.getDao("NewsCateDao");
 19 
 20     private NewsDao newsDao = (NewsDao) DaoFactory.getDao("NewsDao");
 21 
 22     @Override
 23     public List<NewsCate> getAllCate() {
 24         try {
 25             return newsCateDao.getAll();
 26         } catch (Exception e) {
 27             return null;
 28         }
 29     }
 30 
 31     @Override
 32     public List<NewsCateVo> getAllNewsCateVo(Integer length) {
 33         try {
 34             List<NewsCateVo> allNewsCateVo = new ArrayList<>();
 35             List<NewsCate> newsAllCate = newsCateDao.getAll();
 36             for (NewsCate newsCate:newsAllCate ) {
 37                 NewsCateVo newsCateVo = new NewsCateVo();
 38                 newsCateVo.setCateId(newsCate.getId());
 39                 newsCateVo.setName(newsCate.getName());
 40                 newsCateVo.setNews(newsDao.getNewsListByCate(newsCate.getId(),0,length));
 41                 allNewsCateVo.add(newsCateVo);
 42             }
 43             return allNewsCateVo;
 44         }catch (Exception e){
 45             return null;
 46         }
 47     }
 48 
 49     @Override
 50     public NewsCate getNewsCateById(Integer id) {
 51         try {
 52             return newsCateDao.getById(id);
 53         } catch (Exception e) {
 54             return null;
 55         }
 56     }
 57 
 58     @Override
 59     public int insert(NewsCate newsCate) {
 60         try {
 61             return newsCateDao.insert(newsCate);
 62         } catch (Exception e) {
 63             return -1;
 64         }
 65     }
 66 
 67     @Override
 68     public int update(NewsCate newsCate) {
 69         try {
 70             return newsCateDao.update(newsCate);
 71         } catch (Exception e) {
 72             return -1;
 73         }
 74     }
 75 
 76     @Override
 77     public int delete(Integer id) {
 78         try {
 79             JdbcUtils.beginTransaction();
 80             List<News> newsList = newsDao.getNewsListByCate(id,0,newsDao.getCountByCate(id));
 81             for (News news:newsList) {
 82                 newsDao.deleteById(news.getId());
 83             }
 84             int res = newsCateDao.deleteById(id);
 85             if (res<1){
 86                 try {
 87                     JdbcUtils.rollbackTransaction();
 88                     return 0;
 89                 } catch (SQLException ignored) {}
 90             }
 91             JdbcUtils.commitTransaction();
 92             return res;
 93         } catch (Exception e) {
 94             try {
 95                 JdbcUtils.rollbackTransaction();
 96                 return 0;
 97             } catch (Exception ignored) {}
 98         }
 99         try {
100             return newsCateDao.deleteById(id);
101         } catch (Exception e) {
102             return -1;
103         }
104     }
105 }
View Code

 

NewsServiceImpl

  1 package com.jsj.service.Impl;
  2 
  3 import com.jsj.dao.NewsCateDao;
  4 import com.jsj.dao.NewsDao;
  5 import com.jsj.entity.News;
  6 import com.jsj.entity.NewsVo;
  7 import com.jsj.entity.Page;
  8 import com.jsj.factory.DaoFactory;
  9 import com.jsj.service.NewsService;
 10 
 11 import java.util.ArrayList;
 12 import java.util.List;
 13 
 14 public class NewsServiceImpl implements NewsService {
 15 
 16     private NewsDao newsDao = (NewsDao) DaoFactory.getDao("NewsDao");
 17 
 18     private NewsCateDao newsCateDao = (NewsCateDao) DaoFactory.getDao("NewsCateDao");
 19 
 20     @Override
 21     public Page<NewsVo> getNewsVoPage(Integer pageIndex, Integer pageSize) {
 22         Page <NewsVo> newsVoPage = new Page <>();
 23         newsVoPage.setPageIndex(pageIndex);
 24         newsVoPage.setPageSize(pageSize);
 25         newsVoPage.setBeanList(getNewsVoList((pageIndex - 1) * pageSize, pageSize));
 26         try {
 27             newsVoPage.setTotalCount(newsDao.getCount());
 28             newsVoPage.setTotalPage((int) Math.ceil((double) newsDao.getCount() / (double) pageSize));
 29         } catch (Exception e) {
 30             return null;
 31         }
 32         return newsVoPage;
 33     }
 34 
 35     @Override
 36     public Page<News> getNewsPageByCate(Integer cateId, Integer pageIndex, Integer pageSize) {
 37         Page<News> newsPage = new Page<> ();
 38         newsPage.setPageIndex(pageIndex);
 39         newsPage.setPageSize(pageSize);
 40         newsPage.setBeanList(getNewsListByCate(cateId,(pageIndex - 1) * pageSize,pageSize));
 41         try {
 42             newsPage.setTotalCount(newsDao.getCountByCate(cateId));
 43             newsPage.setTotalPage((int) Math.ceil((double) newsDao.getCountByCate(cateId) / (double) pageSize));
 44         } catch (Exception e) {
 45             return null;
 46         }
 47         return newsPage;
 48     }
 49 
 50     @Override
 51     public List<NewsVo> getNewsVoList(Integer pageIndex, Integer length) {
 52         List <NewsVo> newsVoList = new ArrayList<> ();
 53         try {
 54             List<News> newsList = newsDao.getNewsList(pageIndex, length);
 55             for (News news : newsList){
 56                 NewsVo newsVo = new NewsVo();
 57                 newsVo.setNewsCate(newsCateDao.getById(news.getCateId()));
 58                 newsVo.setTitle(news.getTitle());
 59                 newsVo.setContent(news.getContent());
 60                 newsVo.setAuthor(news.getAuthor());
 61                 newsVo.setTime(news.getTime());
 62                 newsVo.setId(news.getId());
 63                 newsVo.setCateId(news.getCateId());
 64                 newsVoList.add(newsVo);
 65             }
 66         } catch (Exception e) {
 67             return null;
 68         }
 69         return newsVoList;
 70     }
 71 
 72     @Override
 73     public List<News> getNewsListByCate(Integer cateId,Integer pageIndex,Integer length) {
 74         try {
 75             return newsDao.getNewsListByCate(cateId,pageIndex,length);
 76         } catch (Exception e) {
 77             return null;
 78         }
 79     }
 80 
 81     @Override
 82     public News getNewsById(Integer id) {
 83         try {
 84             return newsDao.getById(id);
 85         } catch (Exception e) {
 86             return null;
 87         }
 88     }
 89 
 90     @Override
 91     public int publish(News news) {
 92         try {
 93             return newsDao.insert(news);
 94         } catch (Exception e) {
 95             return -1;
 96         }
 97     }
 98 
 99     @Override
100     public List<NewsVo> getAllNewsVo() {
101         List<NewsVo> allNewsVo = new ArrayList<>();
102         try {
103             List<News> allNews = newsDao.getAll();
104             for (News news:allNews) {
105                 NewsVo newsVo = new NewsVo();
106                 newsVo.setId(news.getId());
107                 newsVo.setTitle(news.getTitle());
108                 newsVo.setAuthor(news.getAuthor());
109                 newsVo.setTime(news.getTime());
110                 newsVo.setContent(news.getContent());
111                 newsVo.setCateId(news.getCateId());
112                 newsVo.setNewsCate(newsCateDao.getById(news.getCateId()));
113                 allNewsVo.add(newsVo);
114             }
115             return allNewsVo;
116         } catch (Exception e) {
117             return null;
118         }
119     }
120 
121     @Override
122     public int deleteNewsById(Integer id) {
123         try {
124             return newsDao.deleteById(id);
125         } catch (Exception e) {
126             return -1;
127         }
128     }
129 }
View Code

 

UserServiceImpl

 1 package com.jsj.service.Impl;
 2 
 3 import com.jsj.dao.UserDao;
 4 import com.jsj.entity.User;
 5 import com.jsj.factory.DaoFactory;
 6 import com.jsj.service.UserService;
 7 
 8 public class UserServiceImpl implements UserService {
 9 
10     private UserDao userDao = (UserDao) DaoFactory.getDao("UserDao");
11 
12     @Override
13     public int register(User user) {
14         try {
15             return userDao.insert(user);
16         } catch (Exception e) {
17             return -1;
18         }
19     }
20 
21     @Override
22     public User login(String username, String password) {
23         try {
24             return userDao.getUserByPassword(username,password);
25         } catch (Exception e) {
26             return null;
27         }
28     }
29 
30 }
View Code

 

posted @ 2020-05-15 18:11  赚钱买个打印机  阅读(161)  评论(0编辑  收藏  举报