create table t_user(
id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL,
PRIMARY KEY ( tutorial_id )
);
import lombok.Data;
/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
@Data
public class User {
private Integer id;
private String userName;
private String password;
private String phone;
}
UserMapper.java
import com.andy.framdemo.entity.User;
/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
application中增加mapper扫描目录
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.andy.framdemo.mapper")
public class FramdemoApplication {
public static void main(String[] args) {
SpringApplication.run(FramdemoApplication.class, args);
}
}
UserService.java
import com.andy.framdemo.entity.User;
import com.andy.framdemo.mapper.UserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public int addUser(User user){
user.setUserName(user.getUserName());
user.setPassword(user.getPassword());
user.setPhone(user.getPhone());
return userMapper.insert(user);
}
}
UserController.java
import com.andy.framdemo.entity.User;
import com.andy.framdemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
@Api(description = "用户接口")
@Controller
@RequestMapping(value = "/api/v1/users")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation(value = "新增用户" , notes="新增用户")
@RequestMapping(value = "",method = RequestMethod.POST)
@ResponseBody
public int adduser(@RequestBody User user){
return userService.addUser(user);
}
}