Skip to main content

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.

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 ...
}
}

Inject types

  • JdbcOperations interface, implemented by JdbcTemplate
  • LambdaOperations interface, implemented by LambdaTemplate
  • Configuration
  • Session
  • Custom Mapper interfaces
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

KeyDescription
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