Spring boot + doma2

There are already many, many data access layers, each with more or less its own characteristics.

Due to personal habits, there are the following requirements for this function:

1. Must be able to write native SQL

   I don't like the kind of library that can generate sql, especially the more complex sql, every time I have to print out the sql to check if the generated sql is correct.

   In addition, it is best not to write sql in the xml file like mybatis, which looks too messy.

 

2. For simple CRUD with single table, sql can be automatically generated

   That is to say, I can automatically generate sql without writing sql. After all, in order to simply update a table, it is also very cumbersome to write and update sql.

 

3. Must have orm function

   No one likes to manually convert the extracted data into entities. This must be able to be converted automatically, and implicit matching must be supported.

 

Based on these requirements, doma2 was finally selected.

However, doma2 does not fully meet the above requirements. For example, select cannot automatically generate sql. If necessary, you need to extend it yourself.

In addition, doma2 does not support 1:n entity mapping. The author's explanation is that it will increase the difficulty of use.

 

Doma2, there is a special place, it uses annotation processing, that is, the source program of the implementation class is automatically generated during compilation, and it seems that orm does not use the reflection mechanism, which is more efficient. The disadvantage is of course that it will affect some compilation speed, and will generate a lot of source code.

 

Like mybatis, doma2 also provides the most basic entity and dao file generation tool doma-gen.

 

doma2 can be used in any framework, of course, spring boot is no exception. In addition, someone has written a starter library, which is more convenient to integrate.

The following configuration is a gradle file that integrates doma2 basic spring boot

buildscript {
	ext {
		springBootVersion = '1.5.1.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
	baseName = 'test'
	version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
	mavenCentral()
}

configurations {
    domaGenRuntime
}

dependencies {
	compile('org.springframework.boot:spring-boot-starter-jdbc')
	compile('org.springframework.boot:spring-boot-starter-thymeleaf')
	compile('org.springframework.boot:spring-boot-starter-validation')
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('org.seasar.doma.boot:doma-spring-boot-starter:1.1.0')
	runtime('org.springframework.boot:spring-boot-devtools')
	runtime('mysql:mysql-connector-java')
	testCompile('org.springframework.boot:spring-boot-starter-test')
	
	domaGenRuntime 'org.seasar.doma:doma-gen:2.15.0'
	domaGenRuntime 'mysql:mysql-connector-java:5.1.39'
}

task gen << {
	ant.taskdef(resource: 'domagentask.properties',
		classpath: configurations.domaGenRuntime.asPath)
	ant.gen(url: 'jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=false',
		user: 'username', password: 'password',
		templatePrimaryDir: 'templates') {
		entityConfig(packageName: 'jp.co.abc.entity',entitySuffix:'Entity',useListener: false)
		daoConfig(packageName: 'jp.co.abc.dao', overwrite:true)
		sqlConfig()
	}
}

 

Since doma-gen does not consider the integration of spring, in order to allow the generated dao files to be scanned by spring boot, the annotation @ConfigAutowireable needs to be added. You can copy a default dao.ftl file in the doma library and put it in the corresponding directory. , for example, the above configuration is placed in the templates directory under the project root directory

Then add the following two lines in the corresponding place

import org.seasar.doma.boot.ConfigAutowireable;

@ConfigAutowireable

 

 Of course, if you don't want to intrusively annotate the automatically generated java classes, you can customize a configuration class to scan the generated classes in a regular way.

 

@Component
@ComponentScan(basePackages="jp.co.abc.dao", includeFilters=@Filter(type=FilterType.REGEX,pattern = "jp.co.abc.dao.*DaoImpl"))
public class DomaConfig {

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326438289&siteId=291194637