spring集成指南
# spring集成指南
spring使用与前一章节springboot类似,需要注意的是spring引入的依赖与springboot不同,另外需要手动配置xml bean
# 引入依赖
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-spring</artifactId>
<!-- 版本选当前支持的最新版本即可,须大于等于2.1.0 -->
<version>2.1.0</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
✨最新版本 Latest
Version: (opens new window)
# 1、方式一 按需配置xml bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 以下属性,按需配置 -->
<bean id="easyEsProperties" class="org.dromara.easyes.common.property.EasyEsProperties">
<property name="enable" value="true"/>
<property name="address" value="127.0.0.1:9200"/>
<property name="keepAliveMillis" value="18000"/>
<property name="globalConfig.IKunMode" value="true"/>
<property name="globalConfig.processIndexMode" value="MANUAL"/>
<property name="globalConfig.asyncProcessIndexBlocking" value="true"/>
<property name="globalConfig.printDsl" value="true"/>
<property name="globalConfig.dbConfig.mapUnderscoreToCamelCase" value="true"/>
<property name="globalConfig.dbConfig.idType" value="CUSTOMIZE"/>
<property name="globalConfig.dbConfig.fieldStrategy" value="NOT_EMPTY"/>
<property name="globalConfig.dbConfig.refreshPolicy" value="IMMEDIATE"/>
<property name="globalConfig.dbConfig.enableTrackTotalHits" value="true"/>
</bean>
<!-- 多数据源配置 -->
<bean id="easyEsDynamicProperties" class="org.dromara.easyes.common.property.EasyEsDynamicProperties">
</bean>
<!-- mapper配置,basePackage包路径按实际mapper位置配置 -->
<bean id="mapperScannerConfigurer" class="org.dromara.easyes.spring.MapperScannerConfigurer">
<property name="basePackage" value="org.dromara.easyes.test.mapper"/>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 2、方式二 使用easy-es-spring的扫描类进行配置
import org.dromara.easyes.common.property.EasyEsDynamicProperties;
import org.dromara.easyes.common.property.EasyEsProperties;
import org.dromara.easyes.spring.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* spring不用xml配置的平替
* @author MoJie
* @since 2.1.0
*/
@Configuration
public class EasyEsSpringConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
// mapper配置,basePackage包路径按实际mapper位置配置
mapperScannerConfigurer.setBasePackage("org.dromara.easyes.test.mapper");
return mapperScannerConfigurer;
}
@Bean
public EasyEsProperties easyEsProperties() {
EasyEsProperties easyEsProperties = new EasyEsProperties();
// 以下是easy-es的配置项
easyEsProperties.setAddress("127.0.0.1:9200");
return easyEsProperties;
}
@Bean
public EasyEsDynamicProperties easyEsDynamicProperties() {
EasyEsDynamicProperties easyEsDynamicProperties = new EasyEsDynamicProperties();
// 多数据源配置
Map<String, EasyEsProperties> datasource = new HashMap<>();
// 这里你可以配置多个数据源,在mapper中可通过@EsDS("key")注解来指定数据源
easyEsDynamicProperties.setDatasource(datasource);
return easyEsDynamicProperties;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 3、方式三 通过扫描注解配置(推荐)
import org.dromara.easyes.common.property.EasyEsDynamicProperties;
import org.dromara.easyes.common.property.EasyEsProperties;
import org.dromara.easyes.spring.annotation.EsMapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* spring不用xml配置的平替
* @author MoJie
* @since 2.1.0
*/
@Configuration
@EsMapperScan("org.dromara.easyes.test.mapper")
public class EasyEsSpringConfig {
@Bean
public EasyEsProperties easyEsProperties() {
EasyEsProperties easyEsProperties = new EasyEsProperties();
// 以下是easy-es的配置项
easyEsProperties.setAddress("127.0.0.1:9200");
return easyEsProperties;
}
@Bean
public EasyEsDynamicProperties easyEsDynamicProperties() {
EasyEsDynamicProperties easyEsDynamicProperties = new EasyEsDynamicProperties();
// 多数据源配置
Map<String, EasyEsProperties> datasource = new HashMap<>();
// 这里你可以配置多个数据源,在mapper中可通过@EsDS("key")注解来指定数据源
easyEsDynamicProperties.setDatasource(datasource);
return easyEsDynamicProperties;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 使用
通过@Autowired或@Resource在需要用到Mapper的地方直接注入Mapper即可使用,与Springboot下使用并无差异,其它方面亦然.
帮助我们改善此文档 (opens new window)
上次更新: 2025/02/05