mybatis-plus-demo
Table of Contents
准备工作
实体类User
package com.example.demo.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; private String email; // Getters and Setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
UserMapper
package com.example.demo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.demo.entity.User; public interface UserMapper extends BaseMapper<User> { }
UserService
package com.example.demo.service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.stereotype.Service; @Service public class UserService extends ServiceImpl<UserMapper, User> { }
UserController
- CRUD
- Page
- 自定义SQL
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @PostMapping public User createUser(@RequestBody User user) { userService.save(user); return user; } @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getById(id); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { user.setId(id); userService.updateById(user); return user; } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.removeById(id); } @GetMapping public List<User> getAllUsers() { return userService.list(); } // 分页查询 @GetMapping("/page") public IPage<User> getUsersByPage(@RequestParam int pageNum, @RequestParam int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); return userService.page(page); } // 条件查询 @GetMapping("/search") public List<User> searchUsers(@RequestParam String name, @RequestParam Integer age) { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.like("name", name).eq("age", age); return userService.list(queryWrapper); } }
自定义Sql
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user WHERE age > #{age}") List<User> selectUsersOlderThan(int age); }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService extends ServiceImpl<UserMapper, User> { @Autowired private UserMapper userMapper; public List<User> getUsersOlderThan(int age) { return userMapper.selectUsersOlderThan(age); } }