Mybatis-Plus:简称MP,是对MyBatis功能的增强而不是改变。
Mybatis-Plus的特点:
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3.1version>
dependency>
server:
port: 8080
servlet:
context-path: /springBoot-mybatis-Plus
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
password: "010220"
username: root
url: jdbc:mysql://localhost:3306/mvc?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
redis:
host: localhost
port: 6379
jedis:
pool:
max-idle: 8
max-wait: -1
min-idle: 0
max-active: 8
mvc:
pathmatch:
matching-strategy: ant_path_matcher
aop:
proxy-target-class: true
auto: true
mybatis-plus:
#配置实体类路径
type-aliases-package: com.springbootmybatisplus.demo.dao.entity
configuration:
#配置SQL输出语句
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
MyBatis-Plus注解
package springbootmybatisplus.demo.dao.entity;
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 lombok.ToString;
import java.util.Date;
/**
* @author Una
* @date 2022/8/27 12:26
* @description:
*/
@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "book")//表名
public class Book {
@TableId(value = "book_id")//主键属性
private Integer bookId;
@TableField(value = "book_name")
private String bookName;
@TableField(value = "book_author")
private String bookAuthor;
@TableField(value = "book_date")
private Date bookDate;
@TableField(value = "book_price")
private Integer bookPrice;
}
实现BaseMapper泛型接口,泛型参数是实体类。用户只需要定义自己的Mapper继承自该接口就可以实现单表的CRUD操作
package springbootmybatisplus.demo.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import springbootmybatisplus.demo.dao.entity.Book;
import java.io.Serializable;
/**
* @author Una
* @date 2022/8/27 14:07
* @description:
*/
@Mapper
public interface BookMapper extends BaseMapper<Book> {
@Insert("insert into book values( #{entity.bookId},#{entity.bookName},#{entity.bookAuthor},#{entity.bookDate},#{entity.bookPrice})")
@Override
int insert(Book entity);
@Delete("delect from book where book_id=#{id}")
@Override
int deleteById(Serializable id);
@Update("update book set book_name=#{entity.bookName},book_author=#{entity.bookAuthor},book_date=#{entity.bookDate},book_price=#{entity.bookPrice} where book_id=#{entity.bookId}")
@Override
int updateById(Book entity);
@Select("select * from book where book_id=#{id}")
@Override
Book selectById(Serializable id);
}