entity层、DAO层、service层、controller层
User.java
package cn.livorth.functionallearning.entity;
import lombok.Data;
@Data
public class User {
private int userId;
private String userName;
private String password;
}
UserMapper.java,一般操作交给了MybatisPlus,当出现特殊需求的时候再去写Mapper.xml
UserServicepackage cn.livorth.functionallearning.dao;
import cn.livorth.functionallearning.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
UserService.java
package cn.livorth.functionallearning.service;
import cn.livorth.functionallearning.entity.User;
import java.util.List;
public interface UserService {
List<User> getAllUser();
}
UserServiceImpl.java
package cn.livorth.functionallearning.service.impl;
import cn.livorth.functionallearning.dao.UserMapper;
import cn.livorth.functionallearning.entity.User;
import cn.livorth.functionallearning.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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<User> getAllUser() {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
return userMapper.selectList(queryWrapper);
}
}
UserController.java
package cn.livorth.functionallearning.controller;
import cn.livorth.functionallearning.entity.User;
import cn.livorth.functionallearning.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
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
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> categories(){
return userService.getAllUser();
}
}