Java EE final project - activity management system
- Based on MyBatis+springboot+jsp
- For small projects - job management system
1, About MyBatis
MyBatis is an excellent persistence framework that supports normal SQL queries, stored procedures and advanced mapping. MyBatis eliminates almost all the manual settings of JDBC code and parameters and the retrieval encapsulation of result sets. MyBatis can use simple XML or annotations for configuration and raw mapping, mapping the interface and Java POJO (Plain Old Java Objects) to records in the database.
MyBatis benefits
- Compared with JDBC, it reduces the amount of code by more than 50%.
2. MyBatis is the simplest persistence framework, small and easy to learn.
3. MyBatis is flexible and will not impose any impact on the existing design of the application program or database. SQL is written in XML and completely separated from the program code to reduce the coupling, facilitate unified management and optimization, and can be reused.
4. Provide XML tags to support writing dynamic SQL statements (if, else is used in XML).
5. Provide mapping labels to support the mapping of ORM field relationships between objects and databases (configure mapping relationships in XML, or use annotations).
2, Introduction to springboot
Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial construction and development process of spring applications. The framework uses a specific way to configure, so that developers no longer need to define a templated configuration. Spring Boot makes it very easy to create independent, production level applications based on spring. Most Spring Boot applications only need a small amount of spring configuration. Generally, you only need a small amount of time to develop programs based on Spring Boot.
springboot benefits
1> Creating a stand-alone spring application
2> Spring boot directly embeds Tomcat, Jetty and undercow containers, and does not need to be deployed through war files
3> Simplify build configuration by providing a "starter" dependency
4> Automatically configure spring and third-party class libraries as much as possible
5> Provides production ready features such as metrics and health checks, and externalized configuration
6> No code generation, no configuration via XML
Microservice
(from blog link)
1: What is microservice? Why micro services?
What is microservice? (familiar students can skip it directly)
Simple example: students who read military news should know that although an aircraft carrier has a strong combat ability, its weakness is too obvious, that is, its defense ability is too poor, and a single aircraft carrier rarely acts alone. Usually, the aircraft carrier battle group is the main military force. You can use the single application (poor defense, poor mobility) that can be understood as a single aircraft carrier to fight the aircraft carrier Group (complex scheduling and high maintenance cost) is understood as microservice.
Most developers have experienced and developed single application. Whether it's traditional Servlet + JSP, SSM or spring boot, they are all single application. What's the disadvantage of single application that accompanies us for a long time? What are the problems we are facing that lead us to abandon single application and turn to microservice architecture? The main problems of personal summary are as follows:
High deployment cost (whether modifying 1 line of code or 10 lines of code, full replacement is required)
Great impact and high risk of changes (no matter how small the code changes, the cost is the same)
Low deployment frequency due to high cost and risk (unable to deliver customer requirements quickly)
Of course, there are also problems such as the inability to meet the requirements of rapid expansion, elastic scaling, and adaptability to cloud environment. However, we will not go into details one by one. The above problems are all problems to be solved by microservice architecture. As for how to solve them, we will talk later
2: What problems does microservice solve and what problems does it introduce?
Let's see what micro services can bring us first? Features of microservice architecture:
Release for specific services, with small impact, small risk and low cost
Frequent release and fast delivery of requirements
Low cost capacity expansion, elastic expansion and adaptation to cloud environment
We know that a simple concept, nothing is perfect, everything has two sides, there are gains and losses, so in choosing microservices, it solves the problems of rapid response and elastic scaling, and what problems does it bring to us? Personal summary is as follows:
Complexity of distributed system
Cost of deployment, testing and monitoring
Issues related to distributed transaction and CAP
The system application has changed from the original monomer into dozens to hundreds of different projects, which will cause problems such as the dependence between services, how to unpack services, internal interface specifications, data transmission, etc., especially the service splitting, which requires the team to be familiar with the business process, know how to choose and choose, and ensure that the granularity service of splitting not only conforms to the basic principle of "high cohesion, low coupling", but also Taking into account the development of the business and the vision of the company, we should persuade team members to work hard for it, and actively invest in it to achieve a balance among all parties.
For distributed systems, deployment, testing and monitoring all need a lot of middleware to support, and middleware itself also needs to be maintained. The original single application is a very simple transaction problem , it becomes very complex to switch to the distributed environment. Whether the distributed transaction is solved by a simple retry + compensation mechanism, or by a strong consistency method such as two-stage commit protocol, depends on the familiarity with the business scenario and repeated trade-offs. The same problem also includes the trade-offs of CAP model. In short, microservice requires a higher level of technical stack for the team as a whole
3: What are the principles of using microservices?
The ancients said: before the horses and soldiers move, the food and grass should go first. To build microservices, we need to establish a long-term plan, not build database tables like writing CMS, and then start to work. In all likelihood, it will fail. Before we carry out microservice transformation, the architect should make a plan in advance. We divide this into three steps: preliminary stage, design stage and technical stage
In the early stage, we should do the following things:
Fully communicate with all parties to ensure that the requirements of customers and organizations are met and recognized
Communicate with team members (development / test / operation and maintenance) to understand and actively engage
Communicate with business department, specify version plan and launch time
In the design phase, referring to Sam Newman's work "microservice design", a single microservice must meet the following conditions to meet the basic requirements of microservice:
Standard REST style interface (based on HTTP and JSON formats)
Independent deployment, avoid sharing database (avoid affecting the whole distributed system due to database)
High cohesion in business, reduce dependence (avoid service too large or too small in design)
A huge distributed system needs a strong infrastructure to support. What infrastructure does microservice involve?
CI/CD and automation (distributed systems are almost impossible to manually release)
Virtualization Technology (to ensure the isolation of microservice operation environment, the current industry mainstream is to use Docker container)
Log aggregation, full link monitoring (highly observable and diagnostic)
1, Project requirements
For students and teachers to design different functions, both students and teachers need to have registration and login functions. For students, they need to be able to choose to submit and update assignments. For teachers, they can assign assignments and view assignments. This project needs some changes to the needs of my previous blogs, so this project took a longer time.
2, Project design
Although the project requirements are very simple, we need to consider various aspects when we actually do it, such as the interface, the addition of registered database, the granting of permissions, and the implementation of update and other operations.
Here I'll show you several interfaces of the project:
Interface design:
Login interface Registration interface Error report when user name and password are empty
Teacher interface:
Student interface:
The rest of the UI is similar to that of the previous blog, which will not be shown here. (relatively simple...)
Database design:
Compared with the previous blog database design, the database has an additional user role table, which is responsible for recording the user name, password and identity of the login and registration user:
Job sheet:
User role table:
Students and assignments:
The student watch is not used this time.
3, Relevant code display
Here is the code of several jsp files:
Login interface:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login interface</title> <link rel="stylesheet" type="text/css" href="css/css.css"/> </head> <body> <form name="form1" action="LoginServlet" method="post"> <h1>production and operation management system</h1> <input class=input_1 id=username size=15 name="username" placeholder=user name><br /> <input class=input_1 id=password type=text size=15 name="password" placeholder=password><br /> <span> <input type="radio" id="radio_1" name="gettype" value="teacher" checked> <label for="radio_1"></label> //teacher </span> <span> <input type="radio" id="radio_2" name="gettype" value="student"> <label for="radio_2"></label> //student </span><br/> <input class=input_3 type="button" onclick="login()" value="Sign in" /> <input class=input_3 type="button" onclick="window.location.href='Register.jsp'" value="To register" /> </form> <script type="text/javascript" > function login(){ if(form1.username.value==''){ alert('User name cannot be empty!'); return false; } if(form1.password.value==''){ alert('Password cannot be empty!'); return false; } form1.submit(); if(form1.gettype.value=='teacher'){ window.location.href = 'teacher.jsp'; } if(form1.gettype.value=='student'){ window.location.href = 'student.jsp'; } }</script> </body> </html>
Registration interface:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Registration interface</title> <link rel="stylesheet" type="text/css" href="css/css2.css"/> </head> <body> <form name="form2" action="RegisterServlet" method="post"> <h1>production and operation management system</h1> <input class=input_1 id=username size=15 name="username" placeholder=user name><br /> <input class=input_1 id=password type=text size=15 name="password" placeholder=password><br /> <span> <input type="radio" id="radio_1" name="gettype" value="teacher" checked> <label for="radio_1"></label> //teacher </span> <span> <input type="radio" id="radio_2" name="gettype" value="student"> <label for="radio_2"></label> //student </span><br/> <input class=input_3 type="button" onclick="Register()" value="register" /> </form> <script type="text/javascript" > /** * @return */ function Register(){ if(form2.username.value==''){ alert('User name cannot be empty!'); return false; } if(form2.password.value==''){ alert('Password cannot be empty!'); return false; } form2.submit(); //window.location.href = 'index.jsp'; }</script> </body> </html>
Student selection interface:
<%@ page import="org.example.spring.model.Homework" %> <%@ page import="org.example.spring.jdbc.HomeworkJdbc" %> <%@ page import="java.util.List" %> <%-- Created by IntelliJ IDEA. User: win7 Date: 2020/3/9 Time: 11:05 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>StudentHomework</title> </head> <body> <table align="center" width="960" border="1" bgcolor="black" cellpadding="1" cellspacing="1"> <tr align="center" bgcolor="#7fffd4" height="60"> <td>Job No</td> <td>Job title</td> </tr> <% List<Homework> list = HomeworkJdbc.selectAllh(); if(null == list || ((List) list).size() <= 0){ out.print("None data."); }else { for (Homework sh : list){ %> <tr align="center" bgcolor="white" height="60"> <td><%=sh.getHno()%></td> <td><%=sh.getHtitle()%></td> <td><input type = "button" value = "Select the job" style="width:180px;height: 60px;font-size:16px" onclick = "window.location.href = 'handin.jsp?hno=<%=sh.getHno()%>&htitle=<%=sh.getHtitle()%>'"></td> </tr> <%-- <script type="text/javascript">--%> <%-- $(function(){--%> <%-- $("#tab").on("click", ":button", function(event){--%> <%-- $("#text").val($(this).closest("tr").find("td").eq(0).text());--%> <%-- });--%> <%-- });--%> <%-- </script>--%> <% } } %> </table> </body> </html>
Submission interface:
<%-- Created by IntelliJ IDEA. User: win7 Date: 2020/3/12 Time: 21:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Submit job</title> </head> <% String hno=request.getParameter("hno");System.out.println(hno);%> <% String htitle=request.getParameter("htitle");System.out.println(htitle);%> <body align="center"> <h1>Submit job</h1> <form action="handin" method="post"> Student ID:<label> <input type="text" name="studentno"> </label><br> Job No:<label> <input type="text" name="homeworkno" value="<%=hno%>"> </label><br> Assignment title:<label> <input type="text" name="homeworktitle" value="<%=htitle%>"> </label><br> Job content:<label> <input type="text" name="homeworkcontent"> </label><br> <input type="submit" id="submit" value="Hand in homework"> </form> </body> </html>
4, Problems encountered in the development process
1. Transfer parameters between interfaces
HTTP is a stateless protocol. The Web page itself cannot pass information to the next page, if you need to let the next page know the value in that page, unless through the server. Therefore, it is an important technology for Web pages to keep state and pass it to other pages.
Transferring data between web pages is an important function of Web programs
Refer to this Blog , here I use the method of URL parameter passing, but it still has risks
This method has the following advantages and disadvantages:
advantage:
Simplicity and diversity of platform support (no browsers don't support URL s).
Disadvantages:
1) The transmitted data can only be strings, which has certain restrictions on data types;
2) The value of the transmitted data will be seen in the browser address bar, which is not safe from the perspective of confidentiality. Especially the data with strict confidentiality requirements, such as passwords.
2. Update of student assignments
This problem bothered me for a long time at the beginning. I thought about setting a trigger or writing another function to judge whether there was the same student's judgment on the same assignment. Later, I took a relatively simple method,
String sqlString = "insert into student_homework(no, hno, sno, htitle, hcontent) values (?,?,?,?,?)ON DUPLICATE KEY UPDATE hcontent=? ";
Here I use the update method of INSERT ON DUPLICATE KEY UPDATE. After the primary key is set in the database, I can update the same homework for the same student.
3. css and js files in JSP cannot be loaded
This problem also bothered me at first. I started to web.xml Intercept is configured in:
<!-- Configure the processing of static resources --> <servlet-mapping> <servlet-name>default </servlet-name> <url-pattern>*.js</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>*.map</url-pattern> <url-pattern>*.jpg</url-pattern> <url-pattern>*.png</url-pattern> <url-pattern>*.eot</url-pattern> <url-pattern>*.svg</url-pattern> <url-pattern>*.ttf</url-pattern> <url-pattern>*.woff</url-pattern> <url-pattern>*.otf</url-pattern> <url-pattern>*.woff2</url-pattern> <url-pattern>*.gif</url-pattern> </servlet-mapping>
But in the end, I couldn't produce the pictures and the styles I set. At last, I found the problem:
This is the directory structure after I changed it
The css and js folders should be located in the webapp folder. I put them in the WEB-INF file at the beginning, but they can't be loaded. I hope you can learn from them and avoid mistakes next time.