说明:
// 插入一条记录
int insert(T entity);
参数说明
类型 | 参数 | 描述 |
---|---|---|
T | entity | 实体对象 |
// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Wrapper | wrapper | 实体对象封装操作类(可以为null) |
Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) |
Serializable | id | 主键ID |
Map<String,Object> | columnMap | 表字段map对象 |
// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象(set条件值,可为null) |
Wrapper | updateWrapper | 实体对象封装操作类(可以为null,里面的entity用于生成where语句) |
// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Serializable | id | 主键ID |
Wrapper | queryWrapper | 实体对象封装操作类(可以为null) |
Collection<? extends Serializable> | idList | 主键ID列表(不能为null以及empty) |
Map<String,Object> | columnMap | 表字段map对象 |
IPage | page | 分页查询条件(可以为RowBounds.DEFAULT) |
<?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.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baomidou.mybatisplus</groupId>
<artifactId>mybatisplus01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisplus01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</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>
package com.baomidou.mybatisplus.bean;
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.Data;
/**
* @TableName
* value:指定当前实体类所对应的表名
*/
@Data
@TableName(value = "tbl_employee")
public class Employee {
/**
* @TableId:
* value:指定表中的主键列的列名,如果实体属性名和列名一致,可以省略不指定
* type:指定主键策略
*/
@TableId(value = "id",type = IdType.AUTO)
private Integer id; //int
@TableField(value = "last_name")
private String lastName;
@TableField(value = "email")
private String email;
@TableField(value = "gender")
private Integer gender;
@TableField(value = "age")
private Integer age;
// @TableField(exist = false)
// private Double salary;
}
package com.baomidou.mybatisplus.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
package com.baomidou.mybatisplus.mapper;
import com.baomidou.mybatisplus.bean.Employee;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface EmployeeMapper extends BaseMapper<Employee> {
}
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/mp?&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# mybatis-plus配置控制台打印sql语句:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
数据库数据如下:
@Test
public void testCommonInsert(){
//初始化Eployee对象
Employee employee=new Employee();
employee.setLastName("MP");
employee.setEmail("mp@guigu.com");
employee.setGender(1);
employee.setAge(22);
//插入到数据库
//insert方法在插入时,会根据实体类的每个属性进行非空判断,只有非空的属性对应
//的字段才会出现到sql语句中
Integer result = employeeMapper.insert(employee);
System.out.println("result:"+result);
//获取当前数据在数据库中的主键值
Integer key=employee.getId();
System.out.println("key="+key);
}
截图中得transactional这些错误是我没开启事务,先忽略这个问题。
@Test
public void testCommonUpdate(){
//初始化修改对象
Employee employee=new Employee();
employee.setId(6);
employee.setLastName("汤姆老师");
employee.setEmail("tom@sina.com");
employee.setGender(1);
employee.setAge(22);
Integer result = employeeMapper.updateById(employee);
System.out.println("result="+result);
}
(1) 根据id进行删除
Integer result = employeeMapper.deleteById(8);
System.out.println("result="+result);
(2)根据条件进行删除
Map<String,Object> columnMap=new HashMap<>();
columnMap.put("last_name","Tom");
columnMap.put("email","tom@atguigu.com");
Integer result = employeeMapper.deleteByMap(columnMap);
System.out.println("result="+result);
(1)查询所有
@Test
public void testEmployee(){
List<Employee> employeeList=employeeMapper.selectList(null);
employeeList.forEach(System.out::println);
}
(2)通过id查询
Employee employee = employeeMapper.selectById(6);
System.out.println(employee);
(3)通过多个列查询(selectOne若查出多条数据会报错)
QueryWrapper<Employee> employeeQueryWrapper= Wrappers.query();
employeeQueryWrapper.eq("id",6);
employeeQueryWrapper.eq("last_name","汤姆老师");
employeeQueryWrapper.eq("gender",1);
Employee employee = employeeMapper.selectOne(employeeQueryWrapper);
System.out.println(employee);
(4)通过多个id进行查询
List<Integer> idList=new ArrayList<>();
idList.add(2);
idList.add(5);
idList.add(6);
List<Employee> employeeList = employeeMapper.selectBatchIds(idList);
employeeList.forEach(System.out::println);
(5)通过Map封装条件查询
Map<String,Object> columnMap=new HashMap<>();
columnMap.put("last_name","汤姆老师");
columnMap.put("gender",1);
List<Employee> employeeList = employeeMapper.selectByMap(columnMap);
employeeList.forEach(System.out::println);