解析Java框架中entity層,mapper層,service層,controller各層作用

一、entity層

別名: model層 ,domain層
用途: 實(shí)體層,用于存放我們的實(shí)體類,與數(shù)據(jù)庫中的屬性值基本保持一致,實(shí)現(xiàn)set和get的方法。
例子:user表的實(shí)體User

public class User {
    /**
     * 用戶id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 用戶昵稱
     */
    private String nick;

    /**
     * 手機(jī)號(hào)
     */
    private String mobile;

    /**
     * 頭像地址
     */
    @Column(name = "head_image_url")
    private String headImageUrl;

  
    /**
     * 獲取用戶id
     *
     * @return id - 用戶id
     */
    public Long getId() {
        return id;
    }

    /**
     * 設(shè)置用戶id
     *
     * @param id 用戶id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * 獲取用戶昵稱
     *
     * @return nick - 用戶昵稱
     */
    public String getNick() {
        return nick;
    }

    /**
     * 設(shè)置用戶昵稱
     *
     * @param nick 用戶昵稱
     */
    public void setNick(String nick) {
        this.nick = nick;
    }

    /**
     * 獲取手機(jī)號(hào)
     *
     * @return mobile - 手機(jī)號(hào)
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * 設(shè)置手機(jī)號(hào)
     *
     * @param mobile 手機(jī)號(hào)
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * 獲取頭像地址
     *
     * @return head_image_url - 頭像地址
     */
    public String getHeadImageUrl() {
        return headImageUrl;
    }

    /**
     * 設(shè)置頭像地址
     *
     * @param headImageUrl 頭像地址
     */
    public void setHeadImageUrl(String headImageUrl) {
        this.headImageUrl = headImageUrl;
    }
   
}

二、mapper層

別名: dao層
用途: 對(duì)數(shù)據(jù)庫進(jìn)行數(shù)據(jù)持久化操作,他的方法語句是直接針對(duì)數(shù)據(jù)庫操作的,主要實(shí)現(xiàn)一些增刪改查操作,在mybatis中方法主要與與xxx.xml內(nèi)相互一一映射。
示例:userMapper

public interface userMapper {
    int deleteByPrimaryKey(Long id);

    int insert(user record);

    int insertSelective(user record);

    user selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(user record);

    int updateByPrimaryKey(user record);
}

三、service層

用途:業(yè)務(wù)service層,給controller層的類提供接口進(jìn)行調(diào)用。一般就是自己寫的方法封裝起來,就是聲明一下,具體實(shí)現(xiàn)在serviceImpl中。
示例:UserService

public interface UserService extends Service<User> {

}

四、controller層

別名:web 層
用途: 控制層,負(fù)責(zé)具體模塊的業(yè)務(wù)流程控制,需要調(diào)用service邏輯設(shè)計(jì)層的接口來控制業(yè)務(wù)流程。因?yàn)閟ervice中的方法是我們使用到的,controller通過接收前端H5或者App傳過來的參數(shù)進(jìn)行業(yè)務(wù)操作,再將處理結(jié)果返回到前端。
示例:UserController

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/add")
    public Result add(User user) {
        userService.save(user);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        userService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(User user) {
        userService.update(user);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        User user = userService.findById(id);
        return ResultGenerator.genSuccessResult(user);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<User> list = userService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容