首页 > 数据库 >2024.11.11(spring boot创建数据库)

2024.11.11(spring boot创建数据库)

时间:2025-01-10 23:12:37浏览次数:1  
标签:11 2024.11 springboot spring id User import com public

完整代码
UserController
package com.example.springboot.controller;

import com.example.springboot.pojo.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
@Autowired
UserService userService;

@GetMapping ("/queryUserList")
public List<User> queryUserList(){
    return userService.queryUserList();

}
@GetMapping ("queryUserById")
public User queryUserById(int user_id){
return userService.queryUserById(user_id);

}

@GetMapping("/addUser")
public String addUser(User user){
    return userService.addUser(user);

}

@GetMapping("/deleteUserById")
public String deleteUserById(int id){
    return userService.deleteUserById(id);

}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
UserMapper
package com.example.springboot.mapper;

import com.example.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
List queryUserList();
User queryUserById(int id);
void addUser(User user);
void deleteUserById(int id);

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
User
package com.example.springboot.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

private String name;
private int id;
private String sex;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
UserService
package com.example.springboot.service;

import com.example.springboot.mapper.UserMapper;
import com.example.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
@Autowired
UserMapper userMapper;
public List queryUserList(){
return userMapper.queryUserList();

}
public User queryUserById(int id){
    User user= userMapper.queryUserById(id);
    return user;

}
public String addUser(User user){
    userMapper.addUser(user);
    return "新增成功";

}
public String deleteUserById(int id){
    userMapper.deleteUserById(id);
    return "删除成功";
}

}

标签:11,2024.11,springboot,spring,id,User,import,com,public
From: https://www.cnblogs.com/yangsongduo/p/18664867

相关文章