一、建立后端SpringBoot项目
File-->New-->Project 选择Spring Initializr
依赖选择Lombok,Spring Web,MyBatis Framework,MySQL Driver
二、后端项目配置与调试
在maven的配置文件pom.xml中配置阿里云镜像仓库,加速下载
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
在navicat中操作建立数据库。
编辑配置文件application.properties,连接数据库
server.port=9090
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/creative_tool?serverTimezone=GMT%2b8
spring.datasource.username=root
spring.datasource.password=123456
在启动类中,编写接口进行测试
@RestController
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@GetMapping("/")
public String index() {
return "ok";
}
新建User实体类
import lombok.Data;
@Data
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
private String phone;
private String sex;
private String profilepic;
}
新建UserMapper,通过注解编写sql进行测试
import com.example.springboot.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * from user")
List<User> findAll();
}
新建UserController,编写接口,返回所有用户,进行测试
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
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
private UserMapper userMapper;
@GetMapping("/")
public List<User> index() {
List<User> all = userMapper.findAll();
return all;
}
}
测试结果: