美文网首页JAVA
04SpringBoot项目练习

04SpringBoot项目练习

作者: 国王兔子 | 来源:发表于2019-03-18 20:07 被阅读0次

1.建表

b.png

2.定义实体类User

public class User {
@Data
    private Long userId;
    private String mobile;
    private String password;
    private String username;
    private String avatar;
}

UserMapper

import com.springboot.mybatis.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    @Results({@Result(column = "user_id", property = "userId"),
            @Result(column = "mobile", property = "mobile"),
            @Result(column = "password", property = "password"),
            @Result(column = "username", property = "username"),
            @Result(column = "avatar", property = "avatar")
    })
    @Select("SELECT * FROM t_sys_user ")
    List<User> selectAll();


    @Results({@Result(column = "user_id", property = "userId"),
            @Result(column = "mobile", property = "mobile"),
            @Result(column = "password", property = "password"),
            @Result(column = "username", property = "username"),
            @Result(column = "avatar", property = "avatar")
    })
    @Select("SELECT * FROM t_sys_user WHERE user_id = #{userId} ")
    User getOne(Long userId);

    @Delete("DELETE FROM t_sys_user WHERE user_id = #{userId} ")
    void delete(Long userId);

    @Insert("INSERT INTO t_sys_user(user_id,mobile,password,username,avatar) VALUES(#{userId},#{mobile},#{password},#{username},#{avatar})")
    void insert(User user);

    @Update("UPDATE t_sys_user SET username=#{username},password=#{password},avatar=#{avatar} WHERE user_id=#{userId} ")
    void update(User user);
}

UserService

public interface UserService {
    List<User> selectAll();

    User getOne(long userId);

    void delete(long userId);

    void insert(User user);

    void update(User user);
}

UserServiceImpl

import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.mapper.UserMapper;
import com.springboot.mybatis.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User getOne(long userId) {
        return userMapper.getOne(userId);
    }

    @Override
    public void delete(long userId) {
        userMapper.delete(userId);
    }

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }
}

UserServiceImplTest

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceImplTest {

    @Resource
    private UserService userService;

    @Test
    public void selectAll() {
        List<User> userList = userService.selectAll();
        userList.forEach(user -> System.out.println(user));
    }

    @Test
    public void getOne() {
        User user = userService.getOne(1);
        System.out.println(user);
    }

    @Test
    public void delete() {
        userService.delete(10);
    }

    @Test
    public void insert() {
        User user = new User();
        user.setUserId(10L);
        user.setMobile("455115");
        user.setPassword("12345824");
        user.setUsername("李山");
        user.setAvatar("b8.jpg");
        userService.insert(user);
    }

    @Test
    public void update() {
        User user = userService.getOne(7L);
        user.setAvatar("1.jpg");
        userService.update(user);
    }
}

UserController

@RestController
@RequestMapping(value = "/api")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public List<User> selectAll() {
        return userService.selectAll();
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public User getOne(@PathVariable("id") long id) {
        return userService.getOne(id);
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable("id") long id) {
        userService.delete(id);
    }

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public void addUser(@RequestBody User user) {
        userService.insert(user);
    }

    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public void updateUser(@RequestBody User user) {
        userService.update(user);
    }
}

Postman运行结果

delect.png one.png

相关文章

网友评论

    本文标题:04SpringBoot项目练习

    本文链接:https://www.haomeiwen.com/subject/isabmqtx.html