1.导入依赖
<!-- 导入lombok 的相关依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 导入数据源 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-plus 是自己开发的,非官方的!-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
1.Mapper类的书写
@Mapper // dao操作在这里开启来
public interface AccountMapper extends BaseMapper<Account> {
}
2.Service类的使用
public interface AccountService extends IService<Account> {
}
// 对应的实现类
@Service // 在这里继承 mp 提供的实现类 和 实现自己的接口类对象
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
implements AccountService {
}
3.要看见相关的sql语句的话就要配置日记
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 在控制台上面打印
4.常用注解
@TableName("表名")
当表名与实体类名不一致时,可以在实体类上加入@TableName()声明
`@TableId`声明属性为表中的主键(若属性名称不为默认id)
`@TableFieId("字段")` 当实体类属性与表字段不一致时,可以用来声明
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "account") // 指定sql的名字
public class Account {
@TableId(type = IdType.AUTO)
private Integer id;
@TableField("cardno")
private String cardno;
private String password;
private Double balance;
private Integer status;
}
5.使用分页查询
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}