SpringBoot集成MyBatis-Plus
文章目录
- SpringBoot集成MyBatis-Plus
- application.yml
- pom.xml
- pojo
- mapper
- service
- serviceimpl
- config
- utils
- web
懒得打一遍,直接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
<?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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBoot_Mybatis_plus_crud_page</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot_Mybatis_plus_crud_page</name>
<description>SpringBoot_Mybatis_plus_crud_page</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--lombok简化开发插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot支持的是jpa,mybatisplus自己做了启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!--spring-test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</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);
}
<?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.springboot_mybatis_plus_crud_page.mapper.EmpMapper">
<!--IPage<Emp> selectPage(Page page);-->
<select id="selectPage" resultType="com.example.springboot_mybatis_plus_crud_page.pojo.Emp">
select * from t_emp
</select>
<!--Boolean addEmp(Emp emp);-->
<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;
}
}