Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use springboot Interface in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use the springboot interface in Java, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

Preliminary preparation:

First create a new project in idea:

New project-spring initializr next all the way (if you have any information that needs to be modified, you can also modify it yourself, not important)

Note that you should remember to select here. The function is to initialize the things you need when you need idea (of course, you can also add them manually later, but here to facilitate learning, you still choose what you should choose).

Developer tools-spring boot devtools

Web-spring web

SQL-jdbc api-mysql driver-mybatis framework

Finally, make sure that the selected dependencies in the right column is as follows:

All the way to next. Wait for the loading to complete

Then prepare a student table in the MySQL database:

Next, rename the application.properties file in src\ main\ resources to application.yml because the structure of the yml file is clear, good-looking and easy to understand

Application.yml file is a configuration file, the function is to configure some database information, it tells springboot our database account password, table name and so on.

The following is my application.yml. You can modify the corresponding information according to your actual situation:

Server: port: 8081spring: # Database connection configuration datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/dt55?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: 888888#mybatis related configuration mybatis: # mapper configuration file mapper-locations: classpath:mapper/*.xml

Verify that the previous configuration is successful: click src\ main\ java\ com\ wzy\ demo\ DemoApplication.java

Click the run button indicated by the arrow:

If the console appears:

And typing http://localhost:8081/ in the browser will appear:

Then the configuration is successful.

Step 1: create an entity class, which needs to be consistent with the database table field

Create a new package named entity in the src\ main\ java\ com\ wzy\ demo directory

Then, under the entity package, create a new class named Student, which is the entity class, which is the table in the corresponding database. The attributes in the class are consistent with the database fields, and the constructor and get and set methods are added:

Public class Student {private int id; private String name; private int age; private String hobby; private String address; public Student (int id, String name, int age, String hobby, String address) {this.id = id; this.name = name; this.age = age; this.hobby = address;} public int getId () {return id } public void setId (int id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getHobby () {return hobby } public void setHobby (String hobby) {this.hobby = hobby;} public String getAddress () {return address;} public void setAddress (String address) {this.address = address;}} step 2: establish the mapper interface and define the actions of the database to be operated

Actions of operating database, such as query, update, delete, etc.

These actions are mapper.

Create a new package named mapper in the src\ main\ java\ com\ wzy\ demo directory

Under the mapper package, create a new interface StudentMapper

And because a row of data in the database corresponds to an object.

Let's assume that we want to find all the student information.

@ Mapperpublic interface StudentMapper {List findAllStudent ();} step 3: create the xml file of mapper and write specific sql statements

Then create a package named mapper in the resources directory, and create a new file named StudentMapper.xml under the package of mapper. This file tells java exactly which sql statements to write.

SELECT * FROM student

Note that the namespace here points to which mapper

Id is the name of the method in mapper, and resultType is the returned type

This corresponds to the mapper-locations of application.yml in the previous article

Step 4: set up the service class to handle the business logic

Step 3 has already obtained the data, but when it is shown to the front end, the data needs to be further processed according to its own business logic, so here we establish the service package under src\ main\ java\ com\ wzy\ demo, and in the service package, create a new StudentService class, which is used to deal with the corresponding business logic.

/ / @ Service means to instantiate it. / / before, if there is a class, do you need an object new to use it? / / it can be understood here that it will automatically help you new,new. After adding the instance to the spring container @ Servicepublic class StudentService {/ / introducing StudentMapper,@Autowired means to instantiate it and take it out of the spring container through autowired for us to use @ Autowired private StudentMapper studentMapper. / / deal with business logic. Here, because the business is too simple, it is empty. I will return public List findAllStudent () {return studentMapper.findAllStudent ();}} step 5: show the processing result in the controller class.

Create the controller package under src\ main\ java\ com\ wzy\ demo, and create a new StudentController class in the controller package. The purpose of this class is to send the information to the front end, that is, to return the corresponding data to the browser.

/ / @ RestController automatically converts an object to json format @ RestControllerpublic class StudentController {@ Autowired private StudentService studentService; / / @ RequestMapping ("/ getstudent"), which tells the front end to access the corresponding address @ RequestMapping ("/ getstudent") public List getStudent () {return studentService.findAllStudent ();}} verify

Browser input: http://localhost:8081/getstudent

Database related information appears, and the experiment is successful.

The above is all the contents of the article "how to use the springboot Interface in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report