创建项目时选择依赖
配置数据库连接
在resources中的 application.properties 配置文件进行连接的配置
#驱动名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#连接地址
spring.datasource.url=jdbc:mysql://10.20.22.11:26954/canteen#数据库用户名
spring.datasource.data-username=abc#数据库密码
spring.datasource.data-password=123456
实体类封装
属性必须和数据表中的一致
@Data //自动生成 set get 和构造方法
public class Traffic {private Integer id;private String category;private Integer today;private LocalDate into_date;}
查询数据接口
@Mapper //添加这个注解自动生成实现类
public interface TrafficMapper {@Select("SELECT * FROM number") //查询这个表的所有数据public List<Traffic> getTraffice();}
通过接口返回数据
@RestController
public class TrafficController {@Autowiredprivate TrafficMapper trafficMapper;@GetMapping("/getTraffic")public Result getTraffic(){List<Traffic> data =trafficMapper.getTraffic();return Result.success("获取成功",data);}}
{"code":200,"meg":"获取成功",
"data":[{"id":1000,"category":"一楼","today":2418,"into_date":"2023-12-03"},
{"id":1001,"category":"二楼","today":4871,"into_date":"2023-12-03"},
{"id":1002,"category":"三楼","today":5471,"into_date":"2023-12-03"}]}