作者:夜翊灬瞳_398 | 来源:互联网 | 2023-09-11 14:38
一、建立后端SpringBoot项目File--New--Project选择SpringInitializr依赖选择Lombok,SpringWeb,MyBat
一、建立后端SpringBoot项目
File-->New-->Project 选择Spring Initializr
依赖选择Lombok,Spring Web,MyBatis Framework,MySQL Driver
二、后端项目配置与调试
在maven的配置文件pom.xml中配置阿里云镜像仓库,加速下载
nexus-aliyunnexus-aliyunhttp://maven.aliyun.com/nexus/content/groups/public/truefalse
publicaliyun nexushttp://maven.aliyun.com/nexus/content/groups/public/truefalse
在navicat中操作建立数据库。
编辑配置文件application.properties,连接数据库
server.port=9090spring.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 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 {@Autowiredprivate UserMapper userMapper;@GetMapping("/")public List index() {List all = userMapper.findAll();return all;}
}
测试结果: