MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
这次项目搭建基于【小项目】专栏里的 SSM 实现单表 CRUD,在此基础上整合 MyBatis-Plus
Springboot注解?需要删除原来的 mybatis 依赖和 yml 配置文件中的 mybatis 配置,否则可能会冲突。resources 下的 mapper 文件夹也可以删除了,我们不再需要写 SQL
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version>
</dependency>
虽然不改名也并不影响,但是为了规范还是修改一下。记得修改包名的时候把启动类上的 MapperScan 注解的路径也一并修改了
package com.sisyphus.studentssm.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sisyphus.studentssm.pojo.Student;
import org.springframework.stereotype.Repository;@Repository
public interface StudentMapper extends BaseMapper<Student> {}
package com.sisyphus.studentssm.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;/*** @Description: $* @Param: $* @return: $* @Author: Sisyphus* @Date: $*/
//如果表名和实体类名一致则可以选择不写
@TableName(value = "student")
public class Student {//如果属性名采用驼峰命名 value 可以选择不设置,也就是说 userId 会被自动转换为 user_id//注意!如果字段名也为 userId,那么就会报错,除非设置 value = userId 或者在 yml 中关闭驼峰式编码//@TableId 用于指定主键以及主键类型@TableId(value = "id",type = IdType.INPUT)private Integer id;//@TableField 用于指定普通字段@TableField(value = "name")private String name;@TableField(value = "email")private String email;@TableField(value = "age")private Integer age;@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", email='" + email + '\'' +", age=" + age +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
StudentService 接口可以删除了
package com.sisyphus.studentssm.service;import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sisyphus.studentssm.mapper.StudentMapper;
import com.sisyphus.studentssm.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;/*** @Description: $* @Param: $* @return: $* @Author: Sisyphus* @Date: $*/
@Service
public class StudentServiceImpl implements IService<Student> {@Autowiredprivate StudentMapper studentMapper;//查@Overridepublic Student getById(Serializable id) {return studentMapper.selectById(id);}//删@Overridepublic boolean removeById(Serializable id) {return studentMapper.deleteById(id) > 0;}//增@Overridepublic boolean save(Student entity) {return studentMapper.insert(entity) > 0;}//改@Overridepublic boolean updateById(Student entity) {return studentMapper.updateById(entity) > 0;}//以下方法都是接口强制实现的方法,不用过多关注@Overridepublic boolean saveBatch(Collection<Student> entityList, int batchSize) {return false;}@Overridepublic boolean saveOrUpdateBatch(Collection<Student> entityList, int batchSize) {return false;}@Overridepublic boolean updateBatchById(Collection<Student> entityList, int batchSize) {return false;}@Overridepublic boolean saveOrUpdate(Student entity) {return false;}@Overridepublic Student getOne(Wrapper<Student> queryWrapper, boolean throwEx) {return null;}@Overridepublic Map<String, Object> getMap(Wrapper<Student> queryWrapper) {return null;}@Overridepublic <V> V getObj(Wrapper<Student> queryWrapper, Function<? super Object, V> mapper) {return null;}@Overridepublic BaseMapper<Student> getBaseMapper() {return null;}@Overridepublic Class<Student> getEntityClass() {return null;}
}
package com.sisyphus.studentssm.controller;import com.baomidou.mybatisplus.extension.service.IService;
import com.sisyphus.studentssm.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @Description: $* @Param: $* @return: $* @Author: Sisyphus* @Date: $*/
@RestController
@RequestMapping("student")
public class StudentController {//没有 StudentService 接口了,注入 IService<Student> 接口@Autowiredprivate IService<Student> studentService;@RequestMapping("query")public Student queryById(@RequestParam("id") Integer id){return studentService.getById(id);}@RequestMapping("insert")public void insert(@RequestParam("id") Integer id, @RequestParam("name") String name, @RequestParam("email") String email, @RequestParam("age") Integer age){Student student = new Student();student.setId(id);student.setName(name);student.setEmail(email);student.setAge(age);studentService.save(student);}@RequestMapping("delete")public void deleteById(@RequestParam("id") Integer id){studentService.removeById(id);}@RequestMapping("update")public void update(@RequestParam("id") Integer id, @RequestParam("name") String name, @RequestParam("email") String email, @RequestParam("age") Integer age){Student student = new Student();student.setId(id);student.setName(name);student.setEmail(email);student.setAge(age);studentService.updateById(student);}
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态