springboot集成mybatisplus
1) 引入相关jar包
<!-- 引入mybatis-plus包 --> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- 引入mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2) 编写配置文件
spring: datasource:
# driver-class-name:
url: jdbc:mysql://127.0.0.1:3306/DEMO_DB?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=GMT%2B8
username: root
password: "123456"
3) 配置包扫描路径(必须,要不然启动报错)
@SpringBootApplication@EnableWebMvc
@ComponentScan("com.zhoulp.demo")
// 一个*代表任意字符串,但只代表一级包,比如可以扫到com.zhoulp.aaa.mapper,不能扫到com.zhoulp.aaa.bbb.mapper
// 两个*代表任意个包,比如可以扫到com.zhoulp.aaa.mapper,也可以扫到com.zhoulp.aaa.bbb.mapper
@MapperScan({"com.zhoulp.demo.**.dao"})
public class DemoWebApplication {
public static void main(String[] args) {
SpringApplication.run(DemoWebApplication.class, args);
}
}
4) 实现表映射类,必须有@TableName,@TableId注解
@ApiModel@TableName("t_user")
public class UserEntity {
@ApiModelProperty(value = "用户ID")
@TableId("id")
private String id;
@ApiModelProperty(value = "手机号")
@TableField("mobile")
private String mobile;
}
5) 编写Mapper接口,继承BaseMapper
@Repositorypublic interface UserMapper extends BaseMapper<UserEntity> {
}
6) 编写Service层,接口继承IService接口,实现类继承ServiceImpl
public interface UserService extends IService<UserEntity> {}
@Named("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
}
7) 添加分页插件(分页必须)
@Configuration@EnableAsync
public class MyBatisPlusConfig {
/**
* <p>分页插件</p>
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor interceptor = new PaginationInterceptor();
interceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return interceptor;
}
}
以上是 springboot集成mybatisplus 的全部内容, 来源链接: utcz.com/z/519110.html