Foreword: This is a course designed for sophomore year (still a student management system...). In theory, although it is 4 people per It was done together with the team, but, pay attention to this "but", I am still responsible for all the code and documents... I basically wrote the code and documents by myself. I was so worried at first that I was vomiting blood. This is why some of the functions of the Web version later Reason for not being completed.
The project is divided into a GUI desktop application written in JavaSwing and a semi-finished Web application. The following figure shows the overall functional structure of the project
JavaSwing uses the MyBatis Spring framework combination. Later I found that using the Spring framework seemed to be a mistake in programs developed with Swing.
In addition, there may be some unknown logical bugs in the JavaSwing version.
1. Login module
2. System setting module
3. Student management module
Student addition
Student list
4. Class management module
Class addition
Class management
5. Score management
Score statistics
6. Web version
Click to jump to the browser's http://localhost:8080 URL
Use IDEA to open the project. The structure of the project is as follows:
If you start the project, run LoginFrm in the view package
Problems encountered when using Spring for dependency injection
Swing is a Java package for GUI development. In the course, I used Spring to manage the container. However, a problem occurred when using Spring annotations for container dependency injection. The dependency injection was null and the error was reported as follows:
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at com.view.LoginFrm.loginAct(LoginFrm.java:187)
at com.view.LoginFrm$2.actionPerformed(LoginFrm.java:96)
I searched on Baidu for a long time but could not find the problem. I ruled out that the Spring configuration file was written incorrectly or the @Service annotation was not added. After waiting for the problem, I found an explanation on the Internet, as follows:
The bean cannot be obtained when using @Autowired in multi-threading.
The reason is: new thread is not in the Spring container, so it cannot obtain the bean object in Spring
JavaSwing is not thread-safe. Some places in the project are run by multi-threads, and many UI threads It runs concurrently inside, so Spring injection fails in these threads because they are not Spring-managed threads
And Spring does not allow annotations to be used to inject dependencies in multi-threaded situations, so we can only manually To get the bean object we want, the code is as follows:
private final ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); private final AdminService adminService = (AdminServiceImpl)context.getBean("AdminServiceImpl");
In fact, it can also be managed by configuring the thread pool, but I am not familiar with Swing and don’t know how many threads it has when running.
This problem was something I didn't expect at first. If I had known it, I wouldn't have used Spring on Swing...because I have to manually obtain dependencies for each view layer class, which is much worse than using it myself. There is no difference in obtaining the dependencies of MyBatis in a singleton mode, but it is more troublesome to use Spring.
Problems that occur when Mybatis uses HashMap as the result set
When writing the method to find the specified sign-in, I used List
@Results({
@Result(property = "key",column = "attendance_num",jdbcType = JdbcType.INTEGER),
@Result(property = "value",column = " attendance_date",jdbcType = JdbcType.VARCHAR)})
But it showed a format conversion error, so I output the query results on the console and found that the result was like this
[{value=2018-05-17, key=1}, {value=2018-04-17, key=1}, {value=2018-04-18, key=1}, {value=2018- 04-19, key=3}, {value=2018-04-20, key=1}, {value=2018-04-21, key=1}, {value=2018-05-03, key=1} ]
The value in HashMap has changed from the form of JSON string to the form of xxx=xxx. The property attribute value corresponds to the attribute of the entity class, but the key and vlaue in HashMap cannot be counted. Attribute (I thought wrong), so Mybatis will set an attribute name by itself at this time, so it becomes the above result. Don’t ask why it’s not in the form of an xml file. I didn’t have enough time at the time, so I just used annotations for convenience.
Solution:
Reprocess the above result value and assign it to HashMap
Problems that occur when deleting data associated with foreign keys
The Service layer in the project reports an error as follows:
Cannot delete or update a parent row: a foreign key constraint fails (`ttms`.`s_attendance`, CONSTRAINT `student_attendance_foreign` FOREIGN KEY (`student_id`) REFERENCES `s_student` (`id`)); nested exception is java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`ttms`.`s_attendance`, CONSTRAINT `student_attendance_foreign` FOREIGN KEY (`student_id`) REFERENCES `s_student` (`id`))
Check the error Information, the problem lies in the Dao layer. There is a problem with a delete statement. After analyzing the reason, it is found that a foreign key association is set, which prevents us from deleting the data.
Solution:
Set the foreign key to invalid before deleting the data, as follows:
set foreign_key_checks = 0;
Then you can execute the delete statement at this time
After deletion, set the foreign key to be valid, as follows:
set foreign_key_checks = 1;
This way This record has been deleted perfectly.
JavaWeb The framework combination of SpringBoot Spring Data JPA is used here. The page uses Thymeleaf for data display. There is a statistics page on the page that uses ECharts for data visualization.
As mentioned earlier, the Web is a semi-finished product. The functions it has implemented mainly include the following functional modules: login, logout, password change, student management, class management, and score management. In addition, the database it uses is the same as the JavaSwing version, so their previous data are actually interoperable.
Summary: Although the page is a bit ugly, it can be used as a template for continued development.
1. Login interface
2. Student management
Student list
Student addition
3. Class management
Class list
Class addition
4. Score statistics
Project structure diagram:
To start, just right-click in the DemoApplication class to start.
Problems encountered when using JPA to update the database
When using Spring Data JPA to do the content of the persistence layer on the Web side, an error was encountered. As follows:
Executing an update/delete query
After searching on Baidu, I found that it is JPA. If you perform update or delete operations, you must use Dao or Service The @Transactional annotation is added to the layer, which means that this is a transaction-level operation. This is equivalent to a usage specification of JPA, because JPA requires that without transaction support, update and delete operations cannot be performed.
The above is the detailed content of Analyze the case of student management system implemented in Java.. For more information, please follow other related articles on the PHP Chinese website!