Skip to main content

3.4 Solon Integration

What is Solon​

Solon is a lightweight Java application framework that provides dependency injection, configuration, web, transaction, and plugin extension capabilities.

Features​

  • Auto-configure Datasource
  • Mapper Interface Injection
  • Multi-datasource Support
  • Support Transaction Control

Configuration Method​

First, add the dependency, guide version: 6.8.2

Maven Dependency
<dependency>
<groupId>net.hasor</groupId>
<artifactId>dbvisitor-solon-plugin</artifactId>
<version>6.8.2</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> {
...
}

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.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 NameDescription
mapperLocationsOptional Path to scan for Mapper mapping files. Default is dbvisitor/mapper/*.xml.
mapperPackagesOptional Package names to scan for Mapper interface definitions. Use , to separate multiple packages.
mapperDisabledOptional 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.
markerAnnotationOptional 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.
markerInterfaceOptional 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.
autoMappingOptional 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.
camelCaseOptional Convert table names and property names to underscored table and column names based on camel case rules.
useDelimitedOptional 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).
caseInsensitiveOptional Whether table/column names are case-sensitive. Default true (insensitive).
ignoreNonExistStatementOptional Whether to ignore missing mappings during the mapping of Mapper interface methods to XML. Default is false. Missing mappings will report an error.
dialectOptional Database Dialect used by default.