赞
踩
为简化开发,提升效率而生,对mybatis功能进行增强,但未做改变。
支持任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库。
file --> settings --> Plugins --> Marketplace中搜索MyBatisX --> Installed
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--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>
application.properties:改为application.yml
application.yml:
#端口号8080
server:
port: 8080
#数据库名:mysql,用户名root,密码123456
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis-plus配置
mybatis-plus:
# xml文件位置
mapper-locations: classpath:mapper/*.xml
/src/main/java/com.example.demo下设置:service,controller,mapper,entity包
/src/main/resouces下设置mapping文件夹
controller创建StudentController类;
service下创建StudentService接口,并创建impl包,创建StudentServiceImpl类;
mapper下创建StudentMapper接口;
entity下创建StudentInfo类;
mapping文件夹下创建StudentMapper.xml文件;(可不建此文件,mybatis-plus在写基础功能时不需要xml文件手写sql,复杂sql不在本次讨论范围内)
(本次未使用分页等功能,不需配置config)
CREATE TABLE student(
id VARCHAR(2) COMMENT '学生ID',
sname VARCHAR(20) COMMENT '学生姓名',
classId VARCHAR(3) COMMENT '班级ID',
birthday VARCHAR(5) COMMENT '学生生日',
email VARCHAR(20) COMMENT '学生电子邮箱'
);
INSERT INTO student(id,sname,class_id,birthday,email)
VALUES(1,'张三',101,1016,'1@163.com'),(2,'李四',101,511,'2@163.com'),
(3,'王五',101,1016,'3@163.com'),(4,'赵六',103,615,'4@163.com');
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("student")
public class StudentInfo {
//若id为主键,则为@TableId("id")
@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;
}
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.StudentInfo;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StudentMapper extends BaseMapper<StudentInfo> {
}
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.StudentInfo;
public interface StudentService extends IService<StudentInfo> {
}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.StudentInfo;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentInfo> implements StudentService {
}
import com.example.demo.entity.StudentInfo;
import com.example.demo.service.StudentService;
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
@RequestMapping("/demo11")
public class StudentController {
@Autowired(required = false)
private StudentService studentService;
/**
* 查询学生信息
* @param id
* @return
*/
@RequestMapping("getInfo/{id}")
public StudentInfo getStudentInfo(@PathVariable String id){
return studentService.getById(id);
}
/**
* 插入学生信息
* @param studentInfo
*/
@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);
}
/**
* 查询全部学生信息
* @return
*/
@RequestMapping("/selectAll")
public List<StudentInfo> selectAll(){
return studentService.list();
}
/**
* 根据id更新学生表信息
* @param studentInfo
*/
@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);
}
/**
* 根据id删除学生信息
* @param id
*/
@RequestMapping("/delete")
public void deleteById(String id){
studentService.removeById(id);
}
}
URL:
localhost:8080/demo11/getInfo/2
URL:
localhost:8080/demo11/insert?id=5&sname=小红&classId=103&birthday=1231&email=5@163.com
URL:
localhost:8080/demo11/delete?id=5
URL:
localhost:8080/demo11/update?id=4&sname=小明&classId=103&birthday=1231&email=4@163.com
URL:
localhost:8080/demo11/selectAll
对控制台输出效果不满意,可添加日志使内容更详细,请移步为SpringBoot项目添加日志:slf4j
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。