创建项目
- 添加四个基本依赖
启动项目
报错:
需要配置数据库:
- 创建数据库 test_db
- 创建user表
server.port=8001
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- 重新启动: 并访问:
接口编写
创建UserMapper接口
- 编写实体类和数据库对应
- 在接口中编写查询方法
controller
重启项目,访问:
- 改成UserMapper接口查询数据返回数据
package com.example.learn_from_bilibili3.controller;
import com.example.learn_from_bilibili3.entity.User;
import com.example.learn_from_bilibili3.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
// RestController注解,返回前端数据格式是json
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
UserMapper userMapper;
@GetMapping
public List<User> getUser(){
return userMapper.findAll();
}
}
访问接口:
Post Put Delete
Post方法
接口save方法编写:
package com.example.learn_from_bilibili3.mapper;
import com.example.learn_from_bilibili3.entity.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
List<User> findAll();
@Update("insert into user (name,address,age,sex,phone) values(#{name},#{address},#{age},#{sex},#{phone})")
@Transactional
void save(User user);
}
package com.example.learn_from_bilibili3.controller;
import com.example.learn_from_bilibili3.entity.User;
import com.example.learn_from_bilibili3.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
// RestController注解,返回前端数据格式是json
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
UserMapper userMapper;
@GetMapping
public List<User> getUser(){
return userMapper.findAll();
}
@PostMapping
// RequestBody,把前端传递过来的json变成实体对象
public String addUser(@RequestBody User user){
userMapper.save(user);
return "success";
}
}
重启项目 Postman测试接口:
查看数据库,添加成功:
Put方法
@PutMapping
// 前端必须传过来一个id
public String updateUser(@RequestBody User user){
userMapper.updateById(user);
return "success";
}
@Update("update user set name=#{name},address=#{address},age=#{age},sex=#{sex},phone=#{phone} where id=#{id}")
@Transactional
void updateById(User user);
测试:
Delete方法
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable("id") Long id){
userMapper.deleteById(id);
return "success";
}
@Delete("delete from user where id = #{id}")
void deleteById(Long id);
效果:
查询某一个用户的信息
// 查询某一个用户的信息
@GetMapping("/{id}")
public User findById(@PathVariable("id") Long id){
return userMapper.findById(id);
}
@Select("select * from user where id = #{id}")
User findById(Long id);
效果:
分页查询
- 返回的数据
package com.example.learn_from_bilibili3.vo;
import lombok.Data;
import java.util.List;
@Data
public class Page<T> {
private Integer pageNum;
private Integer pageSize;
private Integer total;
private List<T> data;
}
// 分页
@GetMapping("/page")
public Page<User> findByPage(@RequestParam(defaultValue = "1" ) Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize){
Integer offset = (pageNum -1) * pageSize;
List<User> userData = userMapper.findByPage(offset,pageSize);
Page<User> page = new Page<>();
page.setData(userData);
Integer total = userMapper.countUser();
page.setTotal(total);
page.setPageNum(pageNum);
page.setPageSize(pageSize);
return page;
}
@Select("select * from user limit #{offset},#{pageSize}")
List<User> findByPage(Integer offset, Integer pageSize);
@Select("select count(id) from user")
Integer countUser();
效果: