Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write java web and interface in spring boot

Shulou Source: shulou.com Published: 2022-06-02 03:16:15 09月14日 Update

This article mainly explains "how to write java web and interface in spring boot". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write java web and interface in spring boot.

Process:

Springboot development process

Another is the dependence of mybatis.

Test interface

@ RestControllerpublic class Hello {@ RequestMapping ("/ hello") public String hello () {return "helloworld";}}

* * .yml file configuration

Spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/student?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: 123456mybatis: mapper-locations: classpath:mapper/*.xml

Database fields:

Pojo

@ Datapublic class User {private int id; private String name; private int age; private String email; *

The rest is done by the get and set methods themselves.

Mapper

@ Mapperpublic interface UserMapper {List findAll ();}

If it is springboot, you can use @ MapperScan ("the full name of the package where the mapper interface resides") in the startup class, instead of annotating @ Mapper in the Mapper interface one by one. The @ Mapper annotation is the mapper interface that identifies him as mybatis. It automatically generates a dynamic proxy class from the interface annotated with @ Mapper.

Let springboot know your mapper layer, or add MapperScan ("full name of the package where the mapper layer resides") to the startup class.

Mapper.xml

SELECT * FROM user

Controller

@ RestControllerpublic class UserController {@ Autowired / / instantiate userService private UserService userService; @ RequestMapping ("/ user") public List getUser () {return userService.findAll ();}}

Be sure to inject userService into the container

The data was successfully obtained

At this point, I believe you have a deeper understanding of "how to write java web and interface in spring boot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Interface annotation full name content location data method learning practical deeper success no interest dynamic field instance practicality practice container that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Apple Huawei Xiaomi