SpringBoot整合mybatis-plus实现增删改查

文章目录

    • 0. 代码下载
    • 1.项目所需的依赖
    • 2.配置文件
    • 3.实体类
    • 4. mapper
    • 5.配置mapper.xml文件
    • 6.service
    • 7.serviceImpl
    • 8.controller

0. 代码下载

代码下载链接:mybatis-plus-demo的代码
里面有数据库表的生成文件
注意可能需要新建一个blog-demo的数据库

1.项目所需的依赖

  		<dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.2.2version>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.70version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

2.配置文件

server:
  port: 8082

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: mysql
    url: jdbc:mysql://localhost:3306/blog-demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
  mvc:
    view:
      prefix: /
      suffix: .html


#logging:
#  file:
#    name: log/log.log
#  level:
#    root: info
#    hhf:
#      mybatisplusdemo : debug

mybatis-plus:
  type-aliases-package: hhf.mybatisplusdemo.entity
  mapper-locations: classpath:/hhf/mybatisplusdemo/mapper/*.xml

3.实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    private Integer stuNum;
    private String stuName;
    private Integer age;
    private String gender;
    private LocalDate birthday;
}

4. mapper

StudentMapper继承了mybatis-plus的

@Mapper
public interface StudentMapper extends BaseMapper<Student> {

}

在springboot的启动类上添加注解
@MapperScan(“mapper接口的相对路径”)

5.配置mapper.xml文件

与mapper接口路径一致
SpringBoot整合mybatis-plus实现增删改查_第1张图片
里面没有填写方法,是个空的


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="hhf.mybatisplusdemo.mapper.StudentMapper">

mapper>

6.service

IStudentService继承了mybatis-plus的IService

@Service
public interface IStudentService extends IService<Student> {

}

7.serviceImpl

serviceImpl继承了mybatis-plus的serviceImpl并实现了IStudentService

@Service("IStudentService")
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

}

8.controller

controller类中写明了方法

@RestController
@RequestMapping("/student")
public class StudentController {
	@Resource
	private IStudentService iStudentService;

	/**
	 * 存储新用户
	 */
	@GetMapping("/save")
	public void save(){
		Student student = new Student(44, 232323, "小芳", 19, "女", LocalDate.now());
		boolean save = iStudentService.save(student);

	}

	/**
	 * 根据id删除用户
	 * @param id
	 */
	@GetMapping("/deleteById")
	public void deleteById(@PathParam("id") Integer id){
		boolean b = iStudentService.removeById(id);
	}

	/**
	 * 根据id修改用户
	 */
	@GetMapping("/updateById")
	public void updateById(){
		Student student = new Student(44, 3453454, "小明", 19, "女", LocalDate.now());
		boolean b = iStudentService.updateById(student);
	}

	/**
	 * 根据id搜索用户
	 * @param id
	 * @return
	 */
	@GetMapping("/getById")
	public String getById(@PathParam("id") Integer id){
		Student student = iStudentService.getById(id);
		System.out.println(student);
		return student.toString();
	}
	/**
	 * mybatis-plus测试使用queryWrapper,  根据id查询用户
	 * @param id
	 * @return
	 */
	@GetMapping("/getOneById")
	public Student getOneById(@PathParam("id") Integer id){
		QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id",id);
		Student student = iStudentService.getOne(queryWrapper);
		return student;
	}

	/**
	 * 根据学生名查询学生信息
	 * @param stuName
	 * @return
	 */
	@GetMapping("/getOneByStuName")
	public Student getOneByStuName(@PathParam("stuName") String stuName){
		System.out.println("------------------------------------------");
//        设置查询条件
		QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("stu_name",stuName);

		Student student = iStudentService.getOne(queryWrapper);
		System.out.println("-----------------------------------------");
		System.out.println(student.toString());
		return student;
	}

	/**
	 * 按照性别查询所有符合要求的所有用户
	 * @return
	 */
	@GetMapping("/getStudentListBySex")
	public List<Student> list(@PathParam("gender") String gender){
//
		QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gender", gender);

		List<Student> list = iStudentService.list(queryWrapper);
		return list;
	}

	/**
	 * 按照年龄查询所有符合要求的所有用户
	 * @return
	 */
	@GetMapping("/listStudentListByAge")
	public List<Student> listStudentListByAge(@PathParam("age") Integer age){
//
		QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("age", age);

		List<Student> list = iStudentService.list(queryWrapper);
		return list;
	}

}

你可能感兴趣的:(SpringBoot,spring,boot,java,intellij-idea,mybatis-plus)