yml typeAliasesPackage:
时间: 2023-08-31 18:07:59 浏览: 80
在配置文件中,`typeAliasesPackage`是用来指定扫描别名的包路径。它的作用是将指定包路径下的类注册为别名,方便在配置文件中使用别名代替类的全限定名。例如:
```yaml
mybatis:
typeAliasesPackage: com.example.entity
```
上述配置表示扫描`com.example.entity`包下的所有类,并将它们注册为别名。这样,在使用这些类的时候就可以直接使用别名,而不需要写类的全限定名。
相关问题
mybatisplus typeAliasesPackage
### MyBatis Plus `typeAliasesPackage` 配置方法及作用
#### 一、配置方式
为了使 MyBatis Plus 能够自动识别并处理特定包中的实体类,可以通过设置 `typeAliasesPackage` 来实现这一功能。具体来说,在项目的 application.yml 或者 application.properties 中加入如下所示的配置:
对于 YAML 格式的配置文件而言:
```yaml
mybatis-plus:
typeAliasesPackage: com.example.project.entity,com.example.another.package.entity
```
上述代码指定了两个不同的 Java 包路径给 `typeAliasesPackage` 属性[^1]。
而对于 properties 格式的配置,则应采用下面的形式书写:
```properties
mybatis-plus.typeAliasesPackage=com.example.project.entity,com.example.another.package.entity
```
这里同样定义了多个包作为别名注册的目标位置[^3]。
值得注意的是,当项目构建工具为 Maven 并且版本较新时,确保所使用的 Spring Boot 和 MyBatis Plus 版本兼容是非常重要的。例如在一个典型的开发环境中,JDK 版本设为 1.8 ,数据库选用 MySQL 5.5 及以上版本,IDE 使用 IDEA 2018.2 或更新版次,Maven 的版本不低于 3.3.9,而 Spring Boot 应该处于 2.1.0.RELEASE 左右,MyBatis Plus 则建议至少达到 3.0.5 这样的稳定发行版本[^4]。
#### 二、属性的作用
一旦正确设置了 `typeAliasesPackage` 参数之后,MyBatis 将会自动扫描指定目录下的所有类,并为之创建相应的类型别名。这些别名可以在 XML 形式的 SQL 映射文档里方便地引用,从而简化查询语句编写工作量的同时提高了可读性和维护效率。
另外需要注意的一点是在某些情况下即使 IDE (比如 IntelliJ IDEA) 内部测试能够正常运作,但在实际部署过程中可能会遇到问题——即打包后的应用程序无法按照预期加载自定义类型的别名。这可能是由于在应用初始化阶段未能及时触发相关组件完成必要的资源解析操作所致。因此有必要确认诸如 `afterPropertiesSet()` 方法在内的生命周期回调函数已被适当重写以便于强制执行此类任务;同时也要保证像 `buildSqlSessionFactory()` 函数这样的核心逻辑能够在服务启动期间顺利调用到,进而顺利完成对 `typeAliasesPackage` 下各条目数据源对象实例化前的相关准备工作[^2]。
最全的application.yml配置与bootstrap.yml的区别
### Spring Boot `application.yml` 和 `bootstrap.yml` 区别
#### 加载顺序差异
在 Spring Boot 应用启动过程中,`bootstrap.yml` 或者 `bootstrap.properties` 会优先于 `application.yml` 或者 `application.properties` 被加载。这意味着如果存在相同属性配置,则 `bootstrap.yml` 中的配置项将会覆盖掉 `application.yml` 中相同的配置项[^1]。
#### 使用场景区分
- **Bootstrap Context**: 主要负责应用程序上下文初始化之前的工作,比如从远程仓库拉取配置信息、激活某些特性开关等操作;通常用于微服务架构下的分布式配置管理工具集成(如 Spring Cloud Config),此时需要提前获取到必要的元数据以便后续正常工作。
- **Application Context**: 则更侧重于定义具体业务逻辑层面的内容,例如数据库连接池大小调整、定时任务调度策略设定等方面。这些参数往往是在本地环境中直接生效而无需依赖外部资源提供支持[^2]。
对于使用了 Spring Cloud Config Server 的情况来说,在 `bootstrap.yml` 内部应当指明诸如 `spring.application.name`, `spring.cloud.config.server.git.uri` 这样的基础环境变量以及涉及敏感信息安全传输的相关选项,从而确保能够顺利取得远端存储着的一系列默认或个性化定制过的设置值并将其传递给即将创建出来的 Application Context 实例对象去解析应用[^3]。
### 最全 `application.yml` 配置示例
以下是较为完整的 `application.yml` 文件结构及其常见配置项:
```yaml
server:
port: 8080 # 设置服务器监听端口
spring:
datasource:
url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/*.xml
typeAliasesPackage: com.example.demo.entity
logging:
level:
root: info
com.example.demo: debug
file:
name: logs/demo.log
security:
oauth2:
client:
registration:
github:
clientId: ${GITHUB_CLIENT_ID}
clientSecret: ${GITHUB_CLIENT_SECRET}
management:
endpoints:
web:
exposure:
include: "*"
feign:
hystrix:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/
zuul:
ignored-services: '*'
routes:
user-service:
path: /users/**
serviceId: USER-SERVICE
```
此段代码展示了如何在一个典型的基于 Spring Boot 开发的企业级 Java Web 应用程序中合理组织各类资源配置条目,涵盖了从基本的服务端口号指定直至高级别的熔断机制控制等多个方面[^4]。
阅读全文
相关推荐
















