Skip to main content

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.

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);
...

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