Spring MVC implements a complete instance of MySQL database addition, deletion, modification and query

I just learned the spring MVC framework recently. I feel it is really convenient and reduces a lot of redundant code. I did a small project to practice myself. This is a primary spring MVC application project without mybatis. The project function is still perfect, and the basic functions of addition, deletion, modification and query are realized.

Project environment:

-System: win10

-Development environment: Eclipse seoxygen release candidate 3 (4.7)

-JDK version: Java1.0 8(121)

-MysqL:5.7

-spring:4.0

-tomcat:8.5

Technologies used:

springmvcspringjspjdbcjavaBeanjsjstl

Access address: http://localhost:8080/ Your project name / all

Disclaimer: I'm just a novice who has just started. It's hard to avoid mistakes in the code. If you find it, you're welcome to point it out. Thank you.

Here's the detailed process

1. First create a dynamic web project

I wrote the project name myself, so I won't write it in detail

2. This is my completed project structure

I just want to realize the function without using the interface. I only use three simple classes: entity class under bean package, Dao layer database access class and controller layer interface control class,

All referenced jar packages are in the / webcontent / WEB-INF / lib folder, which is different from ordinary Java projects.

3. Specific java code

1. For student class and entity class, first write a JavaBean. My student is used as a JavaBean. The detailed code is as follows:

2. Studentdao, the database access operation class, and then the Dao layer, that is, the code of the data access layer. Here, a class encapsulated by spring (jdbctemplate) is used. There are some methods to operate the database. You don't have to write a lot of duplicate code yourself, just write SQL statements. The following is the specific code:

3. Studentcontroller, front and back end interaction class, and finally the control layer studentcontroller class related to user interaction. This class is mainly used to combine the front and back ends to realize complete interaction. The following is the specific code:

All the Java code has been completed, and only the specific XML configuration and front-end page are left below.

4. Front page

Because it is a simple small project, my JS and CSS are on the same page, there are only two pages,

1.index. jsp

Home page, screenshot

edit

Detailed code:

2. login. jsp

5. Detailed file configuration

1. applicationContext. xml

This is the configuration file of spring's IOC container, which is used to implement dependency injection. The following is the specific code:

2. springMVC-servlet. XML, spring MVC configuration class,

For us to implement most of the servlet code, we only need to write the business implementation. The following is the specific code

3. web. xml

This is the configuration file of the web project. The following is the main code:

6. Project summary and appendix

This project is my daily practice. In order to be more proficient, I reviewed the whole process and became familiar with it,

Jar package appendix for the project:

In addition to the package of spring, there are jar packages of MySQL jbdc and JSTL

Download address:

Spring framework jar package (optional version): Spring official website

MysqL-jdbc. Jar (optional version): MySQL official website

jstl. Jar (optional version): Maven official address

The above is all about the complete example of spring MVC to add, delete, modify and query MySQL database. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Detailed explanation of web module configuration of spring

Spring MVC rest style introduction and implementation code example

Spring MVC interceptor for single sign on

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>