Mybatis-plus 使用及注意事项
Mybatis-plus官网(不定时无无)
1. pml依赖
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<!-- mybatis plus自动代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<!--模板引擎freemarker或默认的Velocity引擎模板-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<!-- mybatis-plus-join联表查询-->
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join</artifactId>
<version>1.2.4</version>
</dependency>
模板引擎freemarker可更换为默认的Velocity引擎模板
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
2.配置文件(yaml为例)
spring:
thymeleaf:
cache: false
mybatis-plus:
type-aliases-package: com/euphoria/shop/entity
mapper-locations: classpath:com/euphoria/shop/mapper/xml/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
- 若mapper位置位于
resource 下,mapper-locations 可使用默认值 mapper-locations: classpath*:/mapper/**/*.xml
3.MybatisPlusConfig 配置类
@MapperScan("com.euphoria.mapper")
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
//分页
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
//乐观锁
@Bean
public MybatisPlusInterceptor OptimisticLockerInnerInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
4.实体类中注解开发
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableLogic
private Integer deleted;
5.使用mybatis-plus-join联表查询
依赖
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join</artifactId>
<version>1.2.4</version>
</dependency>
使用前需将mapper接口继承的baseMapper 改为MPJBaseMapper
public interface UserCartMapper extends MPJBaseMapper<UserCart>。。。
使用案例
IPage<UserCartVo> goodsPage = baseMapper.selectJoinPage(new Page<>(currentPage, pageSize), UserCartVo.class,
new MPJLambdaWrapper<UserCartVo>().selectAll(UserCart.class)
.select(Goods::getName, Goods::getPicture, Goods::getPriceNew, Goods::getPriceOld, Goods::getStore)
.leftJoin(Goods.class, Goods::getGoodsId, UserCart::getGoodsId)
.eq(UserCart::getUserId, userCart)
.orderByDesc(UserCart::getUserCartId)
);
复杂联表建议还是自写xml⑧hhhhhh.
6. mybatis plus自动代码生成器(数据自改)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
在测试类中运行:
public static void main(String[] args) {
String propath = System.getProperty("user.dir");
FastAutoGenerator.create("jdbc:mysql://localhost:3306/shop?characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
"root", "123456")
.globalConfig(builder -> {
builder.disableOpenDir()
.author("euphoria")
.enableSwagger()
.outputDir(propath + "/src/main/java");
})
.packageConfig(builder -> {
builder.parent("com.euphoria")
.moduleName("shop")
.entity("entity")
.service("Service")
.serviceImpl("Service.impl")
.mapper("mapper")
.controller("controller");
})
.strategyConfig(builder -> {
builder.addInclude("user")
.entityBuilder()
.enableLombok()
.versionColumnName("version")
.versionPropertyName("version")
.logicDeleteColumnName("deleted")
.logicDeletePropertyName("deleted")
.addTableFills(new Column("create_time", FieldFill.INSERT))
.addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
.idType(IdType.ASSIGN_UUID)
.controllerBuilder()
.enableRestStyle()
.serviceBuilder()
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImp")
;
})
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
7.注意事项(BUG日记)
-
启动类上mapperscan 扫描需写对,即@MapperScan("com.euphoria.shop.mapper")
否则报错UnsatisfiedDependencyException: Error creating bean with name ‘userServiceImp’
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sqlSessionFactory' defined in class path resource
[com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]
Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException:
Failed to instantiate[org.apache.ibatis.session.SqlSessionFactory]: Factory method'sqlSessionFactory' threw
exception; nested exception is org.apache.ibatis.type.TypeException: The alias
'Collection' is already mapped to the value 'java.util.Collection'.
解决:修改类名,避免重复
|