SpringBoot项目创建(Layui增删改查)

一:创建SpingBoot项目

点击新建项目

SpringBoot项目创建(Layui增删改查)_第1张图片

 然后点击下一步

SpringBoot项目创建(Layui增删改查)_第2张图片

 然后点击完成

SpringBoot项目创建(Layui增删改查)_第3张图片

 然后创建一个新的index.html页面

SpringBoot项目创建(Layui增删改查)_第4张图片

 SpringBoot项目创建(Layui增删改查)_第5张图片

 有可能你的会报错所以加一个取消数据源的自动配置的一个注解。

SpringBoot项目创建(Layui增删改查)_第6张图片

 application.properties里面还要配置一下Static

SpringBoot项目创建(Layui增删改查)_第7张图片

 然后就出来了

SpringBoot项目创建(Layui增删改查)_第8张图片

 二:SpringBoot项目的书写

SpringBoot项目创建(Layui增删改查)_第9张图片

 UsersController:

package com.hp.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Stadium;
import com.hp.entity.User;
import com.hp.service.Service;
import com.hp.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/login")
public class UsersController {

    @Resource
    private Service service;
    @PostMapping("/login")
    @ResponseBody
    public Object login(User user, HttpServletRequest request) {
        user =service.login(user);
        if(user!=null){
            request.getSession().setAttribute("user",user);
            return JsonData.buildSuccess(200);
        }else{
            return JsonData.buildError("账号密码错误!");
        }
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(Stadium obj, @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",defaultValue = "10")Integer limit) {
        PageHelper.startPage(page,limit);
        List list = service.find(obj);
        return JsonData.buildSuccess(new PageInfo<>(list));
    }

    @RequestMapping("/del")
    @ResponseBody
    public  Object  del(Integer sid){
        int  i=service.del(sid);
        return  JsonData.buildSuccess(i>0?"删除成功!":"删除失败!");
    }

    @RequestMapping("/add")
    @ResponseBody
    public  Object  add(User  user){
        int i=service.add(user);
        return  JsonData.buildSuccess(i>0?"添加成功!":"添加失败!");
    }
}

 dao.mapper

package com.hp.dao;

import com.hp.entity.Stadium;
import com.hp.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface mapper {
    List login(User user);
    List find(Stadium  stu);
    int  del(Integer  sid);
    int add(User user);
}

实体类:entity

package com.hp.entity;

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

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Stadium {
        /**
    *
    **/
    private  Integer sid;
    /**
    *
    **/
    private  String sname;
    /**
    *
    **/
    private  String address;
    /**
    *
    **/
    private  String description;
    /**
    *
    **/
    private Date begintime;
    /**
    *
    **/
    private  Date endtime;

}
package com.hp.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
        /**
    *
    **/
    private  Integer uid;
    /**
    *
    **/
    private  String username;
    /**
    *
    **/
    private  String pwd;
    /**
    *
    **/
    private  String tel;
    /**
    *
    **/
    private  String sex;
    /**
    *
    **/
    private  String idcard;

}

Service:

package com.hp.service;

import com.hp.entity.Stadium;
import com.hp.entity.User;

import java.util.List;

public interface Service {
    User login(User  user);
    List find(Stadium  stu);
    int  del(Integer  sid);
    int add(User user);
}

Serviceimpl

package com.hp.service;

import com.hp.dao.mapper;
import com.hp.entity.Stadium;
import com.hp.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
@org.springframework.stereotype.Service
public class Serviceimpl implements Service {
    @Resource
    private mapper mapper;
    @Override
    public User login(User user) {
        List list = mapper.login(user);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List find(Stadium stu) {
        return mapper.find(stu);
    }

    @Override
    public int del(Integer sid) {
        return mapper.del(sid);
    }

    @Override
    public int add(User user) {
        return mapper.add(user);
    }
}

util:

package com.hp.util;

/**
 * 返回json数据
 */
public class JsonData {
    private Integer code;   //状态码 , 200代表成功; -1代表失败
    private Object data;    //返回的数据
    private String msg;     //当前json信息描述

    public JsonData() {}

    public JsonData(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    /**
     * 操作成功,没有数据返回,携带自定义描述信息
     * @param msg
     * @return
     */
    public static JsonData buildSuccess(String msg){
        return new JsonData(200,null,msg);}

    /**
     * 操作成功,有数据返回,携带自定义描述信息
     * @param data
     * @return
     */
    public static JsonData buildSuccess(Object data,String msg){
        return new JsonData(200,data,msg); }
    public static JsonData buildSuccess(Object data){
        return new JsonData(200,data,null); }

    /**
     * 操作失败,没有数据,携带自定义描述信息
     * @param msg (自定义)
     * @return
     */
    public static JsonData buildError(String msg){
        return new JsonData(-1,null,msg);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }



    @Override
    public String toString() {
        return "JsonData{" +
                "code=" + code +
                ", data=" + data +
                ", msg='" + msg + '\'' +
                '}';
    }
}

前台使用layui,或者使用postman进行测试

你可能感兴趣的:(Spring,Springboot,layui,前端,javascript)