Skip to content

快速开始

springboot2 搭建

Maven 依赖

yaml
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>cn.mybatis-mp</groupId>
            <artifactId>mybatis-mp-spring-boot-parent</artifactId>
            <version>1.7.6-RC5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>cn.mybatis-mp</groupId>
        <artifactId>mybatis-mp-spring-boot-starter</artifactId>
    </dependency>
</dependencies>

导入 jdbc 、连接池 依赖

JDBC 、连接池 Maven依赖

yaml
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
</dependency>

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

YML配置

在 application.yml 中配置:

yaml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/dbName
    username: dbusername
    password: dbpassword

使用代码示例

启动类

java
@SpringBootApplication
@MapperScan("com.xx.xxx.mapper")
public class MybatisMpApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisMpApplication.class, args);
    }
}

实体类

java
@Data
@Table
public class SysUser {

    @TableId
    private Integer id;

    private String userName;

    private String password;
    
    private Integer roleId;

    private LocalDateTime createTime;
}

Mapper

继承 MybatisMapper 即可

java
public interface SysUserMapper extends MybatisMapper<SysUser> {

}

如何使用

java
@Service
public class Test{

    @Autowired
    private SysUserMapper sysUserMapper;

    public void demo() {
        Pager<SysUser> pager = QueryChain.of(sysUserMapper)
                .eq(SysUser::getId,1)
                .like(SysUser::getUserName,"xxx")
                .paging(Pager.of(1,10));
    }
}

如何 简不简单!!!

更多示例