SpringBoot集成MyBatis-Plus

SpringBoot集成MyBatis-Plus

文章目录

  • SpringBoot集成MyBatis-Plus
    • application.yml
    • pom.xml
    • pojo
    • mapper
    • service
    • serviceimpl
    • config
    • utils
    • web

SpringBoot集成MyBatis-Plus_第1张图片
懒得打一遍,直接copy: SpringBoot集成MyBatis-Plus

application.yml

# 端口
server:
  port: 8080
# 数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/employee?characterEncoding=utf-8&useSSL=false
    username: 
    password: 

# mybatis-plus设置
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml  #mapper.xml文件位置,如果没有映射文件,请注释掉。

pom.xml


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>SpringBoot_Mybatis_plus_crud_pageartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>SpringBoot_Mybatis_plus_crud_pagename>
    <description>SpringBoot_Mybatis_plus_crud_pagedescription>
    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.1.2version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.30version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

pojo

package com.example.springboot_mybatis_plus_crud_page.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;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@TableName(value = "t_emp") // 当实体类名与表名不一样时,使用
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable {

    //标识为主键
    @TableId(type = IdType.AUTO, value = "emp_id")
    private Integer empId;
    @TableField(value = "emp_name")
    private String empName;
    @TableField(value = "age")
    private String age;
    @TableField(value = "gender")
    private String gender;
    @TableField(value = "email")
    private String email;
}
package com.example.springboot_mybatis_plus_crud_page.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @Author moon
 * @Date 2023/10/3 14:35
 * @Description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result implements Serializable {
    private boolean success;
    private String message;
    private Object data;

    public Result(boolean success, String message) {
        this.success = success;
        this.message = message;
    }
}

mapper

package com.example.springboot_mybatis_plus_crud_page.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface EmpMapper extends BaseMapper<Emp> {

    IPage<Emp> selectPage(Page page);

    Integer addEmp(Emp emp);

}


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot_mybatis_plus_crud_page.mapper.EmpMapper">
    
    <select id="selectPage" resultType="com.example.springboot_mybatis_plus_crud_page.pojo.Emp">
        select * from t_emp
    select>

    
    <insert id="addEmp" keyProperty="empId">
        insert into t_emp values (null,#{empName},#{age},#{gender},#{email})
    insert>

mapper>

service

package com.example.springboot_mybatis_plus_crud_page.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface EmpService{

    List<Emp> finList();

    IPage<Emp> selectPage(Page<Emp> page);

    Emp selectById(Integer id);

    List<Emp> selectList(Wrapper<Emp> queryWrapper);

    Integer addEmp(Emp emp);

    Integer updateById(Emp emp);

    Integer deleteById(Integer id);
}

serviceimpl

package com.example.springboot_mybatis_plus_crud_page.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot_mybatis_plus_crud_page.mapper.EmpMapper;
import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
import com.example.springboot_mybatis_plus_crud_page.service.EmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author moon
 * @Date 2023/10/3 14:03
 * @Description
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    EmpMapper empMapper;

    @Override
    public List<Emp> finList() {
        return empMapper.selectList(null);
    }

    @Override
    public IPage<Emp> selectPage(Page<Emp> page) {
        return empMapper.selectPage(page);
    }

    @Override
    public Emp selectById(Integer id) {
        return empMapper.selectById(id);
    }

    @Override
    public List<Emp> selectList(Wrapper<Emp> queryWrapper) {
        return empMapper.selectList(queryWrapper);
    }

    @Override
    public Integer addEmp(Emp emp) {
        return empMapper.addEmp(emp);
    }

    @Override
    public Integer updateById(Emp emp) {
        return empMapper.updateById(emp);
    }

    @Override
    public Integer deleteById(Integer id) {
        return empMapper.deleteById(id);
    }
}

config

package com.example.springboot_mybatis_plus_crud_page.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Author moon
 * @Date 2023/10/3 15:08
 * @Description
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.example.springboot_mybatis_plus_crud_page.mapper")
public class MyBatisPlusConfig {

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }

}

utils

package com.example.springboot_mybatis_plus_crud_page.utils;

/**
 * 数据统一放回类
 */
public class ResponseResult {
    private Integer state;
    private String msg;
    private Object data;

    public ResponseResult() {
        this.state = 200;
        this.msg = "成功";
    }

    public ResponseResult(Integer state, String msg) {
        this.state = state;
        this.msg = msg;
    }

    public ResponseResult(Integer state, String msg, Object data) {
        this.state = state;
        this.msg = msg;
        this.data = data;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

web

package com.example.springboot_mybatis_plus_crud_page.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
import com.example.springboot_mybatis_plus_crud_page.pojo.Result;
import com.example.springboot_mybatis_plus_crud_page.service.EmpService;
import org.apache.catalina.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author moon
 * @Date 2023/10/3 14:50
 * @Description
 */
@RestController
public class EmpController {
    @Resource
    private EmpService empService;

    //查询所有
    @GetMapping("emp/findList")
    public ResponseEntity<List<Emp>> findList() {
        List<Emp> empList = empService.finList();
        return ResponseEntity.ok(empList);
    }

    //根据id查询
    @GetMapping("emp/{id}")
    public ResponseEntity<Emp> findEmpById(@PathVariable Integer id) {
        return ResponseEntity.ok(empService.selectById(id));
    }

    //条件查询
    @GetMapping("emp/condition")
    public ResponseEntity<List<Emp>> findEmpByCondition(@RequestBody Map<String, String> map) {
        System.out.println(map.get("key") + map.get("value"));
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        return ResponseEntity.ok(empService.selectList(queryWrapper.eq(map.get("key"),map.get("value"))));

    }

    //模糊查询
    @GetMapping("emp/like")
    public ResponseEntity<List<Emp>> findEmpLike(@RequestBody Map<String, String> map) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        return ResponseEntity.ok(empService.selectList(queryWrapper.like(map.get("key"),map.get("value"))));
    }

    //插入
    @PostMapping("emp/add")
    public ResponseEntity<Integer> addEmpOne(@RequestBody Emp emp) {
        return ResponseEntity.ok(empService.addEmp(emp));
    }

    //通过id修改
    @PutMapping("emp/update/{id}")
    public ResponseEntity<Integer> update(@PathVariable Integer id) {
        Emp emp = empService.selectById(id);
        return ResponseEntity.ok(empService.updateById(emp));
    }

    //通过id删除
    @DeleteMapping("emp/delete/{id}")
    public ResponseEntity<Integer> deleteById(@PathVariable Integer id) {
        System.out.println(id);
        return ResponseEntity.ok(empService.deleteById(id));
    }

    @GetMapping("emp/selectPage")
    public IPage<Emp> selectPage() {
        IPage<Emp> empIPage = empService.selectPage(new Page<>(1,5));
        return empIPage;
    }
}

你可能感兴趣的:(spring,boot,mybatis,后端)