这里我们还是以房产信息管理系统的题目举例:
发现在DAO层和service层都有接口类(注:impl是实现类)
为什么要用接口,不直接写实现类:
1、简单、规范性:这些接口不仅告诉开发人员你需要实现那些业务,而且也将命名规范限制住了(防止一些开发人员随便命名导致别的程序员无法看明白)
2、便于维护、拓展:在不久将来,你突然发现这个类满足不了你了,然后你又要重新设计这个类,更糟糕是你可能要放弃这个类,那么其他地方可能有引用他,这样修改起来很麻烦,如果你一开始定义一个接口,把函数功能放在接口里,然后定义类时实现这个接口,然后你只要用这个接口去引用实现它的类就行了,以后要换的话只不过是引用另一个类而已,这样就达到维护、拓展的方便性。
3、安全、严密性:它描述了系统对外的所有服务,而不涉及任何具体的实现细节。这样比较安全、严密一些(一般软件服务商考虑的比较多)
DAO层接口类
package com.yan.web.dao; import com.yan.web.entity.AdminEntity; import com.yan.web.entity.Customer; import java.util.List; public interface AdminDao { AdminEntity getAdminByUserName(String userName); //注册 int addUser(AdminEntity adminEntity); List<Customer> adminList(String userName, Integer id); List<Customer> adminList(); int deleteAdminById(Integer id); int updateAdmin(AdminEntity admin); }
service层接口
package com.yan.web.service; import com.yan.web.entity.AdminEntity; /** * 管理服务 * * @author galax * @date 2022/10/19 */ public interface AdminService { /** * 被用户名admin * * @param userName 用户名 * @return {@link AdminEntity} */ AdminEntity getAdminByUserName(String userName); /** * 添加用户 * * @param adminEntity 管理实体 * @return boolean */// 注册 boolean addUser(AdminEntity adminEntity); }
DAO层实现类
AdminDaoImpl.java(其他dao类同理)【完整代码】
package com.yan.web.dao.Impl; import com.yan.web.dao.AdminDao; import com.yan.web.entity.AdminEntity; import com.yan.web.entity.Customer; import com.yan.web.util.jdbcUtil; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * @author chenshy * @date 2022/10/19 */ public class AdminDaoImpl implements AdminDao { /** * 被用户名admin * * @param userName 用户名 * @return {@code AdminEntity} */ @Override /*通过姓名获取输入的信息*/ public AdminEntity getAdminByUserName(String userName) { Connection connection = jdbcUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("select * from admin where user_name=?"); try { ps = connection.prepareStatement(stringBuilder.toString()); // 第一个?的参数 ps.setString(1, userName); rs = ps.executeQuery(); if (rs.next()) { int aid = rs.getInt("id"); String name = rs.getString("user_name"); String password = rs.getString("pwd"); int state = rs.getInt("state"); AdminEntity adminEntity = new AdminEntity(); adminEntity.setId(aid); adminEntity.setUserName(name); adminEntity.setPwd(password); adminEntity.setState(state); return adminEntity; } } catch (SQLException e) { e.printStackTrace(); } finally { jdbcUtil.ReleaseResource(connection, ps, rs); } return null; } /** * 添加用户 * * @param adminEntity 管理实体 * @return int */ @Override /*将获取的信息添加到数据库中*/ public int addUser(AdminEntity adminEntity) { Connection connection = jdbcUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("insert into admin(user_name,pwd,state) VALUES(?,?,?)"); try { ps = connection.prepareStatement(stringBuilder.toString()); ps.setString(1, adminEntity.getUserName()); ps.setString(2, adminEntity.getPwd()); ps.setInt(3, adminEntity.getState()); return ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { jdbcUtil.ReleaseResource(connection, ps, rs); } return 0; } @Override public List<Customer> adminList(String userName, Integer id) { Connection connection = jdbcUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("select * from customer"); try { ps = connection.prepareStatement(stringBuilder.toString()); // ps.setObject(1, "%" + userName + "%"); // ps.setObject(2, id); rs = ps.executeQuery(); List<Customer> list = new ArrayList<>(); while (rs.next()) { long ids = rs.getInt("UserID"); String userName1 = rs.getString("UserName"); String sex = rs.getString("sex"); String IdNumber = rs.getString("IdNumber"); String Phone = rs.getString("Phone"); String User = rs.getString("User"); int audit = rs.getInt("audit"); String password = rs.getString("password"); Customer customer = new Customer(); customer.setUserid(ids); customer.setUsername(userName1); customer.setSex(sex); customer.setIdnumber(IdNumber); customer.setPhone(Phone); customer.setUser(User); customer.setAudit(audit); customer.setPassword(password); list.add(customer); } return list; } catch (SQLException e) { e.printStackTrace(); } finally { jdbcUtil.ReleaseResource(connection, ps, rs); } return null; } public List<Customer> adminList() { Connection connection = jdbcUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("select * from customer"); try { ps = connection.prepareStatement(stringBuilder.toString()); // ps.setObject(1, "%" + userName + "%"); // ps.setObject(2, id); rs = ps.executeQuery(); List<Customer> list = new ArrayList<>(); while (rs.next()) { long ids = rs.getInt("UserID"); String userName1 = rs.getString("UserName"); String sex = rs.getString("sex"); String IdNumber = rs.getString("IdNumber"); String Phone = rs.getString("Phone"); String User = rs.getString("User"); int audit = rs.getInt("audit"); String password = rs.getString("password"); Customer customer = new Customer(); customer.setUserid(ids); customer.setUsername(userName1); customer.setSex(sex); customer.setIdnumber(IdNumber); customer.setPhone(Phone); customer.setUser(User); customer.setAudit(audit); customer.setPassword(password); list.add(customer); } return list; } catch (SQLException e) { e.printStackTrace(); } finally { jdbcUtil.ReleaseResource(connection, ps, rs); } return null; } @Override public int deleteAdminById(Integer id) { Connection connection = jdbcUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("delete from admin where id=?"); try { ps = connection.prepareStatement(stringBuilder.toString()); ps.setObject(1, id); return ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { jdbcUtil.ReleaseResource(connection, ps, rs); } return 0; } @Override public int updateAdmin(AdminEntity admin) { Connection connection = jdbcUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("update admin set user_name=?,pwd=?,state=? where id =?"); try { ps = connection.prepareStatement(stringBuilder.toString()); ps.setObject(1, admin.getUserName()); ps.setObject(2, admin.getPwd()); ps.setObject(3, admin.getState()); ps.setObject(4, admin.getId()); return ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { jdbcUtil.ReleaseResource(connection, ps, rs); } return 0; } }
下面我们来解读一下(下一篇)
标签:customer,ps,Javaweb,rs,stringBuilder,connection,关于,接口类,String From: https://www.cnblogs.com/gbrr/p/16886226.html