三层架构
不同于MVC
三层:
- Controller控制层,封装对象
- Service 业务层,业务逻辑
(1)获取已存在的所有用户
(2)通过遍历得到每一个用户信息然后育新用户进行比较
(3)结果已存在就不再注册,不存在就注册 - Dao数据访问层,基本的增删改查
Controller:接收前台传递过来的数据,对数据进行封装,然后交给Service层完成业务逻辑,Service层可以调用Dao层完成增删改查
在三层架构里面是面向接口编程,Service层能够为Controller层提供的所有功能都写到Service层接口。
MVC:
M(Model) 模型 JavaBean
V(View) 视图 Html Jsp Volicity Freemaker
C(Control) 控制器 Servlet,Controller,Action
Jsp+Servlet+JavaBean就是把视图和逻辑隔离开来
JavaBean:一种规范,表达实体和信息的规范,便于封装重用
1、所有属性为private
2、提供默认构造方法
3、提供getter和setter
4、实现serializable接口 serialize
示例
图解
代码
TeacherController
@WebServlet("/teacherController")
public class TeacherController extends HttpServlet {
private ITeacherService teacherService = new TeacherServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("TeacherController.service");
// 解决post请求乱码问题
req.setCharacterEncoding("UTF-8");
String method = req.getParameter("method");
if (method == null || method == "")
{
method = "selectByPage";
}
switch (method){
case "selectByPage":
selectByPage(req,resp);
break;
}
}
private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("TeacherController.selectByPage");
//1.封装前台过来的数据
String pageNoStr = req.getParameter("pageNo");//req.getContextPath()返回String类
if (pageNoStr ==null || pageNoStr.equals("")){
pageNoStr = "1";//默认第一页
}
String pageSizeStr = req.getParameter("pageSize");
if (pageSizeStr == null || pageSizeStr.equals("")){
pageSizeStr = "5";//默认一页5条数据
}
int pageNo = Integer.parseInt(pageNoStr);//转换成int
int pageSize = Integer.parseInt(pageSizeStr);
//2.封装PageInfo
// int offset = (pageNo - 1) * pageSize;
// ArrayList<Teacher> list = getCurrentPageList(offset,pageSize);//得到limit所需数据
// int totalCount = getTotalCount();//得到总数据数
// int totalPage = (int)Math.ceil((double) totalCount / pageSize);//得到总页数
// PageInfo pageInfo = new PageInfo(list, pageNo, totalPage, pageSize);
// System.out.println("pageInfo");
PageInfo pageInfo = teacherService.selectByPage(pageNo,pageSize);
//3.转发到界面
req.setAttribute("pageInfo",pageInfo);
req.getRequestDispatcher("/teacher_list.jsp").forward(req,resp);
}
}
ITeacherService
public interface ITeacherService {
public PageInfo selectByPage (Integer pageNo, Integer pageSize);
}
TeacherServiceImpl
public class TeacherServiceImpl implements ITeacherService {
private ITeacherDao teacherDao = new TeacherDaoImpl();
@Override
public PageInfo selectByPage(Integer pageNo, Integer pageSize) {
System.out.println("TeacherServiceImpl.selectByPage");
int offset = (pageNo - 1) * pageSize;
ArrayList<Teacher> list = teacherDao.selectCurrentPageList(offset,pageSize);//得到limit所需数据
int totalCount = teacherDao.selectTotalCount();//得到总数据数
int totalPage = (int)Math.ceil((double) totalCount / pageSize);//得到总页数
PageInfo pageInfo = new PageInfo(list, pageNo, totalPage, pageSize);
return pageInfo;
}
}
ITeacherDao
public interface ITeacherDao {
ArrayList<Teacher> selectCurrentPageList(int offset, Integer pageSize);
int selectTotalCount();
}
TeacherDaoImpl
public class TeacherDaoImpl implements ITeacherDao {
@Override
public ArrayList<Teacher> selectCurrentPageList(int offset, Integer pageSize) {
System.out.println("TeacherDaoImpl.selectCurrentPageList");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ArrayList<Teacher> list = new ArrayList<>();
try {
connection = JDBCUtil.getConnection();
String sql = "select id, name, age from teacher limit ?,?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, offset);
preparedStatement.setInt(2, pageSize);
System.out.println(preparedStatement);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
Teacher teacher = new Teacher(id, name, age);
list.add(teacher);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.close(connection, preparedStatement, resultSet);
}
return list;
}
@Override
public int selectTotalCount() {
System.out.println("TeacherDaoImpl.selectTotalCount");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int totalCount = 0;
try {
connection = JDBCUtil.getConnection();
String sql = "select count(*) from teacher";
preparedStatement = connection.prepareStatement(sql);
System.out.println(preparedStatement);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
totalCount = resultSet.getInt(1);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.close(connection,preparedStatement,resultSet);
}
return totalCount;
}
}