自定义Iservice接口BaseMapper BaseEntity BaseController

BaseEntity

package com.example.quartzdemo.common;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Date;

/**
 * @author:xxxxx
 * @create: 2023-01-12 14:00
 * @Description:
 */
@Data
public class BaseEntity {
    private String createUser;
    private String updateUser;
    private Date createTime;
    private Date updateTime;
}

MyService

package com.example.quartzdemo.common;

import com.baomidou.mybatisplus.extension.service.IService;

import java.util.Date;

/**
 *
 * 自定义MyService
 * @param 
 */
public interface MyService extends IService {
    @Override
    default boolean save(T entity) {
        entity.setCreateTime(new Date());
        entity.setCreateUser("admin");
        entity.setUpdateTime(new Date());
        entity.setUpdateUser("superadmin");
        return IService.super.save(entity);
    }
}

UserService继承MyService

package com.example.quartzdemo.service;

import com.example.quartzdemo.common.MyService;
import com.example.quartzdemo.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;

/**
* @author 11385
* @description 针对表【user】的数据库操作Service
* @createDate 2023-01-08 21:59:45
*/
public interface UserService extends MyService {

}

BaseController

package com.example.quartzdemo.common;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author:xxxxx
 * @create: 2023-01-12 14:37
 * @Description:
 */
public class BaseController,E extends BaseEntity> {
    @Autowired
    private T t;

    @RequestMapping("/add")
    public String addEntity(E e){
        t.save(e);
        return "新增成功!";
    }
}

Controller

package com.example.quartzdemo.Controller;

import com.example.quartzdemo.common.BaseController;
import com.example.quartzdemo.pojo.User;
import com.example.quartzdemo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author:xxxxx
 * @create: 2023-01-12 14:41
 * @Description:
 */

@RequestMapping("/test")
@RestController
public class UserController extends BaseController {

}

运行截图

自定义Iservice接口BaseMapper BaseEntity BaseController_第1张图片

你可能感兴趣的:(mybatis,spring,boot,数据库,java)