前后端交互基本使用

1.数据库

创建一个数据库,创建一个user表,设置四个字段 id为主键 name , sex , class

前后端交互基本使用_第1张图片

插入一些数据

 前后端交互基本使用_第2张图片

2.HTML前端页面



	
		
		用户列表展现案例
	
	
		

用户列表展现案例

ID编号 姓名 性别 班级 操作

页面展示前后端交互基本使用_第3张图片

 3.后端java代码

controller层

package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
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 //交给spring容器管理
@RequestMapping("/user")//抽取共性的/user
@CrossOrigin//解决跨域问题
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * URL: localhost:8090/user/findAll
     * 参数: User
     * 返回值结果: 多个结果 用list<>接收
     */
    @GetMapping("/findAll")
    public List findAll(User user) {

      return userService.findAll(user);
    }
}

mapper层

package com.jt.mapper;

import com.jt.pojo.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(User user);
}

pojo

package com.jt.pojo;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data//重写了get,set,,方法
@Accessors(chain = true)//实现了链式连接
public class User implements Serializable {
    private Integer id;
    private String name;
    private String sex;
    private String userClass;
}

service层

接口

package com.jt.service;

import com.jt.pojo.User;

import java.util.List;

public interface UserService {
    List findAll(User  user);
}

接口实现类

package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public List findAll(User user) {

        return userMapper.findAll(user);
    }
}

xml映射文件








    
    
    
    



主启动类

package com.jt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

最终结果

前后端交互基本使用_第4张图片

以上是前后端交互,查询的使用

 

你可能感兴趣的:(前后端交互demo,mysql,java,html,ajax,vue.js)