3.6 Guice Integration
What is Guice
Guice is a lightweight dependency injection tool open-sourced by Google. Unlike Spring, which contains many other capabilities besides dependency injection, if you only want to use dependency injection in your project, consider using Guice.
- Guice Project URL: https://github.com/google/guice
Features
- Auto-configure Datasource
- Annotation-based Transaction Control
- Mapper Interface Injection
- Multi-datasource Support
How to Use
First, add the dependency, current version: 6.7.0
Maven Dependency
<dependency>
<groupId>net.hasor</groupId>
<artifactId>dbvisitor-guice</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
# Data source using HikariCP
dbvisitor.jdbc-ds=com.zaxxer.hikari.HikariDataSource
dbvisitor.jdbc-ds.jdbc-url=jdbc:mysql://127.0.0.1:13306/devtester?allowMultiQueries=true
dbvisitor.jdbc-ds.username=root
dbvisitor.jdbc-ds.password=123456
dbvisitor.jdbc-ds.minimum-idle=5
dbvisitor.jdbc-ds.maximum-pool-size=12
dbvisitor.jdbc-ds.max-lifetime=1200000
dbvisitor.jdbc-ds.auto-commit=true
dbvisitor.jdbc-ds.connection-timeout=20000
dbvisitor.mapper-locations=classpath:dbvisitor/mapper/*.xml
dbvisitor.mapper-packages=net.hasor.dbvisitor.test.dao
Inject Mapper
public class ServiceTest {
@Inject
private UserMapper userMapper;
...
}
public interface UserMapper extends BaseMapper<UserDTO> {
...
}
Start Guice
// Load configuration file
Properties properties = new Properties();
properties.load(ResourcesUtils.getResourceAsStream("jdbc.properties"));
// Initialize Guice and load DbVisitorModule plugin
Injector injector = Guice.createInjector(new DbVisitorModule(properties));
// Enjoy
ServiceTest service = injector.getInstance(ServiceTest.class);
...
Example
Registered Types
- JdbcOperations interface, JdbcTemplate is its implementation
- LambdaOperations interface, LambdaTemplate is its implementation
- TransactionManager interface, interface for developing Programmatic transactions
- TransactionTemplate interface, interface for developing Template transactions
- 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
Configuration Items
| Property Name | Description |
|---|---|
| dbvisitor.mapper-locations | 可选 Path to scan for Mapper mapping files. Default is dbvisitor/mapper/*.xml. |
| dbvisitor.mapper-packages | 可选 Package names to scan for Mapper interface definitions. Use , to separate multiple packages. |
| dbvisitor.mapper-disabled | 可选 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. |
| dbvisitor.marker-annotation | 可选 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. |
| dbvisitor.marker-interface | 可选 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. |
| dbvisitor.auto-mapping | 可选 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. |
| dbvisitor.camel-case | 可选 Convert table names and property names to underscored table and column names based on camel case rules. |
| dbvisitor.use-delimited | 可选 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). |
| dbvisitor.case-insensitive | 可选 Whether table/column names are case-sensitive. Default true (insensitive). |
| dbvisitor.ignore-nonexist-statement | 可选 Whether to ignore missing mappings during the mapping of Mapper interface methods to XML. Default is false. Missing mappings will report an error. |
| dbvisitor.dialect | 可选 Database Dialect used by default. |