springboot项目写接口案例

一、项目配置

示例:application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.5.131:3306/datav?characterEncoding=UTF-8
    username: root
    password: 123456
mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml
  type-aliases-package: com.echarts.demo.entity

二、编写mapper文件

1.mapper

代码如下(示例):

springboot项目写接口案例_第1张图片

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.echarts.demo.dao.StudentDAO">
    <resultMap id="studentMap" type="Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="age" property="age"></result>
    </resultMap>
    <select id="queryStudents"  resultMap="studentMap">
        SELECT * FROM student
    </select>
</mapper>

2.dao

代码如下(示例):

springboot项目写接口案例_第2张图片

package com.echarts.demo.dao;

import com.echarts.demo.entity.Student;

import java.util.List;

public interface StudentDAO {

    List<Student> queryStudents();

}

3.entity

代码如下(示例):

springboot项目写接口案例_第3张图片

package com.echarts.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;
    private int age;

}

4.service

代码如下(示例):

springboot项目写接口案例_第4张图片

package com.echarts.demo.service;

import com.echarts.demo.entity.Student;

import java.util.List;

public interface StudentService {
     List<Student> queryStudents();
}



package com.echarts.demo.service.impl;

import com.echarts.demo.dao.StudentDAO;
import com.echarts.demo.entity.Student;
import com.echarts.demo.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDAO studentDAO;

    @Override
    public List<Student> queryStudents() {
        return studentDAO.queryStudents();
    }
}



5.controller

代码如下(示例):

package com.echarts.demo.controller;


import com.echarts.demo.entity.Student;
import com.echarts.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ss")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    public List<Student> list(  String name){
        System.out.println("============="+ name);
        List<Student> list = studentService.queryStudents();
        return list;
    }
}




你可能感兴趣的:(大数据hadoop,echarts,spring,boot,java)