Skip to main content

3.3 Spring Integration

What is Spring​

Spring is a widely used Java application framework that provides an IoC container, transaction management, and Spring Boot auto-configuration.

Features​

  • Extensive version support
    • SpringBoot 2/3
    • Spring 4.0.0 minimum
  • Multiple configuration methods
    • application.properties configuration file
    • Annotation-based
    • Spring XML configuration

Configuration Method​

First, add the dependency, guide version: 6.8.2

<dependency>
<groupId>net.hasor</groupId>
<artifactId>dbvisitor-spring-starter</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>
# Spring JDBC Datasource Configuration
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/devtester
spring.datasource.username=root
spring.datasource.password=123456
# Required
dbvisitor.mapper-packages=com.example.demo.dao
dbvisitor.mapper-locations=classpath:dbvisitor/mapper/*.xml
Inject Mapper
public class ServiceTest {
@Resource // Or @Autowired
private UserMapper userMapper;
...
}

public interface UserMapper extends BaseMapper<UserDTO> {
...
}

Using Transaction​

@Configuration
@EnableTransactionManagement
public class DsConfig {
@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
Transaction control via @Transactional annotation
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

public class TxExample {
@Transactional(propagation = Propagation.REQUIRED)
public void exampleMethod() {
...
}
}

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

Example Project​

Configuration Items​

application.properties Configuration​

Property NameDescription
dbvisitor.mapper-packagesOptional Package names to scan for Mapper interface definitions. Use , to separate multiple packages.
dbvisitor.mapper-locationsOptional Path to scan for Mapper mapping files. Default is classpath*:/dbvisitor/mapper/**/*.xml. Multiple paths can be separated by , ; \t \n.
dbvisitor.mapper-disabledOptional 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.mapper-name-generatorOptional Class name of the generator for simplifying mapper bean names. Needs to implement BeanNameGenerator interface. Default: empty.
dbvisitor.marker-annotationOptional 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-interfaceOptional 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.mapper-factory-beanOptional Factory class for creating Mappers. Default: net.hasor.dbvisitor.spring.support.MapperBean.
dbvisitor.ref-sessionOptional Used to customize the name of the Session Bean used by the Mapper Bean.
dbvisitor.mapper-lazy-initOptional lazyInit attribute of Mapper Bean. Default: false.
dbvisitor.mapper-scopeOptional Spring scope of Mapper Bean. Default determined by AbstractBeanDefinition.SCOPE_DEFAULT.
If set, singleton is recommended.
dbvisitor.auto-mappingOptional 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-caseOptional Convert table names and property names to underscored table and column names based on camel case rules.
dbvisitor.use-delimitedOptional 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-insensitiveOptional Whether table/column names are case-sensitive. Default true (insensitive).
dbvisitor.ignore-nonexist-statementOptional Whether to ignore missing mappings during the mapping of Mapper interface methods to XML. Default is false. Missing mappings will report an error.
dbvisitor.dialectOptional Database Dialect used by default.

@MapperScan Annotation Attributes​

Property NameDescription
value, basePackages, basePackageClassesRefer to dbvisitor.mapper-packages config
mapperLocationsRefer to dbvisitor.mapper-locations config
nameGeneratorRefer to dbvisitor.mapper-name-generator config
annotationClassRefer to dbvisitor.marker-annotation config
markerInterfaceRefer to dbvisitor.marker-interface config
sessionRefRefer to dbvisitor.ref-session config
factoryBeanRefer to dbvisitor.mapper-factory-bean config
lazyInitRefer to dbvisitor.mapper-lazy-init config
defaultScopeRefer to dbvisitor.mapper-scope config
mapperDisabledRefer to dbvisitor.mapper-disabled config
tip

Annotation configuration has higher priority than configuration file (application.properties is recommended for simpler configuration).

ConfigurationBean Attributes​

Property NameDescription
mapperResourcesRefer to dbvisitor.mapper-locations config
mapperInterfacesRefer to dbvisitor.marker-interface config. ConfigurationBean will load resource references declared on Mapper.
typeRegistryUsed to customize Type Handler registry.
javaTypeHandlerMapUsed to register custom TypeHandler as a handler for a Java type.
jdbcTypeHandlerMapUsed to register custom TypeHandler as a handler for a JDBC type.
ruleRegistryUsed to customize Rule registry.
ruleHandlerMapUsed to register custom Rule.
autoMappingRefer to dbvisitor.auto-mapping config
camelCaseRefer to dbvisitor.camel-case config
caseInsensitiveRefer to dbvisitor.case-insensitive config
useDelimitedRefer to dbvisitor.use-delimited config
dialectNameRefer to dbvisitor.dialect config
ignoreNonExistStatementRefer to dbvisitor.ignore-nonexist-statement config

SessionBean Attributes​

Property NameDescription
configurationConfiguration used by the Session created by SessionBean.
dataSourceDataSource used.
dsAdapterAbstractDsAdapter type object. This class determines how to get Connection from dataSource.
dsAdapterClassdsAdapter type name (Class type). Equivalent to dsAdapterName attribute.
dsAdapterNamedsAdapter type name (String type). Equivalent to dsAdapterClass attribute.

MapperBean Attributes​

Property NameDescription
sessionSession used by MapperBean to create Mapper object.
mapperInterfaceSpecific Mapper created by MapperBean.

MapperScannerConfigurer Attributes​

Property NameDescription
basePackageRefer to dbvisitor.mapper-packages config
nameGeneratorNameRefer to dbvisitor.mapper-name-generator config (Type Name)
nameGeneratorRefer to dbvisitor.mapper-name-generator config (Object)
annotationClassNameRefer to dbvisitor.marker-annotation config (Type Name)
annotationClassRefer to dbvisitor.marker-annotation config (Type)
markerInterfaceNameRefer to dbvisitor.marker-interface config (Type Name)
markerInterfaceRefer to dbvisitor.marker-interface config (Type)
sessionRefer to dbvisitor.ref-session config (Object)
sessionRefRefer to dbvisitor.ref-session config (Bean Name)
mapperFactoryBeanClassNameRefer to dbvisitor.mapper-factory-bean config (Type Name)
mapperFactoryBeanClassRefer to dbvisitor.mapper-factory-bean config (Type)
mapperDisabledRefer to dbvisitor.mapper-disabled config
lazyInitRefer to dbvisitor.mapper-lazy-init config
defaultScopeRefer to dbvisitor.mapper-scope config
dependsOnPre-requisite Beans that the Mapper depends on during creation.