spring boot mybatis

1、创建完新模块后,开始创建entity实体类

  • user类
package com.springboot.mybatis.entity;

import lombok.Data;

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

2、创建Mapper类,编写“增删改查”的SQL语句(注意语句正确,并且每个方法后面要手动加一个空格)

  • UserMapper接口
package com.springboot.mybatis.mapper;

import org.apache.ibatis.annotations.*;

import java.util.List;
import com.springboot.mybatis.entity.User;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

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 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(mobile,password,username,avatar)"
            + "VALUES(#{mobile},#{password},#{username},#{avatar})")
    void insert(User user);

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

3、编写service类及其实现impl类

  • UserService接口
package com.springboot.mybatis.service;

import com.springboot.mybatis.entity.Course;
import com.springboot.mybatis.entity.User;

import java.util.List;

public interface UserService {
    List selectAll();

    User getOne(long userId);

    void delete(long userId);

   User insert(User user);

    void update(User user);
}

  • serviceImpl类
package com.springboot.mybatis.service.impl;

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 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 User insert(User user) {
        userMapper.insert(user);
        return user;
    }

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

3、对Impl类进行单元测试

  • 测试代码
package com.springboot.mybatis.service;

import com.springboot.mybatis.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import java.util.List;

import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceImplTest {

@Resource
private UserService userService;

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

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

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

    @Test
    public void insert() {
        User user = new User();
        user.setMobile("18351897198");
        user.setPassword("999999");
        user.setUsername("李老师");
        user.setAvatar("user5.jpg");
        userService.insert(user);
    }

    @Test
    public void update() {
      User user=userService.getOne(5L);
      user.setAvatar("user6.jpg");
      user.setPassword("333333");
      userService.update(user);
    }
}

4、单元测试结果正确之后,创建controller类

package com.springboot.mybatis.controller;

import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.service.UserService;
import org.springframework.web.bind.annotation.*;

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

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

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List 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 = "/user", 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);
    }
}

5、重启服务器,打开postman进行测试,测试结果如下

image
image
image
image
image

你可能感兴趣的:(spring boot mybatis)