实战篇01-开发环境搭建
一、准备数据库
- 执行资料中的big_event.sql脚本,准备数据库表,命令:
source /root/Desktop/bigEvent/big_event.sql
- 连接数据库,验证执行
二、创建spring boot工程
- 创建spring boot工程,勾选web,mybatis和mysql依赖
- 在resource中新增application.yml文件
三、引入mybatis
- 在配置文件application.yml中引入mybatis的配置信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.44.135:3306/big_event
username: root
password: ******
四、准备包文件夹以及实体类
- 创建包文件夹:controller;mapper;pojo;service;service.impl;utils
- 在pojo(实体类)中,生成三个实体类:User;Article;Category,对应数据库中的三个表
package com.example.bigevent.pojo;
import java.time.LocalDateTime;
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
private String userPic;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
package com.example.bigevent.pojo;
import java.time.LocalDateTime;
public class Article {
private Integer id;
private String title;
private String content;
private String coverImg;
private String state;
private Integer categoryId;
private Integer createUser;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
package com.example.bigevent.pojo;
import java.time.LocalDateTime;
public class Category {
private Integer id;
private String categoryName;
private String categoryAlias;
private Integer createUser;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
五、引入lombok
- 在pom依赖中,引入lombok,为实体类自动生成getter,setter和tostring
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
- 在实体类上添加注解使用lombok,注解:@Data,以User实体类为例
package com.example.bigevent.pojo;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
private String userPic;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
- 使用maven的compile,自动生成getter,setter和tostring
- 验证:在target->classes->com->pojo->User中可以看到,已经自动生成getter,setter和tostring