1 demand analysis
What data should be displayed for page analysis
2 write code
2.1 preparation of Dao layer
-
Modify CourseDao and add findCourseList method
Interface CourseDao //Query course list information public List<Course> findCourseList(); Implementation class CourseDaoImpl @Override public List<Course> findCourseList() { try { //1.establish QueryRunner QueryRunner qr = new QueryRunner(DruidUtils.getDataSource()); //2.to write SQL String sql = "SELECT id,course_name,price,sort_num,STATUS FROM course where id_del = ?"; //3.Execute query List<Course> courseList = qr.query(sql, new BeanListHandler<Course>(Course.class), 0); return courseList; } catch (SQLException e) { e.printStackTrace(); return null; } }
Logical deletion
-
The essence of logical deletion is modification. The so-called logical deletion is not a real deletion, but a corresponding deletion flag in the table to modify.
-
For example, 0 is undeleted and 1 is deleted. Logically, the data is deleted, but the data itself still exists in the library.
Physical deletion
-
Physical deletion is the real deletion from the database
2.2 preparation of service layer
Modify CourseService and add findCourseList method
Interface CourseService public List<Course> findCourseList(); Implementation class CourseServiceImpl //establish CourseDao CourseDao courseDao = new CourseDaoImpl(); @Override public List<Course> findCourseList() { //call Dao Make a query return courseDao.findCourseList(); }
2.3 Servlet writing
2.3.1 interface development specification
What we are doing is a front-end and back-end separation project that needs to be docked through interface documents. Therefore, during the development process, we should carefully check the api interfaces and parameter fields required by the front end
In order to develop in strict accordance with the interface and improve efficiency, the request and response formats are standardized.
Development specification
1. When making a get request, the request is made in the format of key/value, which can be obtained in the Servlet using getParameter(). |
2. There are three data formats for post requests. The first is Json data, which is parsed by fastjson in a jsonl type data Servlet. The second is to submit form data. The third is multipart / form data such as files |
3. The unified format of response results is json |
Why use JSON?
The data format is relatively simple and easy to read and write. JSON format can be used directly for server-side code, which greatly simplifies the amount of code development on server-side and client-side, but the completed tasks remain unchanged and easy to maintain
The JSON parsing tool used in this project is Alibaba's fastjson, maven project. Just import the following dependencies
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.37</version> </dependency> <dependency> <groupId>com.colobu</groupId> <artifactId>fastjson-jaxrs-json-provider</artifactId> <version>0.3.1</version> </dependency>
2.3.2 interface documents
The front-end development is based on the interface written by the server. If the front-end personnel wait for the server personnel to complete the interface development before developing the front-end content, this is very inefficient,
Therefore, when the interface definition is completed, the tool can be used to generate the interface document, and the front-end personnel can view the interface document for front-end development. In this way, the front-end and service personnel can develop in parallel, which greatly improves the production efficiency
2.3.3 writing CourseServlet
In the CourseServlet, add the findCourseList method
@WebServlet("/course") public class CourseServlet extends BaseServlet { //Query course information list public void findCourseList(HttpServletRequest request, HttpServletResponse response){ try { //1.Receive parameters //2.Business processing CourseService cs = new CourseServiceImpl(); List<Course> courseList = cs.findCourseList(); //3.Response results //SimplePropertyPreFilter Specify the to convert JSON field SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id","course_name","price","sort_num","status"); String result = JSON.toJSONString(courseList,filter); response.getWriter().print(result); } catch (IOException e) { e.printStackTrace(); } } }