欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

前后端分离开发的后端代码简单案例

程序员文章站 2024-03-20 20:22:40
...

前后端分离开发的后端代码简单案例

1.pom依赖

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<!-- 数据库连接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
<!-- mybatis-plus启动器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!-- 代码生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>

    <!-- lombok辅助工具 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2. 代码实例

用mybatis-plus启动器生成代码,并进行了修改
对应数据库student表格
前后端分离开发的后端代码简单案例
student类

@Data
@EqualsAndHashCode(callSuper = false)
//@ApiModel(value="Student对象", description="")
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;

    private String sSex;


}

mapper

public interface StudentMapper extends BaseMapper<Student> {

}

IStudentService

public interface IStudentService extends IService<Student> {

}

StudentServiceImpl

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

}

自定义返回数据的类R

//用于返回数据的封装
@Data
public class R {
    private Integer code;
    private Boolean status;
    private Map<String,Object> data =new HashMap<>();
    private String message;


    //私有构造方法
    private R(){}

    //链式编程
    public static R ok(){
        R r = new R();
        r.setStatus(true);
        r.setCode(200);
        return  r;
    }
    public static R error(String message,Integer code){
        R r =new R();
        r.setStatus(false);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    //数据封装
    public R put(String key,Object value){
        this.data.put(key,value);
        return this;
    }


}

controller

@RestController
@RequestMapping("/stu/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    //RestFul风格
    @GetMapping
    public R list(){
        return  R.ok().put("data",studentService.list());
    }

}

application.properties

# 微服务命名
spring.application.name=stu-service
#端口声明
server.port=8090


#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/temporary?useUnicode=true&useSSL=false&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

显示结果:
前后端分离开发的后端代码简单案例

相关标签: 代码 代码