Welcome everyone to learn the mybatis-plus framework together
study together, work hard together
Table of contents
1. Use mybatis-plus in springboot
1. Add MyBatis Plus dependency in Maven.
2. Common CRUD encapsulation methods for BaseMappe in Mybatis-plus
1. Use mybatis-plus in springboot
1. Add MyBatis Plus dependency in Maven.
For Maven users, pom.xml
add the following dependencies to the file:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>{你的版本}</version>
</dependency>
2. Configure the data source
Configure data source information in ( application.properties
or application.yml
) file:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
3. Create entity classes
Create a Java class that represents a row of records in a database table, and use @TableName
annotations to specify the table name associated with it. For example:
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
}
Lombok is used to simplify the code, and the annotation provided by MyBatis Plus is used @TableId
to identify the primary key, and the primary key generation strategy is specified as auto-increment.
4. Create Mapper interface
Create a Java interface, inherit from it BaseMapper
, and specify the generic type as your entity class. For example:
public interface UserMapper extends BaseMapper<User> {
}
The base class provided by MyBatis Plus is used BaseMapper
, which provides many common CRUD operation methods.
5. Perform query operations
Inject the instance in your Java code UserMapper
, and use it for queries. For example:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAllUsers() {
return userMapper.selectList(null);
}
}
The method provided by MyBatis Plus is used selectList
to query all user records.
2. Common CRUD encapsulation methods for BaseMappe in Mybatis-plus
insert method
// 插入一条记录
int insert(T entity);
// 批量插入多条记录
int insertBatch(List<T> entityList);
// 插入一条记录并返回主键值
int insertAndGetId(T entity);
update method
// 根据主键更新记录
int updateById(T entity);
// 根据主键批量更新记录
int updateBatchById(List<T> entityList);
// 根据条件更新记录
int update(@Param("entity") T entity, @Param("updateWrapper") UpdateWrapper<T> updateWrapper);
delete method
// 根据主键删除记录
int deleteById(Serializable id);
// 根据主键批量删除记录
int deleteBatchIds(Collection<? extends Serializable> idList);
// 根据条件删除记录
int delete(@Param("queryWrapper") QueryWrapper<T> queryWrapper);
query method
// 根据主键查询记录
T selectById(Serializable id);
// 根据主键列表查询记录
List<T> selectBatchIds(Collection<? extends Serializable> idList);
// 查询所有记录
List<T> selectAll();
// 根据条件查询记录数量
int selectCount(@Param("queryWrapper") QueryWrapper<T> queryWrapper);
// 根据条件查询单条记录
T selectOne(@Param("queryWrapper") QueryWrapper<T> queryWrapper);
// 根据条件查询多条记录
List<T> selectList(@Param("queryWrapper") QueryWrapper<T> queryWrapper);
It should be noted that these methods are generic methods, where the type parameter T represents the entity class. If annotations are used in your entity class @TableId
to identify the primary key, MyBatis Plus will automatically set the primary key value according to the primary key generation strategy when calling the above CRUD operation method.
In addition to the basic CRUD operation methods, BaseMapper
it also provides some other useful methods, such as batch insert or update records, pagination query, etc., which will be discussed later