springboot整合mybatisplus (详细)
文章目录
- springboot整合mybatisplus (详细)
- 1.项目目录
- 2.pom.xml
- 3.StudentInfo
- 4.StudentController
- 5.StudentService
- 6.StudentServiceImpl
- 7.StudentMapper
- 8.StudentMapper.xml
- 9.application.yml
- 10 表sql
- 11.执行成功例子
- 12 完整代码地址
1.项目目录

2.pom.xml
<!--Mybatis-plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!--mysql的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
3.StudentInfo
package com.example.schedulelockscreen.model;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("student")
public class StudentInfo {
@TableField("id")
private String id;
@TableField("sname")
private String sname;
@TableField("classId")
private String classId;
@TableField("birthday")
private String birthday;
@TableField("email")
private String email;
}
4.StudentController
package com.example.schedulelockscreen.controller;
import com.example.schedulelockscreen.IService.StudentService;
import com.example.schedulelockscreen.model.StudentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController {
@Autowired(required = false)
private StudentService studentService;
@RequestMapping("getInfo")
public StudentInfo getStudentInfo(){
StudentInfo byId = null;
try {
byId = studentService.getById(2);
} catch (Exception e) {
e.printStackTrace();
}
return byId;
}
@RequestMapping("/insert")
public void insertInfo(StudentInfo studentInfo){
StudentInfo info=new StudentInfo();
info.setId(studentInfo.getId());
info.setSname(studentInfo.getSname());
info.setClassId(studentInfo.getClassId());
info.setBirthday(studentInfo.getBirthday());
info.setEmail(studentInfo.getEmail());
studentService.save(info);
}
@RequestMapping("/selectAll")
public List<StudentInfo> selectAll(){
return studentService.list();
}
@RequestMapping("/update")
public void updateById(StudentInfo studentInfo){
StudentInfo info=new StudentInfo();
info.setId(studentInfo.getId());
info.setSname(studentInfo.getSname());
info.setClassId(studentInfo.getClassId());
info.setBirthday(studentInfo.getBirthday());
info.setEmail(studentInfo.getEmail());
studentService.updateById(info);
}
@RequestMapping("/delete")
public void deleteById(String id){
studentService.removeById(id);
}
}
5.StudentService
package com.example.schedulelockscreen.IService;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.schedulelockscreen.model.StudentInfo;
public interface StudentService extends IService<StudentInfo> {
}
6.StudentServiceImpl
package com.example.schedulelockscreen.IService.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.schedulelockscreen.IService.StudentService;
import com.example.schedulelockscreen.Mapper.StudentMapper;
import com.example.schedulelockscreen.model.StudentInfo;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentInfo> implements StudentService {
}
7.StudentMapper
package com.example.schedulelockscreen.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.schedulelockscreen.model.StudentInfo;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StudentMapper extends BaseMapper<StudentInfo> {
}
8.StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.schedulelockscreen.Mapper.StudentMapper">
</mapper>
9.application.yml
#端口号8080
server:
port: 8083
#数据库名:mysql,用户名root,密码123456
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.jdbc.Driver
# mybatis-plus配置
mybatis-plus:
# xml文件位置
mapper-locations: classpath:mapper
10 表sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` varchar(2) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生ID',
`sname` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生姓名',
`classId` varchar(3) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '班级ID',
`birthday` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生生日',
`email` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生电子邮箱'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '32423', '1', '4', '234');
INSERT INTO `student` VALUES ('2', '34', '3', '3', '3');
SET FOREIGN_KEY_CHECKS = 1;
11.执行成功例子

12 完整代码地址
https://download.csdn.net/download/weixin_48616345/90441555