springboot写接口

今天来写一下springboot的接口,也是初学者,有报错的自己调试哈

第一步,创建项目

springboot写接口_第1张图片

点击下一步就是了

springboot写接口_第2张图片

 勾选这几项

springboot写接口_第3张图片

数据库选择mySQL,勾选mybatis,后续用mabatis写sql语句。

好,接下来就是。。。

 等待项目的生成。

springboot写接口_第4张图片

项目生成之后就是这鸟样了。。

 springboot写接口_第5张图片

个人喜好用yml的写法,看起来舒服些

接下来我们来创建表

mysql数据库客户端个人建议用下面这玩意,看着挺舒服的

springboot写接口_第6张图片

 springboot写接口_第7张图片

创建了一个名为练习的库,然后往这个库里添加表,添加数据就可以了

springboot写接口_第8张图片

创建一个表,表名自定义好了,字段那些自己看着办吧,

create table tb_User(
        userName VARCHAR(20),
        userAge  VARCHAR(20) ,
        userSex  VARCHAR(5),
        userSize VARCHAR(10),
        userHeight VARCHAR(10),
        userHobby VARCHAR(20)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

然后往表里插入一些数据,

像这样子,INSERT INTO tb_user VALUES('李蛋儿','25','男','10cm','165cm','流哈喇子') 

表里有了数据之后我们开始我们的接口。

springboot写接口_第9张图片

先创建好这些文件,consoller控制层,用来接收参数,返回查询结果用的(个人理解),还有dao层接口,实体类,业务层serve,mapper文件夹下面的.xml文件是写mybatis的。

接下来我们来配置数据库,用来连接mysql,设置端口之类的。

 springboot写接口_第10张图片

server:
  port: 9720
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/practice?useUnicode=true&autoReconnect=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&failOverReadOnly=false
    username: root
    password: 12345678
    driver-class-name: com.mysql.cj.jdbc.Driver
    hikari: #连接池
      minimum-idle: 10
      max-lifetime: 1800000
      maximum-pool-size: 15
      auto-commit: true
      pool-name: practice
      connection-timeout: 300000
      connection-test-query: select 1
  jackson:
    time-zone: GMT+8
mybatis:
  mapper-locations: classpath:mapper/*.xml

好的,我们配置好了数据库和连接池之后就是写接口了。。千万千万要睁大眼睛,一个都不要放过。

我们先从controller写起

springboot写接口_第11张图片

 

@RestController
@RequestMapping("/practice")
public class PracticeController {

    private PracticeServe practiceServe;

    @Autowired
    public PracticeController(PracticeServe practiceServe) {
        this.practiceServe = practiceServe;
    }

    @GetMapping("/test")
    public List findAll() {
        return practiceServe.findAll();
    }

 写完这个之后你会发现

springboot写接口_第12张图片

这几个地方报错,为什么呢,因为我们没写完。

好,不要管这个报错,我们接着写。。。

接下来是实体类和接口Dao

springboot写接口_第13张图片 这个是实体类,省去了getter和setter。写上去也不碍事的

@Data
public class PracticeEntity {
    private String userName;

    private String userAge;

    private String userSex;

    private String userSize;

    private String userHeight;

    private String userHobby;
}

记住,这里的类型要和数据库的字段类型是一样。

springboot写接口_第14张图片

 

 这是接口dao层,

@Mapper
@Repository
public interface PracticeDao {
    List findData();
}

dao层写好了我们就来写mybatis,一定一定看好了。。

springboot写接口_第15张图片

 



    
        
        
        
        
        
        
    
    

好了,万事具备了,我们来写业务层了

springboot写接口_第16张图片

@Service
public class PracticeServe {
    private PracticeDao practiceDao;
    @Autowired
    public PracticeServe(PracticeDao practiceDao){
        this.practiceDao = practiceDao;
    }

    public  List findAll(){
        return practiceDao.findData();
    }
}

 findData()这个方法一定是跳到controller层的,什么,怎么跳转?好家伙,按住ctrl建,然后把鼠标指向在这个findData,点它,就可以跳到controller层了。。

好的,接口写完了,你会发现之前报错的controlller层也不报错了。。。

打开Apipost工具测试一下写的接口

 springboot写接口_第17张图片

成功得到数据。。。接口完成 

你可能感兴趣的:(spring,boot,spring,java)