数据访问:@Db 一站式注入
多数据源配置YAMLsolon.dataSources:db1:class: com.zaxxer.hikari.HikariDataSourceurl: jdbc:mysql://localhost/testdriverClassName: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456db2:url: jdbc:mysql://localhost/orderdriverClassName: com.mysql.cj.jdbc.Driverusername: rootpassword: 1234565.2 Db 注入// Solon 一行搞定多数据源Db(“db1”) UserMapper userMapper;Db(“db2”) OrderMapper orderMapper;对比 Spring Boot 多数据源需要配置多个 DataSource 多个 SqlSessionFactory 多个 TransactionManager MapperScan… Solon 使用 Db。5.3 ORM 集成ORM Spring Boot 依赖 Solon 依赖MyBatis mybatis-spring-boot-starter mybatis-solon-pluginMyBatis-Plus mybatis-plus-spring-boot-starter mybatis-plus-solon-pluginJPA spring-boot-starter-data-jpa solon-data-jpaEasy-Query easy-query-spring-boot-starter easy-query-solon-plugin5.4 事务Solon Spring Boot 说明Transaction Transactional 声明式事务Transaction(policy TranPolicy.requires_new) Transactional(propagation Propagation.REQUIRES_NEW) 事务传播策略Solon 的 Transaction 默认遇到任何异常都回滚无需指定异常类型。六、测试迁移Solon Spring Boot 说明SolonTest(App.class) SpringBootTest 测试启动SolonTest(env“test”) ActiveProfiles(“test”) 测试环境HttpTester MockMvc HTTP 接口测试Rollback Transactional Rollback 事务回滚Import(profiles…) TestPropertySource 导入测试属性HttpTester 用法示例SolonTest(App.class)public class UserControllerTest extends HttpTester {Test public void testGetUser() { // GET 请求 String resp path(/users/1).get(); assertEquals(200, resp.code()); // POST 请求 String json {\name\:\test\}; String resp2 path(/users).body(json).post(); assertContains(resp2, test); }}七、其他常见对照场景 Spring Boot Solon拦截器 HandlerInterceptor Around 注解或 Solon Filter定时任务 Scheduled Scheduled同名 EnableScheduling文件上传 MultipartFile UploadedFile用法一致RPC 调用 FeignClient NamiClient注册发现 EnableDiscoveryClient 无需注解引入插件自动生效配置刷新 RefreshScope 无需注解配置变更自动感知八、迁移检查清单POMspring-boot-starter-parent → solon-parentPOMspring-boot-starter-* → solon-* / *-solon-plugin配置application.yml → app.yml环境文件同理启动类SpringBootApplication → SolonMain启动SpringApplication.run() → Solon.start()IoCAutowired → InjectIoCService/Repository → ComponentWebRestController → ControllerWebRequestMapping → MappingWebPathVariable → PathWebHttpServletRequest → Context配置Value → Inject(“${…}”)数据spring.datasource.* → solon.dataSources.*数据Transactional → Transaction测试SpringBootTest → SolonTest测试MockMvc → HttpTester九、写在最后这篇对照表覆盖了日常开发中最常用的 90% 场景。Solon 的注解体系并非简单的换皮它在统一性