原文链接
Sprint Boot 的残缺实例,从数据库读取数据并应用 Postman 测试。
我的项目地址:https://github.com/Snowstorm0…
1 本地数据库
本地数据库创立教程:
创立本地数据库 user_data:
2 代码主体
2.1 Controller
文件名 MyController,内容为:
@RestController
@RequestMapping("/homepage")
public class MyController {
@Resource
MyService myService;
@GetMapping("/learnSpringBoot")
public String learnController(@RequestParam("number") int number){return myService.learnService(number);
}
}
2.2 Service
文件名 MyService,内容为:
@Service
@EnableScheduling
public class MyService {
@Resource
MyMapper myMapper;
public String learnService(int number){UserDTO res = myMapper.LearnMapper(number);
String name = res.getName();
System.out.println("name:" + name + "\n");
return name;
}
}
2.3 dao
文件名 MyMapper,内容为:
@Mapper
public interface MyMapper {UserDTO LearnMapper(int number);
}
2.4 model
文件名 UserDTO,内容为:
@Data
public class UserDTO {
private Integer number;
private String name;
public Integer getNumber() {return number;}
public void setNumber(Integer number) {this.number = number;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
2.5 Application
在 application.properties 配置:
# 设置端口号
server.port=8888
在 application.yml 配置:
# 连贯数据库
spring:
datasource:
url: jdbc:mysql://localhost:3306/sys
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
username: root
# 配置 mapper.xml 门路
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.spring.root.dao
3 Postman
配置 Get,地址为:http://localhost:8888/homepag…
输出 number:1001
即可取得返回的 name:赵一
学习更多编程常识,请关注我的公众号:
代码的路