mybatis-plus使用、以及springboot项目出初始创建

mybatis-plus使用:
快速开始参考:

一、创建并初始化数据库

创建数据库,创建数据表,添加数据

二、初始化工程

创建springboot工程

三、添加依赖

1.引入依赖

在pom.xml中:
spring-boot-starter、spring-boot-starter-test
添加:mybatis-plus-boot-starter、MySQL、lombok、

	
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.0.5version>
    dependency>

注意:引入 MyBatis-Plus 之后不能再次引入 MyBatis 以及 MyBatis-Spring

2.在项目中使用Lombok

需要先在idea中下载插件

    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
    dependency>

四、配置

application.properties 配置文件中添加 MySQL 数据库的相关配置:

# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

注意

1、这里的 url 使用了 ?serverTimezone=GMT%2B8 后缀

2、这里的 driver-class-name 使用了 com.mysql.cj.jdbc.Driver

五、编写代码

1、实体

创建包 entity 编写实体类 User.java

@Data
public class User {
     
	//设置主键策略
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private String name;
    private Integer age;
    private String email;
	//自动填充
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
	//自动更新
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

	//版本号
    @Version
    @TableField(fill = FieldFill.INSERT)
    private Integer version;

    //逻辑删除
    @TableLogic
    @TableField(fill = FieldFill.INSERT)
    private Integer deleted;
}

2、mapper

创建包 mapper 编写Mapper 接口: UserMapper.java

public interface UserMapper extends BaseMapper<User> {
     
    
}

3、配置类

在 配置类中添加 @MapperScan 注解,扫描 Mapper 文件夹
注意:扫描的包名根据实际情况修改

@Configuration
@MapperScan("com.shenan.mpdemo.mapper")
public class MpConfig {
     
	......
}

六、开始使用

添加测试类,进行功能测试:

七、配置日志

application.properties中添加,查看sql输出日志

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

总结

1.application.properties配置文件主要写的是spring全局配置,mysql配置mybatis-plus配置等配置信息
2.pom.xml配置文件主要写的是引入依赖,以及引入版本
3.tips:修改完pom.xml文件,就要更新一下maven

你可能感兴趣的:(mybatis-plus使用、以及springboot项目出初始创建)