javaweb第七次课后作业

 EmpController

package controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import service.EmpService;

import java.util.List;

public class EmpController {
    @Autowired
    private EmpService empService;

    @RequestMapping("/findAll")
    public List<Emp> findAll(){
        return   empService.findAll();
    }

    @RequestMapping("/findAllJsoon")
    public Result findAllJson(){
        return  Result.success(empService.findAll()) ;
    }
    @RequestMapping("/deleteEmp")
    public void deleteEmp(Integer id){
        empService.deleteEmp(id);
    }
    @RequestMapping("/empfindById/{id}")
    public Result empfindById(@PathVariable("id") Integer id) {
        return  Result.success(empService.empfindById(id));
    }

    @RequestMapping("/peotupdate")
    public  Result updateEmp(@RequestBody Emp emp){
        boolean r = empService.updatEmp(emp);

        if(r) {
            // 成功  code==1
            return Result.success();
        } else {
            // 失败  code==0
            return Result.erro("更新失败");
        }
    }

    @RequestMapping("/peotinsert")
    public Result insertUser(@RequestBody Emp emp){
        boolean result =empService.insert(emp);
        if(result) {
            // 成功  code==1
            return Result.success();
        } else {
            // 失败  code==0
            return Result.erro("添加失败");

        }

    }

}

Emp.java

package com.itheima.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    private Integer id;
    private String name;
    private Short gender;
    private String dynasty;
    private String title;
    private String style;


}

Result.java

package com.itheima.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private Integer code;//响应码,1 代表成功; 0 代表失败
    private String msg;  //响应信息 描述字符串
    private Object data; //返回的数据
    //增删改 成功响应
    public static Result success(){
        return new Result(1,"success",null);
    }
    //查询 成功响应
    public static Result success(Object data){
        return new Result(1,"success",data);
    }
    //失败响应
    public static Result error(String msg){
        return new Result(0,msg,null);
    }
}

EmpMapper

package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EmpMapper {

//    //根据id删除数据
//    @Delete("delete from emp where id = #{id}")
//   public int delete(Integer id);
//
//    //新增
//    @Insert("insert into emp(id, name, gender, dynasty, title, style)"
//    +"values (#{id},#{name},#{gender},#{dynasty},#{title},#{style})")
//    public void insert(Emp emp);
//    @Update("update emp set id=#{id},name=#{name},gender=#{gender},"
//            +"dynasty=#{dynasty},title=#{title},style=#{style} where id=#{id}")
//    public void update(Emp emp);
//    @Select("select * from emp where id=#{id}")
//    public Emp selectById(Integer id);
//    //@Select("select * from emp where name like '%${name}%'")
//    public List<Emp> list(String name);
//
//    @Select("select * from peom")
//        public List<Emp> findAll();
@Select("select * from emp")
public List<Emp> findAll();

    @Delete("delete from emp where id=#{id}")
    public int deleteEmp(Integer id);

    @Select("select * from emp where id=#{id}")
    public Emp peotfindById(Integer ID);

    @Update("update emp set author=#{author},gender=#{gender},dynasty=#{dynasty},title=#{title} ,style=#{style} where id=#{id} ")
    public  boolean updateEmp(Emp emp);

    @Insert("insert into emp(id,name, gender, dynasty, title, style) values (#{id},#{name}, #{gender}, #{dynasty}, #{title}, #{style})")
    public int insert(Emp emp);


    Emp empfindById(Integer id);
}

EmpService

package service;

import com.itheima.pojo.Emp;

import java.util.List;

public interface EmpService {
    public List<Emp> findAll();

    updateEmp(Emp emp);

    int deleteEmp(Integer id);

    Object empfindById(Integer id);

    boolean insert(Emp emp);

}

EmpServicaImpl

package service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import service.EmpService;

import java.util.List;

public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Emp> findAll() {
        return empMapper.findAll();
    }
    @Override
    public Emp empfindById(Integer id) {
        return empMapper.empfindById(id);
    }

    @Override
    public boolean updateEmp(Emp emp) {
        return empMapper.updateEmp(emp);
    }

    @Override
    public int deleteEmp(Integer id) {
        return empMapper.deleteEmp(id);
    }

    @Override
    public boolean  insertUser(Emp emp) {
        int result =  empMapper.insert(emp);
        return result == 1;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值