3.4 Solon Integration
Hint
This article is generated by AI translation.
What is Solon
An all-scenario Java enterprise framework: lean, efficient, and open. Concurrency +300%; memory -50%; startup 10x faster; package size -90%; compatible with Java 8 ~ 23. A Spring alternative.
- Solon project: https://solon.noear.org/
Features
- Auto-configured DataSource
- Mapper interface injection
- Multiple DataSource
- Transaction support
Setup
First add the dependency, current version: 6.4.0
Maven dependencies
<dependency>
<groupId>net.hasor</groupId>
<artifactId>dbvisitor-solon-plugin</artifactId>
<version>latest version</version>
</dependency>
Use HikariCP
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
</dependency>
Configuration
solon.dataSources:
default!: # 数据源名称(尾部 ! 标识默认数据源)
class: "com.zaxxer.hikari.HikariDataSource"
jdbcUrl: "jdbc:mysql://127.0.0.1:13306/devtester?allowMultiQueries=true&user=root&password=123456"
dbvisitor:
default: # 对应的数据源名称
mapperLocations: classpath:dbvisitor/mapper/*.xml
mapperPackages: net.hasor.dbvisitor.test.dao.*
Inject a Mapper
@Controller
public class HelloController {
@Inject
private UserMapper userMapper;
@Get
@Mapping("/hello")
public String hello(String name) {
return ...
}
}
Example
Inject types
JdbcOperationsinterface, implemented by JdbcTemplateLambdaOperationsinterface, implemented by LambdaTemplateConfigurationSessionCustom Mapperinterfaces
Inject tips
- Types above support @Db and @Inject
- Use @Db("name") to select a data source when multiple are configured
Transactions
Control transactions with @Tran
import org.noear.solon.data.annotation.Tran;
public class TxExample {
@Tran(policy = TranPolicy.required)
public void exampleMethod() throws SQLException {
...
}
}
Configuration reference
| Key | Description |
|---|---|
| mapperLocations | 可选 Locations of Mapper XML files; default dbvisitor/mapper/*.xml |
| mapperPackages | 可选 Packages to scan for Mapper interfaces; separate multiple with , |
| mapperDisabled | 可选 true/false to disable Mapper interfaces discovered via dbvisitor.mapper-packages; default false. Set true if conflicts arise. |
| markerAnnotation | 可选 Annotation that marks an interface as a Mapper; default net.hasor.dbvisitor.mapper.MapperDef |
| markerInterface | 可选 Interface that marks an interface as a Mapper; default net.hasor.dbvisitor.mapper.Mapper |
| autoMapping | 可选 Automatically map all fields to columns; true = auto, false = require @Column |
| camelCase | 可选 Convert table/column names using camel-case to underscore |
| useDelimited | 可选 Force identifier quoting for table/column/index names (e.g., when names are keywords); default false |
| caseInsensitive | 可选 Case-insensitive table/column names; default true |
| ignoreNonExistStatement | 可选 Ignore missing mappings when binding Mapper methods to XML; default false (throws) |
| dialect | 可选 Default database dialect |