#Java篇:java项目init和写接口流程步骤详细

idea里面 file---new-project


Server url

https://start.aliyun.com

目录结构

数据库链接项目配置

application.yml

yaml 复制代码
server:
  port: 8888
spring:
    datasource:
     url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8
     username: root
     password: root
     driver-class-name: com.mysql.cj.jdbc.Driver

主要目录

controller
service
impl实现类
model
mapper

java接口详细描述

创建文件顺序

model---Role

mapper---RoleMapper 继承Role

service----IRoleService 继承Role

service(impl)---RoleServiceImpl 实现IRoleService

controller---RoleController 实现接口返回前端

model---Role
java 复制代码
// model---Role
package com.xiaolibao.javacxyxiaolibao.model;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@TableName("role_info")
@Data
public class Role {
    private String id;
    private String name;
    private String phone;
}
mapper---RoleMapper
java 复制代码
// mapper---RoleMapper
package com.xiaolibao.javacxyxiaolibao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiaolibao.javacxyxiaolibao.model.Role;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public  interface RoleMapper extends BaseMapper<Role> {

}
service----IRoleService
java 复制代码
// service----IRoleService 继承Role
package com.xiaolibao.javacxyxiaolibao.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.xiaolibao.javacxyxiaolibao.model.Role;

import java.util.List;

public interface IRoleService extends IService<Role>  {
    List<Role> test();
}
impl--- RoleServiceImpl
java 复制代码
// RoleServiceImpl 实现IRoleService
package com.xiaolibao.javacxyxiaolibao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaolibao.javacxyxiaolibao.mapper.RoleMapper;
import com.xiaolibao.javacxyxiaolibao.model.Role;
import com.xiaolibao.javacxyxiaolibao.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> test() {
        List<Role> roles = roleMapper.selectList(null);
        return roles;
    }
}

####controller--- RoleController

java 复制代码
// RoleController 实现接口返回前端
package com.xiaolibao.javacxyxiaolibao.controller;

import com.xiaolibao.javacxyxiaolibao.model.Role;
import com.xiaolibao.javacxyxiaolibao.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
 @Autowired
    private IRoleService roleService;
 @GetMapping("/test")
    public List<Role> test(){
      List<Role> test = roleService.test();
      return  test;
    }
}
相关推荐
杨荧1 分钟前
【开源免费】基于Vue和SpringBoot的靓车汽车销售网站(附论文)
java·前端·javascript·vue.js·spring boot·spring cloud·开源
龙少95433 分钟前
【深入理解Maven】
java·maven
ThetaarSofVenice23 分钟前
【Spring框架 三】
java·后端·spring
Starshime36 分钟前
【Java】网络套接字
java·开发语言·网络
等什么君!37 分钟前
学习反射(反射的使用,反射的应用场景)
java·开发语言·学习
TT哇39 分钟前
练习题 逆波兰表达式求值
java·开发语言
Little丶Seven1 小时前
Unity Runtime控制编辑器的一些操作
java·unity·编辑器
m0_749317521 小时前
蓝桥杯练习生第四天
java·算法·职场和发展·矩阵·蓝桥杯
JWASX1 小时前
【BUG记录】Apifox 参数传入 + 号变成空格的 BUG
java·bug·urlencoder·urldecoder