关于作业题目中service和dao一致的问题
业务类LibServiceImpl,此类中的方法形式与LibDaoImpl类完全一致,用于对LibDaoImpl中对应方法的调用是要写成下面这样吗?
public interface LibDao { public void regist(User user); public int login(String username, String password); public int isUserExist(String username); public void addBookCatgory(Long id, String catgoryName, String description); public List<Book> getBooksByCondition(String bookID, String bookName, String catgoryName); }
和
public interface LibService { public void regist(User user); public int login(String username, String password); public int isUserExist(String username); public void addBookCatgory(Long id, String catgoryName, String description); public List<Book> getBooksByCondition(String bookID, String bookName, String catgoryName); }
那如果我RegistServlet.java现在写了一些代码如下
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LibServiceImpl ls = new LibServiceImpl(); //后台校验 String username = request.getParameter("username"); String password = request.getParameter("password"); String phoneNumber = request.getParameter("phone"); String email = request.getParameter("email"); String unRegex = "[a-zA-Z_0-9]{3,12}"; String pswRegex = "[0-9]{6,12}"; String pnRegex = "[1][3578][0-9]{9}"; String emlRegex = "[a-zA-Z_0-9]{3,}@([a-zA-Z]+|\\d+)(\\.[a-zA-Z]+)+"; boolean flag1 = username.matches(unRegex); boolean flag2 = password.matches(pswRegex); boolean flag3 = phoneNumber.matches(pnRegex); boolean flag4 = email.matches(emlRegex); if(flag1 && flag2 && flag3 && flag4) { User user = new User(username,password,phoneNumber,email); ls.regist(user); } else { request.setAttribute("msg", "请输入正确的格式"); response.sendRedirect("/mook_library/pages/admin/regist.jsp"); } }
我的LibSeviceImpl.java我写成
package com.imooc.service.impl; import java.util.List; import com.imooc.bean.Book; import com.imooc.bean.User; import com.imooc.dao.impl.LibDaoImpl; import com.imooc.service.LibService; public class LibServiceImpl implements LibService{ private LibDaoImpl libDao = new LibDaoImpl(); @Override public void regist(User user) { int flag = libDao.isUserExist(user.getUsername()); if (flag == 1) { libDao.regist(user); } else { } } @Override public int login(String username, String password) { // TODO Auto-generated method stub return 0; } @Override public int isUserExist(String username) { // TODO Auto-generated method stub return 0; } @Override public void addBookCatgory(Long id, String catgoryName, String description) { // TODO Auto-generated method stub } @Override public List<Book> getBooksByCondition(String bookID, String bookName, String catgoryName) { // TODO Auto-generated method stub return null; } }
LibDaoImpl.java写成
package com.imooc.dao.impl; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.imooc.bean.Book; import com.imooc.bean.User; import com.imooc.dao.LibDao; public class LibDaoImpl implements LibDao{ private static final List<User> userDb = new ArrayList<User>(); private static final List<Map<String, Object>> catgory = new ArrayList<Map<String, Object>>(); private static final List<Book> books = new ArrayList<Book>(); @Override public void regist(User user) { userDb.add(user); } @Override public int login(String username, String password) { // TODO Auto-generated method stub return 0; } @Override public int isUserExist(String username) { int flag = 0; for(User user : userDb ) { if(user.getUsername().equals(username)) { flag = 1; return flag; } } return flag; } @Override public void addBookCatgory(Long id, String catgoryName, String description) { // TODO Auto-generated method stub } @Override public List<Book> getBooksByCondition(String bookID, String bookName, String catgoryName) { // TODO Auto-generated method stub return null; } }
感觉有点怪怪的,LibServiceImpl的isUserExist()有啥用,既然dao中已经写了
14
收起
正在回答
1回答
一般在开发中,我们都会有一个service层,通过service层去调用DAO中的方法,这个题目中要求一致,当然你不这么写也不会报错。只是一个默认的习惯。当然在这里你也可以不在LibServiceImpl中写isUserExist()方法。祝学习愉快。
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星