Student Management System (Java Edition)

Student Management System (Java Edition)

Foreword: This is the course design (or the student management system...) in the second year of the sophomore year. In theory, although it is done by a group of 4 people, but, pay attention to this "but", I am responsible for all... The code and documentation are basically I wrote it all by myself. At first, I vomited blood, which is why some functions of the later Web version have not been completed.

Project Introduction

The project is divided into a GUI desktop application written by JavaSwing and a semi-finished web application. The following figure shows the overall functional structure of the project

Please add image description

JavaSwing

JavaSwing uses the framework combination of MyBatis + Spring here. Later, it is found that using the Spring framework in the program developed by Swing seems to be a mistake.
In addition, there may be some logically unknown bugs in the JavaSwing version.

Function display (part)

1. Login module

insert image description here

2. System setting module
insert image description here
3. Student management module

student added
insert image description here

student list
insert image description here

4. Class management module

class added
insert image description here

class management
insert image description here

5. Performance management

performance statistics
insert image description here

6. Web version
Click to jump to the browser's http://localhost:8080 URL
insert image description here

Instructions for use

Use IDEA to open the project, the structure of the project is as follows:
insert image description here

If you start the project, run LoginFrm in the view package
insert image description here

problems encountered

Problems encountered with dependency injection with Spring

Swing is a Java package for GUI development. In the course design, I use Spring to manage the container, but there is a problem when using Spring annotations for container dependency injection. The dependency injection is null and the error is 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
have been looking for a long time on Baidu and haven't found it The problem is that after excluding the wrong Spring configuration file or not adding the @Service annotation, I found an explanation on the Internet, as follows:
Using @Autowired in multi-threading always fails to get beans.

The reason is: the new thread is not in the Spring container, so the bean object in Spring cannot be obtained.
JavaSwing is not thread-safe. Some parts of the project are run in multiple threads, and many UI threads run concurrently in it, so use Spring in these threads. Injection fails because they are not Spring managed threads

In the case of multi-threading, Spring does not allow the use of annotations to inject dependencies, so we can only manually 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 I don't know which threads it has when it runs.

This problem is something I didn't think of at the beginning. If I knew it, I would not use Spring on Swing... Because I have to manually obtain dependencies on each of the view layer classes, rather than using a singleton mode with myself to obtain MyBatis. There is no difference in dependencies, but using Spring is more troublesome

Problems with Mybatis using HashMap as a result set

When writing the method to find the specified check-in, I used List<HashMap<String,String>> as the return value, but it shows an error, the first time it shows a null pointer error, and then I configure the @Results result set, as follows:

@Results({
    
    
@Result(property = "key",column = "attendance_num",jdbcType = JdbcType.INTEGER),
@Result(property = "value",column = "attendance_date",jdbcType = JdbcType.VARCHAR)})

But it shows a format conversion error, so I output the query result in the console and found that the result is 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 the HashMap are not attributes (I was wrong), so At this point, Mybatis will set an attribute name by itself, so it becomes the above result. Don't ask why you don't use the form of xml files. At that time, there was not enough time, so I just used annotations for convenience.

Solution:
Reprocess the above result value and assign it to HashMap

Problems when deleting data with foreign key associations

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`))

After checking the error message, 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:
It is invalid to set the foreign key before deleting the data, as follows:

set foreign_key_checks = 0;

Then you can execute the delete statement at this time.
After deleting, set the foreign key to be valid, as follows:

set foreign_key_checks = 1;

This perfectly deletes the record.

JavaWeb

JavaWeb uses the framework combination of SpringBoot + Spring Data JPA here, the page is Thymeleaf for data display, and 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 realized mainly include login, logout, password modification, student management, class management, and grades statistics for grade management. In addition, it uses the same database as the JavaSwing version, so their previous data is actually interoperable.
Summary: Although the page is ugly, it can continue to be developed as a template.

Function display (part)

1. Login interface

insert image description here

2. Student management

student list
insert image description here
student add
insert image description here

3. Class management

class list
insert image description here

class added
insert image description here

4. Score statistics
insert image description here

insert image description here

insert image description here

Instructions for use

Project structure diagram:
insert image description here

If you start it, you can directly enter the DemoApplication class and right-click to start it.

problems encountered

Problems updating database with JPA

When using Spring Data JPA as the content of the persistence layer on the Web side, I encountered an error, as follows:

Executing an update/delete query

After searching on Baidu, I found that if JPA executes operations such as update or delete, the @Transactional annotation should be added to the Dao or Service layer, which means that this is a transaction-level operation, which is equivalent to a usage specification of JPA. , because of the JPA requirement, 'Update and delete operations cannot be performed without transaction support'.

At last

The project address is as follows:
Github address: https://github.com/guanchanglong/StudentManagementSystem-Java
Please give me a star when looking at the code ^ _ ^, thank you very much for your effort.

PS: You can also go to my personal blog to see more content
Personal blog address: Xiaoguan classmate's blog

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/120611591