面向接口编程(Interface Oriented Programming:OIP)是一种编程思想,接口作为实体抽象出来的一种表现形式,用于抽离内部实现进行外部沟通,最终实现内部变动而不影响外部与其他实现交互,可以理解成按照这种思想来设计编程的方式就可以称为面向接口编程。 它并不是比面向对象编程更先进的一种独立的编程思想,可以说属于面向对象思想体系的一部分或者说它是面向对象编程体系中的思想精髓之一
用我的话说就是:用接口来解耦
Entity:@Data注解来自lombok 此处帮助简化了set(), get(),toString()等方法
@Data
public class Client {
private int clientId ;
private String clientName;
private String sex;
private int age;
public Client(int id, String clientName, String sex, int age) {
this.clientId = id;
this.clientName = clientName;
this.sex = sex;
this.age = age;
}
public Client(){
}
public Client(int clientId){
this.clientId = clientId;
}
}
Utils:
JdbcUtil.class:主要用于jdb的频繁连接和释放 *** 作
JDBC工具类
方法的命名:通常是和SQL语句有关 selectById updateById …
dao.ClientDao.java接口
/**
* function annotation:通过id查询指定Client
* TODO
* @param id
* @return Client
*/
Client selectById(Integer id) throws SQLException, ClassNotFoundException;
dao.impl.ClientDaoImpl.java 实现类
@Override
public Client selectById(Integer id) throws SQLException, ClassNotFoundException {
String sql = "select * from client_TB where clientId = ?";
//连接数据库
Connection connection = JdbcUtils.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, id);
ResultSet resultSet = ps.executeQuery();
Client client = new Client();
while (resultSet.next()) {
client.setClientId(resultSet.getInt(1));
client.setClientName(resultSet.getString(2));
//数据转化 birthday 转化成age
Date date = resultSet.getDate(3);
int age = DateCompareUtils.compareYear(date.toString());
client.setAge(age);
client.setSex(resultSet.getString(4));
}
//释放资源
JdbcUtils.release(resultSet,connection,ps);
return client;
}
Service:
本项目就是简单的增删查看,所以直接return clientdao.selectById();
//创建dao层对象
private ClientDao clientDao = new ClientDaoImpl();
@Override
public Client getById(Integer id) throws SQLException, ClassNotFoundException {
return clientDao.selectById(id);
}
Controller:
public class FindClientServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@SneakyThrows
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1 获取表单数据
//客户ID
String clientId = req.getParameter("clientId");
int id = Integer.parseInt(clientId);
//2 创建 ClientService对象
ClientService clientService = new ClientServiceImpl();
//3 调用 clientService getById方法
Client cl = clientService.getById(id);
List<Client> list = new ArrayList<>();
list.add(cl);
//4 将结果放到request域当中
req.setAttribute("clients",list);
// 请求转发
req.getRequestDispatcher("/clientList.jsp").forward(req,resp);
}
}
在web.xml上设置servlet路径映射
<servlet>
<servlet-name>ListClientServletservlet-name>
<servlet-class>cn.edu.bnuz.order_system.controller.clientServlet.ListClientServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>ListClientServletservlet-name>
<url-pattern>/listClienturl-pattern>
servlet-mapping>
View:
在编写JSP代码时候一定要弄清楚逻辑顺序:Servlet→JSP 还是 JSP→Servlet 等等
这个时候在一开始项目设计时候用到的逻辑顺序图可以帮助我理解这样复杂的逻辑关系,能够快速定位
例如所有的VIEW层的JSP页面转发一定是要在一张JSP主页面里显示的,首先在启动Tomcat服务器时候就需要使用web.xml下设置默认开启的主页面
<welcome-file-list>
<welcome-file>main.jspwelcome-file>
welcome-file-list>
此处使用main.jsp页面作为启动项目的默认页面,
main.jsp
<table width="100%" height="700" cellpadding="0" cellspacing="0" border="1">
<tr>
<td width="15%" height="100%" valign="top" >
<a href="${pageContext.request.contextPath}/listClient" target="mainFrame" style="padding-left: 90px">查询客户a><br>
<a href="${pageContext.request.contextPath}/listProduct" target="mainFrame"style="padding-left: 90px">查询产品a><br>
<a href="${pageContext.request.contextPath}/listOrder" target="mainFrame"style="padding-left: 90px">查询订单a><br>
td>
<td width="60%" height="100%" valign="top" >
<iframe name="mainFrame" width="1100" height="2000" scrolling="no" frameborder="0" align="center">iframe>
td>
tr>
table>
评论列表(0条)