3.4 Solon Integration
What is Solon
A full-scenario Java enterprise application development framework: restrained, efficient, open, ecological! Concurrency increased by 300%; memory usage reduced by 50%; startup time faster by 10 times; package size smaller by 90%; compatible with java8 ~ java23 at the same time. (Can replace Spring)
- Solon Project URL: https://solon.noear.org/
Features
- Auto-configure Datasource
- Mapper Interface Injection
- Multi-datasource Support
- Support Transaction Control
Configuration Method
First, add the dependency, current version: 6.7.0
Maven Dependency
<dependency>
<groupId>net.hasor</groupId>
<artifactId>dbvisitor-solon-plugin</artifactId>
<version>latest version</version>
</dependency>
Using HikariCP as Database Connection Pool
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
</dependency>
Configuration File
solon.dataSources:
default!: # Datasource name (with ! at the end identifying default datasource)
class: "com.zaxxer.hikari.HikariDataSource"
jdbcUrl: "jdbc:mysql://127.0.0.1:13306/devtester?allowMultiQueries=true&user=root&password=123456"
dbvisitor:
default: # Corresponding datasource name
mapperLocations: classpath:dbvisitor/mapper/*.xml
mapperPackages: net.hasor.dbvisitor.test.dao.*
Inject Mapper
@Controller
public class HelloController {
@Inject
private UserMapper userMapper;
@Get
@Mapping("/hello")
public String hello(String name) {
return ...
}
}
public interface UserMapper extends BaseMapper<UserDTO> {
...
}
Example
Injectable Types
- JdbcOperations interface, JdbcTemplate is its implementation
- LambdaOperations interface, LambdaTemplate is its implementation
- Configuration class
- Session class
- Custom Mapper
Relevant Classes
- net.hasor.dbvisitor.jdbc.JdbcOperations
- net.hasor.dbvisitor.jdbc.core.JdbcTemplate
- net.hasor.dbvisitor.lambda.LambdaOperations
- net.hasor.dbvisitor.lambda.LambdaTemplate
- net.hasor.dbvisitor.transaction.TransactionTemplate
- net.hasor.dbvisitor.session.Configuration
- net.hasor.dbvisitor.session.Session
- net.hasor.dbvisitor.mapper.BaseMapper
Injection Hint
- The above injected types support @Db, @Inject annotations
- @Db can specify the data source name under multi-data source via @Db("name")
Using Transaction
Transaction control via @Tran annotation
import org.noear.solon.data.annotation.Tran;
public class TxExample {
@Tran(policy = TranPolicy.required)
public void exampleMethod() throws SQLException {
...
}
}
Configuration Items
| Property Name | Description |
|---|---|
| mapperLocations | 可选 Path to scan for Mapper mapping files. Default is dbvisitor/mapper/*.xml. |
| mapperPackages | 可选 Package names to scan for Mapper interface definitions. Use , to separate multiple packages. |
| mapperDisabled | 可选 Use true/false to disable Mapper interfaces scanned by dbvisitor.mapper-packages. Default is false. Consider setting to true if conflicts occur when sharing the same mapper file with other frameworks. |
| markerAnnotation | 可选 Set a scan path so that only Mapper interfaces annotated with a specific annotation are recognized as Mapper interface classes. Default: net.hasor.dbvisitor.mapper.MapperDef. |
| markerInterface | 可选 Set a scan path so that only Mapper interfaces implementing a specific interface are recognized as Mapper interface classes. Default: net.hasor.dbvisitor.mapper.Mapper. |
| autoMapping | 可选 Whether to automatically map all fields under the type to columns in the database. true means automatic. false means must be declared via @Column annotation. |
| camelCase | 可选 Convert table names and property names to underscored table and column names based on camel case rules. |
| useDelimited | 可选 Force adding identifier delimiters when generating table names/column names/index names. For example: set this property to solve the problem of column names being keywords. Default is false (not set). |
| caseInsensitive | 可选 Whether table/column names are case-sensitive. Default true (insensitive). |
| ignoreNonExistStatement | 可选 Whether to ignore missing mappings during the mapping of Mapper interface methods to XML. Default is false. Missing mappings will report an error. |
| dialect | 可选 Database Dialect used by default. |