一、SpringBoot整合Mybatis框架
1. 创建项目
2. 引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBoot2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot2</name>
<description>SpringBoot2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis框架依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<!--mysql 驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--pagehelper 分页框架依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
<!--swagger的依赖引入-->
<dependency>
<groupId>io.github.jianzhichun</groupId>
<artifactId>spring-boot-starter-swagger2</artifactId>
<version>0.0.1</version>
</dependency>
<!--swagger ui 依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<!--实体类注解依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--单元测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.配置文件
#数据源信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# characterEncoding防止您添加到数据的数据出现乱码。
spring.datasource.url=jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
#指定mybatis映射文件的路径 classpath编译后的路径
mybatis.mapper-locations=classpath:mapper/*.xml
#控制台打印sql日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4.创建实体类
pojo
package com.aaa.pojo;
import lombok.Data;
@Data
public class Student {
private Integer sid;//学生id
private String name;//学生名字
private Integer age;//学生年龄
private String sex;//学生性别
private Integer cid;//学生班级id
private String headimg;//学生头像
}
5.创建Dao层
dao
需要在类上加@Mapper注解
package com.aaa.dao;
import com.aaa.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentDao {
//查询所有学生数据
public List<Student> findALl();
}
6.创建Mapper
<?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.aaa.dao.StudentDao">
<resultMap id="baseMap" type="com.aaa.pojo.Student">
<id column="sid" property="sid"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<result column="cid" property="cid"/>
<result column="headImg" property="headImg"/>
</resultMap>
<select id="findALl" resultMap="baseMap">
select * from student
</select>
</mapper>
7.测试
package com.aaa;
import com.aaa.dao.StudentDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBoot5ApplicationTests {
@Autowired
private StudentDao studentDao;
@Test
void find() {
studentDao.findALl().forEach(System.out::println);
}
}
脑图
dao层方法通过mapper中的sql语句对数据库数据进行处理
前提是mapper中sql语句要被springBoot框架扫描
单元测试中要调用dao方法的时候用到了依赖注入,
前提是dao层要加mapper注解交给spring Boot Ioc容器处理
(也可以在主类上中加@MapperScan(basePackages = “com.example.springboot2.dao”))
二、SpringBoot整合pageHelper框架
1.引入依赖
<!--pagehelper 分页框架依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
2.测试代码
@Test
void contextLoads() {
PageHelper.startPage(2,3);
List<Dept> all = deptDao.findAll();
//把查询的结果封装到PageInfo中
PageInfo<Dept> pageInfo=new PageInfo<>(all);
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("当前页的记录:");
List<Dept> list = pageInfo.getList();
list.stream().forEach(System.out::println);
}
三、SpringBoot整合Mybatis实现crud
controller层
package com.example.springboot2.controller;
import com.example.springboot2.pojo.Student;
import com.example.springboot2.service.StudentService;
import com.example.springboot2.vo.Result;
import com.example.springboot2.vo.StudentVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/student")
@Api(tags = "学生类接口")
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping
@ApiOperation(value = "添加学生信息")
public Result inset(@RequestBody Student student){
int i = studentService.insertSelective(student);
return i>0?new Result(200,"添加成功",null):new Result(500,"添加失败",null);
}
@PutMapping
@ApiOperation(value = "修改学生信息")
public Result update2(@RequestBody Student student){
int i = studentService.updateByPrimaryKey(student);
return i>0?new Result(200,"修改成功",null):new Result(500,"修改失败",null);
}
@DeleteMapping
@ApiOperation(value = "根据id删除学生信息")
@ApiImplicitParams(
@ApiImplicitParam(paramType = "query",name = "id",value = "学生编号")
)
public Result delte(Integer id){
int i = studentService.deleteByPrimaryKey(id);
return i>0?new Result(200,"删除成功",null):new Result(500,"删除失败",null);
}
@GetMapping
@ApiOperation(value = "根据id查询学生")
@ApiImplicitParams(
@ApiImplicitParam(paramType = "query",name = "id",value = "学生编号")
)
public Result select(Integer id){
Student student = studentService.selectByPrimaryKey(id);
return student!=null? new Result(200,"查询成功",student):new Result(500,"查询失败",null);
}
@PostMapping("select")
@ApiOperation(value = "分页模糊查询学生")
public Result select2(Integer current, Integer pageSize,StudentVo studentVo){
PageInfo<Student> pageInfo = studentService.selectBy(current, pageSize, studentVo);
return pageInfo!=null?new Result(200,"查询成功",null):new Result(500,"查询失败",null);
}
}
dao层
package com.example.springboot2.dao;
import com.example.springboot2.pojo.Student;
import com.example.springboot2.vo.StudentVo;
import java.util.List;
/**
* @author LIYANGCUNZHUANG
* @description 针对表【student(学生表)】的数据库操作Mapper
* @createDate 2023-06-16 14:47:14
* @Entity com.example.springboot2.pojo.Student
*/
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);//删除
int insert(Student record);//添加
int insertSelective(Student record);//模糊添加
Student selectByPrimaryKey(Integer id);//根据id查询数据
List<Student> selectfindAll(StudentVo studentVo);
int updateByPrimaryKeySelective(Student record);//模糊修改
int updateByPrimaryKey(Student record);//根据id修改
}
pojo层
package com.example.springboot2.pojo;
import java.io.Serializable;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.print.attribute.standard.Sides;
/**
* 学生表
* @TableName student
*/
@Data
@ApiModel
public class Student implements Serializable {
/**
* 学号
*/
@ApiModelProperty(name = "sid",value = "学生编号")
private Integer sid;
/**
* 姓名
*/
@ApiModelProperty(name = "name",value = "学生名字")
private String name;
/**
* 性别
*/
@ApiModelProperty(name = "age",value = "学生年龄")
private Integer age;
/**
* 性别
*/
@ApiModelProperty(name = "sex",value = "学生性别")
private String sex;
/**
* 班级号
*/
@ApiModelProperty(name = "cid",value = "学生班级号")
private Integer cid;
/**
*
*/
@ApiModelProperty(name = "headimg",value = "学生头像")
private String headimg;
private static final long serialVersionUID = 1L;
}
service层
Studentservice
package com.example.springboot2.service;
import com.example.springboot2.pojo.Student;
import com.example.springboot2.vo.StudentVo;
import com.github.pagehelper.PageInfo;
public interface StudentService {
int deleteByPrimaryKey(Integer id);//删除
int insert(Student record);//添加
int insertSelective(Student record);//模糊添加
Student selectByPrimaryKey(Integer id);//根据id查询数据
int updateByPrimaryKeySelective(Student record);//模糊修改
int updateByPrimaryKey(Student record);//根据id修改
PageInfo<Student> selectBy(Integer current, Integer pageSize, StudentVo studentVo);
}
StudentServiceImpl
package com.example.springboot2.service.impl;
import com.example.springboot2.dao.StudentMapper;
import com.example.springboot2.pojo.Student;
import com.example.springboot2.service.StudentService;
import com.example.springboot2.vo.StudentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
@Override
public int deleteByPrimaryKey(Integer id) {
int i = studentMapper.deleteByPrimaryKey(id);
return i;
}
@Override
public int insert(Student record) {
int insert = studentMapper.insert(record);
return insert;
}
@Override
public int insertSelective(Student record) {
int i = studentMapper.insertSelective(record);
return i;
}
@Override
public Student selectByPrimaryKey(Integer id) {
Student student = studentMapper.selectByPrimaryKey(id);
return student;
}
@Override
public int updateByPrimaryKeySelective(Student record) {
int i = studentMapper.updateByPrimaryKeySelective(record);
return i;
}
@Override
public int updateByPrimaryKey(Student record) {
int i = studentMapper.updateByPrimaryKey(record);
return i;
}
@Override
public PageInfo<Student> selectBy(Integer current, Integer pageSize, StudentVo studentVo) {
PageHelper.startPage(current,pageSize);
List<Student> students = studentMapper.selectfindAll(studentVo);
PageInfo<Student> pageInfo = new PageInfo<>(students);
return pageInfo;
}
}
vo层
package com.example.springboot2.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Result {
//表示状态码
private Integer code;
//消息提示
private String msg;
//响应的数据内容
private Object data;
}
config层
package com.aaa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket docket() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("阿娇")
.apiInfo(getInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.aaa.controller"))
.build();
return docket;
}
private ApiInfo getInfo() {
Contact DEFAULT_CONTACT = new Contact("阿娇", "https://www.baidu.com", "110@qq.com");
ApiInfo apiInfo = new ApiInfo("学生管理系统", "学生管理系统",
"1.0", "http://localhost:8080/doc.html", DEFAULT_CONTACT, "致远有限公司", "https://www.baidu.com");
return apiInfo;
}
}