├── .gitignore ├── README.MD ├── elastic-core ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── leno │ │ │ ├── ElasticCoreApplication.java │ │ │ ├── common │ │ │ ├── BeanConfig.java │ │ │ ├── ElasticControllerAdvice.java │ │ │ ├── RestCodeEnum.java │ │ │ ├── RestResult.java │ │ │ └── SpringContextUtil.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── model │ │ │ └── request │ │ │ │ └── UserRequest.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application.yml │ │ └── elastic-logback.xml │ └── test │ └── java │ └── com │ └── leno │ └── ElasticsCoreApplicationTests.java ├── elastic-dal ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── leno │ │ └── elastic │ │ ├── dal │ │ ├── common │ │ │ ├── BaseCriteria.java │ │ │ ├── BaseQuery.java │ │ │ ├── Criterion.java │ │ │ └── PageResult.java │ │ ├── mapper │ │ │ ├── UserMapper.java │ │ │ └── ext │ │ │ │ └── UserExtMapper.java │ │ ├── model │ │ │ └── UserDO.java │ │ └── query │ │ │ └── UserQuery.java │ │ └── manager │ │ ├── UserManager.java │ │ └── impl │ │ └── UserManagerImpl.java │ └── resources │ └── mapper │ ├── UserMapper.xml │ └── ext │ └── UserExtMapper.xml ├── elastic-generator ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── leno │ │ └── mybatisgen │ │ ├── ColumnInfo.java │ │ ├── Main.java │ │ ├── MyBatisGenConst.java │ │ ├── MyBatisGenCore.java │ │ └── enums │ │ └── ColumnTypeEnum.java │ └── resources │ ├── db.config │ ├── generate_tables.config │ └── template │ ├── do.txt │ ├── manager.txt │ ├── managerImpl.txt │ ├── mapper-ext.txt │ ├── mapper.txt │ ├── query.txt │ ├── sqlmap-ext.txt │ └── sqlmap.txt ├── elastic-search ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── leno │ │ └── search │ │ ├── common │ │ ├── ESClientDecorator.java │ │ ├── EsSearchClient.java │ │ ├── IESEntity.java │ │ ├── LuSearchClient.java │ │ ├── SearchClient.java │ │ └── SimpleESEntity.java │ │ ├── config │ │ ├── ElasticsConfig.java │ │ └── ElasticsProperties.java │ │ ├── entity │ │ ├── BaseEntity.java │ │ ├── UserESEntity.java │ │ ├── enums │ │ │ └── ESQueryTypeEnum.java │ │ └── request │ │ │ ├── CommonSearchRequest.java │ │ │ └── SearchWord.java │ │ └── repository │ │ ├── UserSearchRepository.java │ │ └── impl │ │ └── UserSearchRepositoryImpl.java │ └── resources │ └── conf │ ├── jdbc.conf │ └── user.sql ├── moyu_index_sync.sh ├── pom.xml └── start.sh /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # elasticsearch 6.x DEMO 2 | ## 简述 3 | ### SpringBoot+elasticsearch6.x搭建的es全文搜索服务demo,主要技术: 4 | - 1、SpringBoot+Mybatis+Elasticsearch6.x框架 5 | - 2、使用RestHighLevelClient作为搜索java api 6 | - 3、使用LogStash同步mysql数据 7 | 8 | ## 环境安装 9 | 10 | * 创建需用同步数据的表 示例: 11 | 12 | ```CREATE TABLE user 13 | ( 14 | user_id BIGINT AUTO_INCREMENT 15 | COMMENT '主键' 16 | PRIMARY KEY, 17 | user_name VARCHAR(30) DEFAULT '' NOT NULL 18 | COMMENT '用户名', 19 | sex INT DEFAULT '0' NOT NULL 20 | COMMENT '性别', 21 | login_name VARCHAR(20) DEFAULT '' NOT NULL 22 | COMMENT '登录名', 23 | login_pwd VARCHAR(30) DEFAULT '' NOT NULL 24 | COMMENT '密码', 25 | status INT DEFAULT '0' NOT NULL 26 | COMMENT '状态', 27 | gmt_create TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, 28 | gmt_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL 29 | ); 30 | 31 | * elasticsearch安装
32 | ` brew install elasticsearch 33 | 安装完成之后检查一下elasticsearch状态,使用curl命令 34 | curl localhost:9200 35 | ` 36 | * 中文分词器安装
37 | ` elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.x/elasticsearch-analysis-ik-6.x.zip ` 38 |
"6.x表示对应的elasticsearch版本" 39 | 40 | * 创建索引和映射 41 | ```{ 42 | "mappings":{ 43 | "doc":{ 44 | "properties":{ 45 | "@timestamp":{ 46 | "type":"date" 47 | }, 48 | "@version":{ 49 | "type":"text", 50 | "fields":{ 51 | "keyword":{ 52 | "type":"keyword", 53 | "ignore_above":256 54 | } 55 | } 56 | }, 57 | "gmt_create":{ 58 | "type":"date" 59 | }, 60 | "gmt_modified":{ 61 | "type":"date" 62 | }, 63 | "login_name":{ 64 | "type":"text", 65 | "fields":{ 66 | "keyword":{ 67 | "type":"keyword", 68 | "ignore_above":256 69 | } 70 | }, 71 | "analyzer":"ik_max_word" 72 | }, 73 | "login_pwd":{ 74 | "type":"text", 75 | "fields":{ 76 | "keyword":{ 77 | "type":"keyword", 78 | "ignore_above":256 79 | } 80 | } 81 | }, 82 | "sex":{ 83 | "type":"long" 84 | }, 85 | "status":{ 86 | "type":"long" 87 | }, 88 | "user_id":{ 89 | "type":"long" 90 | }, 91 | "user_name":{ 92 | "type":"text", 93 | "fields":{ 94 | "keyword":{ 95 | "type":"keyword", 96 | "ignore_above":256 97 | } 98 | }, 99 | "analyzer":"ik_max_word" 100 | } 101 | } 102 | } 103 | } 104 | } 105 | 106 | * 安装LogStash
107 | ` brew install LogStash` 108 | 109 | ## 使用方法 110 | 111 | * 同步数据脚本 112 | `sh moyu_index_sync.sh` 113 | 114 | 115 | -------------------------------------------------------------------------------- /elastic-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | elastic-core 7 | jar 8 | 9 | elastic-core 10 | 11 | 12 | com.leno 13 | elastic-demo 14 | 1.0-SNAPSHOT 15 | 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-aop 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-test 32 | test 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | com.leno 42 | elastic-dal 43 | 44 | 45 | 46 | com.leno 47 | elastic-search 48 | 49 | 50 | 51 | org.projectlombok 52 | lombok 53 | 54 | 55 | 56 | com.alibaba 57 | fastjson 58 | 59 | 60 | 61 | mysql 62 | mysql-connector-java 63 | 64 | 65 | 66 | 67 | elastics 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-maven-plugin 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/ElasticCoreApplication.java: -------------------------------------------------------------------------------- 1 | package com.leno; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ElasticCoreApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ElasticCoreApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/common/BeanConfig.java: -------------------------------------------------------------------------------- 1 | package com.leno.common; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | *

TODO

8 | * 9 | * @author: XianGuo 10 | * @date: 2018年02月07日 11 | */ 12 | @Configuration 13 | public class BeanConfig { 14 | 15 | @Bean 16 | public SpringContextUtil getContext() { 17 | return new SpringContextUtil(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/common/ElasticControllerAdvice.java: -------------------------------------------------------------------------------- 1 | package com.leno.common; 2 | 3 | import org.springframework.util.CollectionUtils; 4 | import org.springframework.validation.BindingResult; 5 | import org.springframework.validation.ObjectError; 6 | import org.springframework.web.bind.MethodArgumentNotValidException; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | *

controller异常统一处理

15 | * 16 | * @author: XianGuo 17 | * @date: 2018年02月10日 18 | */ 19 | @ControllerAdvice 20 | public class ElasticControllerAdvice { 21 | 22 | @ExceptionHandler(MethodArgumentNotValidException.class) 23 | @ResponseBody 24 | public Object handlerValidException(MethodArgumentNotValidException e) { 25 | RestResult result = RestResult.getFailResult(RestCodeEnum.PARAMS_ERROR); 26 | BindingResult bindingResult = e.getBindingResult(); 27 | if (bindingResult == null || CollectionUtils.isEmpty(bindingResult.getAllErrors())) { 28 | return result; 29 | } 30 | 31 | List errors = bindingResult.getAllErrors(); 32 | result.setDesc(errors.get(0).getDefaultMessage()); 33 | return result; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/common/RestCodeEnum.java: -------------------------------------------------------------------------------- 1 | package com.leno.common; 2 | 3 | /** 4 | *

rest 状态枚举类

5 | * 6 | * @author: XianGuo 7 | * @date: 2018年02月10日 8 | */ 9 | public enum RestCodeEnum { 10 | SUCCESS(200, "请求成功"), NOT_FOUND(500, "服务器错误"), PARAMS_ERROR(400, "请求参数错误"); 11 | 12 | private Integer code; 13 | private String desc; 14 | 15 | RestCodeEnum(Integer code, String desc) { 16 | this.code = code; 17 | this.desc = desc; 18 | } 19 | 20 | public Integer getCode() { 21 | return code; 22 | } 23 | 24 | public String getDesc() { 25 | return desc; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/common/RestResult.java: -------------------------------------------------------------------------------- 1 | package com.leno.common; 2 | 3 | import com.fasterxml.jackson.annotation.JsonInclude; 4 | import lombok.Data; 5 | 6 | import static com.leno.common.RestCodeEnum.SUCCESS; 7 | 8 | /** 9 | *

rest api 结果

10 | * 11 | * @author: XianGuo 12 | * @date: 2018年02月10日 13 | */ 14 | @Data 15 | public class RestResult { 16 | 17 | private int code; 18 | 19 | private String desc; 20 | 21 | @JsonInclude(JsonInclude.Include.NON_NULL) 22 | private T data; 23 | 24 | public void setRestCode(int code, String desc) { 25 | this.code = code; 26 | this.desc = desc; 27 | } 28 | 29 | public void setRestCodeEnum(RestCodeEnum restCodeEnum) { 30 | this.setRestCode(restCodeEnum.getCode(), restCodeEnum.getDesc()); 31 | } 32 | 33 | public static RestResult getSuccessResult(T data) { 34 | RestResult restResult = new RestResult<>(); 35 | restResult.setRestCodeEnum(SUCCESS); 36 | restResult.setData(data); 37 | return restResult; 38 | } 39 | 40 | public static RestResult getFailResult(int code, String desc) { 41 | RestResult restResult = new RestResult(); 42 | restResult.setRestCode(code, desc); 43 | return restResult; 44 | } 45 | 46 | public static RestResult getFailResult(RestCodeEnum restCodeEnum) { 47 | return getFailResult(restCodeEnum.getCode(), restCodeEnum.getDesc()); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/common/SpringContextUtil.java: -------------------------------------------------------------------------------- 1 | package com.leno.common; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextAware; 6 | 7 | /** 8 | *

TODO

9 | * 10 | * @author: XianGuo 11 | * @date: 2018年02月07日 12 | */ 13 | public class SpringContextUtil implements ApplicationContextAware { 14 | 15 | private static ApplicationContext act; 16 | 17 | @Override 18 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 19 | act = applicationContext; 20 | } 21 | 22 | public static T getBean(Class tClass) { 23 | return act.getBean(tClass); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.leno.controller; 2 | 3 | import com.leno.model.request.UserRequest; 4 | import com.leno.service.UserService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.validation.annotation.Validated; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | /** 11 | *

TODO

12 | * 13 | * @author: XianGuo 14 | * @date: 2018年01月22日 15 | */ 16 | @RestController 17 | @RequestMapping("/user") 18 | @Slf4j 19 | public class UserController { 20 | 21 | @Autowired 22 | private UserService userService; 23 | 24 | @PostMapping("/getUserList.json") 25 | public Object getUserList(@RequestBody @Validated UserRequest userRequest) { 26 | return userService.getUserList(userRequest); 27 | } 28 | 29 | @PostMapping("/searchMatch.json") 30 | public Object searchMatch(@RequestParam String userName) { 31 | return userService.searchMatchByName(userName); 32 | } 33 | 34 | @PostMapping("/searchTerm.json") 35 | public Object searchTerm(@RequestParam String userName) { 36 | return userService.searchTermByName(userName); 37 | } 38 | 39 | @PostMapping("/searchMatchPhrase.json") 40 | public Object searchMatchPhrase(@RequestParam String userName) { 41 | return userService.searchMatchPhraseByName(userName); 42 | } 43 | 44 | @GetMapping("/test") 45 | public Object log() { 46 | new Thread(() -> { 47 | for (int i = 0; i < 100000; i++) { 48 | log.info("info测试日志{}s", System.currentTimeMillis()); 49 | log.warn("warn测试日志{}s", System.currentTimeMillis()); 50 | log.error("error测试日志{}s", System.currentTimeMillis()); 51 | } 52 | }).start(); 53 | return "测试日志"; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/model/request/UserRequest.java: -------------------------------------------------------------------------------- 1 | package com.leno.model.request; 2 | 3 | import lombok.Data; 4 | import org.hibernate.validator.constraints.NotBlank; 5 | 6 | /** 7 | *

用户请求参数

8 | * 9 | * @author: XianGuo 10 | * @date: 2018年02月10日 11 | */ 12 | @Data 13 | public class UserRequest { 14 | 15 | @NotBlank(message = "用户名不能为空") 16 | private String userName; 17 | 18 | @NotBlank(message = "登录名不能为空") 19 | private String loginName; 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.leno.service; 2 | 3 | import com.leno.common.RestResult; 4 | import com.leno.elastic.dal.model.UserDO; 5 | import com.leno.model.request.UserRequest; 6 | import com.leno.search.entity.UserESEntity; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | *

用户相关服务

12 | * 13 | * @author: XianGuo 14 | * @date: 2018年02月10日 15 | */ 16 | public interface UserService { 17 | 18 | /** 19 | * 根据请求参数获取用户利比饿哦 20 | */ 21 | RestResult> getUserList(UserRequest request); 22 | 23 | /** 24 | * 全文搜索 25 | */ 26 | RestResult> searchMatchByName(String userName); 27 | 28 | 29 | /** 30 | * 精确搜索 31 | */ 32 | RestResult> searchTermByName(String userName); 33 | 34 | 35 | /** 36 | * 紧邻搜索 37 | */ 38 | RestResult> searchMatchPhraseByName(String userName); 39 | } 40 | -------------------------------------------------------------------------------- /elastic-core/src/main/java/com/leno/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.leno.service.impl; 2 | 3 | import com.leno.common.RestResult; 4 | import com.leno.elastic.dal.model.UserDO; 5 | import com.leno.elastic.dal.query.UserQuery; 6 | import com.leno.elastic.manager.UserManager; 7 | import com.leno.model.request.UserRequest; 8 | import com.leno.search.common.SearchClient; 9 | import com.leno.search.entity.UserESEntity; 10 | import com.leno.service.UserService; 11 | import org.elasticsearch.action.search.SearchRequest; 12 | import org.elasticsearch.index.query.QueryBuilders; 13 | import org.elasticsearch.search.builder.SearchSourceBuilder; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.stereotype.Service; 16 | 17 | import java.util.List; 18 | 19 | /** 20 | *

用户相关接口实现

21 | * 22 | * @author: XianGuo 23 | * @date: 2018年02月10日 24 | */ 25 | @Service 26 | public class UserServiceImpl implements UserService { 27 | 28 | @Autowired 29 | private SearchClient searchClient; 30 | 31 | @Autowired 32 | private UserManager userManager; 33 | 34 | @Override 35 | public RestResult> getUserList(UserRequest request) { 36 | UserQuery userQuery = new UserQuery(); 37 | userQuery.createCriteria().andStatusEqualTo(0) 38 | .andUserNameEqualTo(request.getUserName()) 39 | .andLoginNameEqualTo(request.getLoginName()); 40 | return RestResult.getSuccessResult(userManager.selectByQuery(userQuery)); 41 | } 42 | 43 | @Override 44 | public RestResult> searchMatchByName(String userName) { 45 | SearchRequest searchRequest = new SearchRequest(); 46 | searchRequest.indices("moyu_index2"); 47 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 48 | searchSourceBuilder.query(QueryBuilders.matchQuery("user_name", userName)); 49 | searchRequest.source(searchSourceBuilder); 50 | return RestResult.getSuccessResult(searchClient.search(searchRequest, UserESEntity.class)); 51 | } 52 | 53 | @Override 54 | public RestResult> searchTermByName(String userName) { 55 | SearchRequest searchRequest = new SearchRequest(); 56 | searchRequest.indices("moyu_index2"); 57 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 58 | searchSourceBuilder.query(QueryBuilders.termQuery("user_name", userName)); 59 | searchRequest.source(searchSourceBuilder); 60 | return RestResult.getSuccessResult(searchClient.search(searchRequest, UserESEntity.class)); 61 | } 62 | 63 | @Override 64 | public RestResult> searchMatchPhraseByName(String userName) { 65 | SearchRequest searchRequest = new SearchRequest(); 66 | searchRequest.indices("moyu_index2"); 67 | SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 68 | searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("user_name", userName)); 69 | searchRequest.source(searchSourceBuilder); 70 | return RestResult.getSuccessResult(searchClient.search(searchRequest, UserESEntity.class)); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /elastic-core/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8020 3 | spring: 4 | data: 5 | elasticsearch: 6 | cluster-name: elasticsearch_xianguo 7 | cluster-nodes: localhost 8 | datasource: 9 | driverClassName: com.mysql.jdbc.Driver 10 | username: root 11 | password: alixian 12 | url: jdbc:mysql://localhost:3306/moyu_db?useUnicode=true&characterEncoding=utf-8&useSSL=true 13 | type: org.apache.tomcat.jdbc.pool.DataSource 14 | profiles: 15 | active: local 16 | application: 17 | name: elastic 18 | mybatis: 19 | mapper-locations: classpath*:/mapper/**/*Mapper.xml 20 | type-aliases-package: com.leno.elastic.dal.model 21 | logging: 22 | config: classpath:elastic-logback.xml 23 | -------------------------------------------------------------------------------- /elastic-core/src/main/resources/elastic-logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | v 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | %date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) | 34 | %msg | [%file:%line]%n 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | ${FILE_PATH}/info.log 43 | 44 | 45 | ${FILE_PATH}/info.%d{yyyy-MM-dd}.%i.log 46 | 47 | 100000 48 | 49 | 50 | 10MB 51 | 52 | 53 | 54 | 55 | ${PATTERN} 56 | UTF-8 57 | 58 | 59 | 60 | 61 | 62 | ${ERROR_FILE_PATH}/error.log 63 | 64 | 65 | ${ERROR_FILE_PATH}/error.%d{yyyy-MM-dd}.%i.log 66 | 67 | 100000 68 | 69 | 70 | 10MB 71 | 72 | 73 | 74 | 75 | ${PATTERN} 76 | UTF-8 77 | 78 | 79 | ERROR 80 | ACCEPT 81 | DENY 82 | 83 | 84 | 85 | 86 | 87 | ${WARN_FILE_PATH}/warn.log 88 | 89 | 90 | ${WARN_FILE_PATH}/warn.%d{yyyy-MM-dd}.%i.log 91 | 92 | 100000 93 | 94 | 95 | 10MB 96 | 97 | 98 | 99 | 100 | ${PATTERN} 101 | UTF-8 102 | 103 | 104 | WARN 105 | ACCEPT 106 | DENY 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /elastic-core/src/test/java/com/leno/ElasticsCoreApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.leno; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class ElasticsCoreApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /elastic-dal/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | elastic-dal 7 | jar 8 | 9 | 10 | com.leno 11 | elastic-demo 12 | 1.0-SNAPSHOT 13 | 14 | 15 | 16 | 17 | 18 | org.projectlombok 19 | lombok 20 | 21 | 22 | 23 | org.mybatis.spring.boot 24 | mybatis-spring-boot-starter 25 | 26 | 27 | 28 | commons-lang 29 | commons-lang 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/dal/common/BaseCriteria.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.dal.common; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * Created by XianGuo 9 | * Date: 2017-07-04 20:24 10 | */ 11 | public class BaseCriteria implements Serializable { 12 | private static final long serialVersionUID = 1L; 13 | protected List criteria; 14 | 15 | protected BaseCriteria() { 16 | super(); 17 | criteria = new ArrayList(); 18 | } 19 | 20 | public boolean isValid() { 21 | return criteria.size() > 0; 22 | } 23 | 24 | public List getAllCriteria() { 25 | return criteria; 26 | } 27 | 28 | public List getCriteria() { 29 | return criteria; 30 | } 31 | 32 | protected void addCriterion(String condition) { 33 | if (condition == null) { 34 | throw new RuntimeException("Value for condition cannot be null"); 35 | } 36 | criteria.add(new Criterion(condition)); 37 | } 38 | 39 | protected void addCriterion(String condition, Object value, String property) { 40 | if (value == null) { 41 | throw new RuntimeException("Value for " + property + " cannot be null"); 42 | } 43 | criteria.add(new Criterion(condition, value)); 44 | } 45 | 46 | protected void addCriterion(String condition, Object value1, Object value2, String property) { 47 | if (value1 == null || value2 == null) { 48 | throw new RuntimeException("Between values for " + property + " cannot be null"); 49 | } 50 | criteria.add(new Criterion(condition, value1, value2)); 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/dal/common/BaseQuery.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.dal.common; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * Created by XianGuo 9 | * Date: 2017-07-04 20:28 10 | */ 11 | public class BaseQuery implements Serializable { 12 | private static final long serialVersionUID = 1L; 13 | 14 | public static int DEFAULT_PAGE_SIZE = 20; // 默认页大小 15 | public static int MAX_PAGE_SIZE = 100; // 最大页大小 16 | public static int DEFAULT_PAGE = 1; // 默认分页 17 | /** 18 | * order by clause. 19 | */ 20 | protected String orderByClause; 21 | 22 | /** 23 | * distinct 24 | */ 25 | protected boolean distinct; 26 | 27 | /** 28 | * criteria list 29 | */ 30 | protected List oredCriteria; 31 | 32 | /** 33 | * page 34 | */ 35 | protected Integer pageOffset; 36 | 37 | 38 | /** 39 | * pageNo 40 | */ 41 | protected Integer pageNo; 42 | 43 | /** 44 | * page size 45 | */ 46 | protected Integer pageSize ;//= DEFAULT_PAGE_SIZE ; 47 | 48 | public BaseQuery() { 49 | oredCriteria = new ArrayList(); 50 | } 51 | 52 | public void setOrderByClause(String orderByClause) { 53 | this.orderByClause = orderByClause; 54 | } 55 | 56 | public String getOrderByClause() { 57 | return orderByClause; 58 | } 59 | 60 | public void setDistinct(boolean distinct) { 61 | this.distinct = distinct; 62 | } 63 | 64 | public boolean isDistinct() { 65 | return distinct; 66 | } 67 | 68 | public List getOredCriteria() { 69 | return oredCriteria; 70 | } 71 | 72 | public void or(BaseCriteria criteria) { 73 | oredCriteria.add(criteria); 74 | } 75 | 76 | 77 | public void clear() { 78 | oredCriteria.clear(); 79 | orderByClause = null; 80 | distinct = false; 81 | } 82 | 83 | public void clearPage(){ 84 | this.pageSize = null; 85 | this.pageNo = null; 86 | this.pageOffset = null; 87 | } 88 | 89 | protected void calPageOffset(){ 90 | if(null == pageSize || null == pageNo){ 91 | pageOffset = null; 92 | }else{ 93 | pageOffset = (pageNo -1)*pageSize; 94 | } 95 | } 96 | 97 | public boolean isValid() { 98 | for (int i = 0; i < oredCriteria.size(); i++) { 99 | if (oredCriteria.get(i).isValid()) { 100 | return true; 101 | } 102 | } 103 | return false; 104 | } 105 | 106 | public void setPageOffset(Integer pageOffset) { 107 | this.pageOffset = pageOffset; 108 | } 109 | 110 | public Integer getPageOffset() { 111 | return pageOffset; 112 | } 113 | 114 | public void setPageSize(Integer pageSize) { 115 | if(pageSize == null || pageSize <= 0){ 116 | this.clearPage(); 117 | }else if(pageSize > MAX_PAGE_SIZE){ 118 | this.pageSize = MAX_PAGE_SIZE; 119 | }else { 120 | this.pageSize = pageSize; 121 | } 122 | this.calPageOffset(); 123 | } 124 | 125 | public Integer getPageSize() { 126 | return pageSize; 127 | } 128 | 129 | 130 | public Integer getPageNo() { 131 | return pageNo; 132 | } 133 | 134 | public void setPageNo(Integer pageNo) { 135 | if(pageNo == null || pageNo ) { 73 | this.listValue = true; 74 | } else { 75 | this.singleValue = true; 76 | } 77 | } 78 | 79 | public Criterion(String condition, Object value) { 80 | this(condition, value, null); 81 | } 82 | 83 | public Criterion(String condition, Object value, Object secondValue, String typeHandler) { 84 | super(); 85 | this.condition = condition; 86 | this.value = value; 87 | this.secondValue = secondValue; 88 | this.typeHandler = typeHandler; 89 | this.betweenValue = true; 90 | } 91 | 92 | public Criterion(String condition, Object value, Object secondValue) { 93 | this(condition, value, secondValue, null); 94 | } 95 | 96 | 97 | @Override 98 | public String toString() { 99 | if (isNoValue()) { 100 | return condition; 101 | } else if (isSingleValue()) { 102 | return condition + " " + this.getValue(); 103 | } else if (isBetweenValue()) { 104 | return condition + " " + this.value + " and " + this.secondValue; 105 | } else if (isListValue()) { 106 | return condition + " in " + this.value; 107 | } else { 108 | return "invalid status"; 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/dal/common/PageResult.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.dal.common; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by XianGuo 8 | * Date: 2017-07-04 20:27 9 | */ 10 | public class PageResult implements Serializable { 11 | private static final long serialVersionUID = 1L; 12 | 13 | private List result; 14 | 15 | private int pageNo = BaseQuery.DEFAULT_PAGE; 16 | 17 | private int totalCount; // 总的数目 18 | 19 | private int pageSize = BaseQuery.DEFAULT_PAGE_SIZE; //每一页数目 20 | 21 | public List getResult() { 22 | return result; 23 | } 24 | 25 | public void setResult(List result) { 26 | this.result = result; 27 | } 28 | 29 | public int getPageNo() { 30 | return pageNo; 31 | } 32 | 33 | public void setPageNo(int pageNo) { 34 | this.pageNo = pageNo; 35 | } 36 | 37 | public int getTotalPage() { 38 | if(pageNo < 1){ 39 | pageNo = BaseQuery.DEFAULT_PAGE; 40 | } 41 | return totalCount / pageSize + (totalCount % pageSize == 0 ? 0 : 1); 42 | } 43 | 44 | 45 | public int getTotalCount() { 46 | return totalCount; 47 | } 48 | 49 | public void setTotalCount(int totalCount) { 50 | this.totalCount = totalCount; 51 | } 52 | 53 | public int getPageSize() { 54 | return pageSize; 55 | } 56 | 57 | 58 | public void setPageSize(int pageSize) { 59 | this.pageSize = pageSize; 60 | } 61 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/dal/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.dal.mapper; 2 | 3 | import com.leno.elastic.dal.model.UserDO; 4 | import com.leno.elastic.dal.query.UserQuery; 5 | 6 | import java.util.List; 7 | 8 | import org.apache.ibatis.annotations.Mapper; 9 | import org.apache.ibatis.annotations.Param; 10 | import org.springframework.stereotype.Repository; 11 | /** 12 | * MyBatis Mapper for User. 13 | */ 14 | @Mapper 15 | @Repository 16 | public interface UserMapper { 17 | /** 18 | * query count by query condition. 19 | */ 20 | int countByQuery(UserQuery query); 21 | 22 | /** 23 | * delete by query condition. 24 | */ 25 | int deleteByQuery(UserQuery query); 26 | 27 | /** 28 | * delete by primary key. 29 | */ 30 | int deleteByPrimaryKey(UserDO record); 31 | 32 | /** 33 | * insert selective. 34 | */ 35 | int insertSelective(UserDO record); 36 | 37 | /** 38 | * select by query condition. 39 | */ 40 | List selectByQuery(UserQuery query); 41 | 42 | /** 43 | * select by primary key. 44 | */ 45 | UserDO selectByPrimaryKey(Long id); 46 | 47 | /** 48 | * update by query condition selective. 49 | */ 50 | int updateByQuerySelective(@Param("record") UserDO record, @Param("query") UserQuery query); 51 | 52 | /** 53 | * update by query condition. 54 | */ 55 | int updateByQuery(@Param("record") UserDO record, @Param("query") UserQuery query); 56 | 57 | /** 58 | * update by primary key selective. 59 | */ 60 | int updateByPrimaryKeySelective(UserDO record); 61 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/dal/mapper/ext/UserExtMapper.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.dal.mapper.ext; 2 | 3 | import com.leno.elastic.dal.mapper.UserMapper; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.springframework.stereotype.Repository; 6 | 7 | /** 8 | * MyBatis Ext Mapper for User. 9 | */ 10 | 11 | @Mapper 12 | @Repository 13 | public interface UserExtMapper extends UserMapper { 14 | 15 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/dal/model/UserDO.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.dal.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import lombok.Data; 6 | 7 | @Data 8 | public class UserDO implements Serializable { 9 | 10 | private static final long serialVersionUID = 1L; 11 | 12 | /** 13 | * 主键 14 | * user.user_id 15 | */ 16 | private Long userId; 17 | 18 | /** 19 | * 用户名 20 | * user.user_name 21 | */ 22 | private String userName; 23 | 24 | /** 25 | * 登录名 26 | * user.login_name 27 | */ 28 | private String loginName; 29 | 30 | /** 31 | * 密码 32 | * user.login_pwd 33 | */ 34 | private String loginPwd; 35 | 36 | /** 37 | * 状态 38 | * user.status 39 | */ 40 | private Integer status; 41 | 42 | /** 43 | * 44 | * user.gmt_create 45 | */ 46 | private Date gmtCreate; 47 | 48 | /** 49 | * 50 | * user.gmt_modified 51 | */ 52 | private Date gmtModified; 53 | 54 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/dal/query/UserQuery.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.dal.query; 2 | 3 | 4 | import com.leno.elastic.dal.common.BaseCriteria; 5 | import com.leno.elastic.dal.common.BaseQuery; 6 | import org.apache.commons.lang.builder.ReflectionToStringBuilder; 7 | import org.apache.commons.lang.builder.ToStringStyle; 8 | 9 | import java.io.Serializable; 10 | 11 | import java.util.Date; 12 | import java.util.List; 13 | 14 | public class UserQuery extends BaseQuery implements Serializable { 15 | private static final long serialVersionUID = 1L; 16 | 17 | public UserQuery() { 18 | super(); 19 | } 20 | 21 | public Criteria or() { 22 | Criteria criteria = createCriteriaInternal(); 23 | super.oredCriteria.add(criteria); 24 | return criteria; 25 | } 26 | 27 | public Criteria createCriteria() { 28 | Criteria criteria = createCriteriaInternal(); 29 | if (oredCriteria.size() == 0) { 30 | oredCriteria.add(criteria); 31 | } 32 | return criteria; 33 | } 34 | 35 | protected Criteria createCriteriaInternal() { 36 | Criteria criteria = new Criteria(); 37 | return criteria; 38 | } 39 | 40 | /** 41 | * This class corresponds to the database table user 42 | */ 43 | protected abstract static class GeneratedCriteria extends BaseCriteria { 44 | 45 | protected GeneratedCriteria() { 46 | super(); 47 | } 48 | 49 | public Criteria andUserIdIsNull() { 50 | addCriterion("user_id is null"); 51 | return (Criteria) this; 52 | } 53 | 54 | public Criteria andUserIdIsNotNull() { 55 | addCriterion("user_id is not null"); 56 | return (Criteria) this; 57 | } 58 | 59 | public Criteria andUserIdEqualTo(Long value) { 60 | addCriterion("user_id =", value, "id"); 61 | return (Criteria) this; 62 | } 63 | 64 | public Criteria andUserIdNotEqualTo(Long value) { 65 | addCriterion("user_id <>", value, "id"); 66 | return (Criteria) this; 67 | } 68 | 69 | public Criteria anUserIdGreaterThan(Long value) { 70 | addCriterion("user_id >", value, "id"); 71 | return (Criteria) this; 72 | } 73 | 74 | public Criteria andUserIdGreaterThanOrEqualTo(Long value) { 75 | addCriterion("user_id >=", value, "id"); 76 | return (Criteria) this; 77 | } 78 | 79 | public Criteria andUserIdLessThan(Long value) { 80 | addCriterion("user_id <", value, "id"); 81 | return (Criteria) this; 82 | } 83 | 84 | public Criteria andUserIdLessThanOrEqualTo(Long value) { 85 | addCriterion("user_id <=", value, "id"); 86 | return (Criteria) this; 87 | } 88 | 89 | public Criteria andUserIdIn(List values) { 90 | addCriterion("user_id in", values, "id"); 91 | return (Criteria) this; 92 | } 93 | 94 | public Criteria andUserIdNotIn(List values) { 95 | addCriterion("user_id not in", values, "id"); 96 | return (Criteria) this; 97 | } 98 | 99 | public Criteria andUserIdBetween(Long value1, Long value2) { 100 | addCriterion("user_id between", value1, value2, "id"); 101 | return (Criteria) this; 102 | } 103 | 104 | public Criteria andUserIdNotBetween(Long value1, Long value2) { 105 | addCriterion("user_id not between", value1, value2, "id"); 106 | return (Criteria) this; 107 | } 108 | 109 | public Criteria andGmtCreateIsNull() { 110 | addCriterion("gmt_create is null"); 111 | return (Criteria) this; 112 | } 113 | 114 | public Criteria andGmtCreateIsNotNull() { 115 | addCriterion("gmt_create is not null"); 116 | return (Criteria) this; 117 | } 118 | 119 | public Criteria andGmtCreateEqualTo(Date value) { 120 | addCriterion("gmt_create =", value, "gmtCreate"); 121 | return (Criteria) this; 122 | } 123 | 124 | public Criteria andGmtCreateNotEqualTo(Date value) { 125 | addCriterion("gmt_create <>", value, "gmtCreate"); 126 | return (Criteria) this; 127 | } 128 | 129 | public Criteria andGmtCreateGreaterThan(Date value) { 130 | addCriterion("gmt_create >", value, "gmtCreate"); 131 | return (Criteria) this; 132 | } 133 | 134 | public Criteria andGmtCreateGreaterThanOrEqualTo(Date value) { 135 | addCriterion("gmt_create >=", value, "gmtCreate"); 136 | return (Criteria) this; 137 | } 138 | 139 | public Criteria andGmtCreateLessThan(Date value) { 140 | addCriterion("gmt_create <", value, "gmtCreate"); 141 | return (Criteria) this; 142 | } 143 | 144 | public Criteria andGmtCreateLessThanOrEqualTo(Date value) { 145 | addCriterion("gmt_create <=", value, "gmtCreate"); 146 | return (Criteria) this; 147 | } 148 | 149 | public Criteria andGmtCreateIn(List values) { 150 | addCriterion("gmt_create in", values, "gmtCreate"); 151 | return (Criteria) this; 152 | } 153 | 154 | public Criteria andGmtCreateNotIn(List values) { 155 | addCriterion("gmt_create not in", values, "gmtCreate"); 156 | return (Criteria) this; 157 | } 158 | 159 | public Criteria andGmtCreateBetween(Date value1, Date value2) { 160 | addCriterion("gmt_create between", value1, value2, "gmtCreate"); 161 | return (Criteria) this; 162 | } 163 | 164 | public Criteria andGmtCreateNotBetween(Date value1, Date value2) { 165 | addCriterion("gmt_create not between", value1, value2, "gmtCreate"); 166 | return (Criteria) this; 167 | } 168 | 169 | public Criteria andGmtModifiedIsNull() { 170 | addCriterion("gmt_modified is null"); 171 | return (Criteria) this; 172 | } 173 | 174 | public Criteria andGmtModifiedIsNotNull() { 175 | addCriterion("gmt_modified is not null"); 176 | return (Criteria) this; 177 | } 178 | 179 | public Criteria andGmtModifiedEqualTo(Date value) { 180 | addCriterion("gmt_modified =", value, "gmtModified"); 181 | return (Criteria) this; 182 | } 183 | 184 | public Criteria andGmtModifiedNotEqualTo(Date value) { 185 | addCriterion("gmt_modified <>", value, "gmtModified"); 186 | return (Criteria) this; 187 | } 188 | 189 | public Criteria andGmtModifiedGreaterThan(Date value) { 190 | addCriterion("gmt_modified >", value, "gmtModified"); 191 | return (Criteria) this; 192 | } 193 | 194 | public Criteria andGmtModifiedGreaterThanOrEqualTo(Date value) { 195 | addCriterion("gmt_modified >=", value, "gmtModified"); 196 | return (Criteria) this; 197 | } 198 | 199 | public Criteria andGmtModifiedLessThan(Date value) { 200 | addCriterion("gmt_modified <", value, "gmtModified"); 201 | return (Criteria) this; 202 | } 203 | 204 | public Criteria andGmtModifiedLessThanOrEqualTo(Date value) { 205 | addCriterion("gmt_modified <=", value, "gmtModified"); 206 | return (Criteria) this; 207 | } 208 | 209 | public Criteria andGmtModifiedIn(List values) { 210 | addCriterion("gmt_modified in", values, "gmtModified"); 211 | return (Criteria) this; 212 | } 213 | 214 | public Criteria andGmtModifiedNotIn(List values) { 215 | addCriterion("gmt_modified not in", values, "gmtModified"); 216 | return (Criteria) this; 217 | } 218 | 219 | public Criteria andGmtModifiedBetween(Date value1, Date value2) { 220 | addCriterion("gmt_modified between", value1, value2, "gmtModified"); 221 | return (Criteria) this; 222 | } 223 | 224 | public Criteria andGmtModifiedNotBetween(Date value1, Date value2) { 225 | addCriterion("gmt_modified not between", value1, value2, "gmtModified"); 226 | return (Criteria) this; 227 | } 228 | 229 | public Criteria andUserNameIsNull() { 230 | addCriterion("user_name is null"); 231 | return (Criteria) this; 232 | } 233 | 234 | public Criteria andUserNameIsNotNull() { 235 | addCriterion("user_name is not null"); 236 | return (Criteria) this; 237 | } 238 | 239 | public Criteria andUserNameEqualTo(String value) { 240 | addCriterion("user_name =", value, "userName"); 241 | return (Criteria) this; 242 | } 243 | 244 | public Criteria andUserNameNotEqualTo(String value) { 245 | addCriterion("user_name <>", value, "userName"); 246 | return (Criteria) this; 247 | } 248 | 249 | public Criteria andUserNameGreaterThan(String value) { 250 | addCriterion("user_name >", value, "userName"); 251 | return (Criteria) this; 252 | } 253 | 254 | public Criteria andUserNameGreaterThanOrEqualTo(String value) { 255 | addCriterion("user_name >=", value, "userName"); 256 | return (Criteria) this; 257 | } 258 | 259 | public Criteria andUserNameLessThan(String value) { 260 | addCriterion("user_name <", value, "userName"); 261 | return (Criteria) this; 262 | } 263 | 264 | public Criteria andUserNameLessThanOrEqualTo(String value) { 265 | addCriterion("user_name <=", value, "userName"); 266 | return (Criteria) this; 267 | } 268 | 269 | public Criteria andUserNameLike(String value) { 270 | addCriterion("user_name like", value, "userName"); 271 | return (Criteria) this; 272 | } 273 | 274 | public Criteria andUserNameNotLike(String value) { 275 | addCriterion("user_name not like", value, "userName"); 276 | return (Criteria) this; 277 | } 278 | 279 | public Criteria andUserNameIn(List values) { 280 | addCriterion("user_name in", values, "userName"); 281 | return (Criteria) this; 282 | } 283 | 284 | public Criteria andUserNameNotIn(List values) { 285 | addCriterion("user_name not in", values, "userName"); 286 | return (Criteria) this; 287 | } 288 | 289 | public Criteria andUserNameBetween(String value1, String value2) { 290 | addCriterion("user_name between", value1, value2, "userName"); 291 | return (Criteria) this; 292 | } 293 | 294 | public Criteria andUserNameNotBetween(String value1, String value2) { 295 | addCriterion("user_name not between", value1, value2, "userName"); 296 | return (Criteria) this; 297 | } 298 | 299 | public Criteria andLoginNameIsNull() { 300 | addCriterion("login_name is null"); 301 | return (Criteria) this; 302 | } 303 | 304 | public Criteria andLoginNameIsNotNull() { 305 | addCriterion("login_name is not null"); 306 | return (Criteria) this; 307 | } 308 | 309 | public Criteria andLoginNameEqualTo(String value) { 310 | addCriterion("login_name =", value, "loginName"); 311 | return (Criteria) this; 312 | } 313 | 314 | public Criteria andLoginNameNotEqualTo(String value) { 315 | addCriterion("login_name <>", value, "loginName"); 316 | return (Criteria) this; 317 | } 318 | 319 | public Criteria andLoginNameGreaterThan(String value) { 320 | addCriterion("login_name >", value, "loginName"); 321 | return (Criteria) this; 322 | } 323 | 324 | public Criteria andLoginNameGreaterThanOrEqualTo(String value) { 325 | addCriterion("login_name >=", value, "loginName"); 326 | return (Criteria) this; 327 | } 328 | 329 | public Criteria andLoginNameLessThan(String value) { 330 | addCriterion("login_name <", value, "loginName"); 331 | return (Criteria) this; 332 | } 333 | 334 | public Criteria andLoginNameLessThanOrEqualTo(String value) { 335 | addCriterion("login_name <=", value, "loginName"); 336 | return (Criteria) this; 337 | } 338 | 339 | public Criteria andLoginNameLike(String value) { 340 | addCriterion("login_name like", value, "loginName"); 341 | return (Criteria) this; 342 | } 343 | 344 | public Criteria andLoginNameNotLike(String value) { 345 | addCriterion("login_name not like", value, "loginName"); 346 | return (Criteria) this; 347 | } 348 | 349 | public Criteria andLoginNameIn(List values) { 350 | addCriterion("login_name in", values, "loginName"); 351 | return (Criteria) this; 352 | } 353 | 354 | public Criteria andLoginNameNotIn(List values) { 355 | addCriterion("login_name not in", values, "loginName"); 356 | return (Criteria) this; 357 | } 358 | 359 | public Criteria andLoginNameBetween(String value1, String value2) { 360 | addCriterion("login_name between", value1, value2, "loginName"); 361 | return (Criteria) this; 362 | } 363 | 364 | public Criteria andLoginNameNotBetween(String value1, String value2) { 365 | addCriterion("login_name not between", value1, value2, "loginName"); 366 | return (Criteria) this; 367 | } 368 | 369 | public Criteria andLoginPwdIsNull() { 370 | addCriterion("login_pwd is null"); 371 | return (Criteria) this; 372 | } 373 | 374 | public Criteria andLoginPwdIsNotNull() { 375 | addCriterion("login_pwd is not null"); 376 | return (Criteria) this; 377 | } 378 | 379 | public Criteria andLoginPwdEqualTo(String value) { 380 | addCriterion("login_pwd =", value, "loginPwd"); 381 | return (Criteria) this; 382 | } 383 | 384 | public Criteria andLoginPwdNotEqualTo(String value) { 385 | addCriterion("login_pwd <>", value, "loginPwd"); 386 | return (Criteria) this; 387 | } 388 | 389 | public Criteria andLoginPwdGreaterThan(String value) { 390 | addCriterion("login_pwd >", value, "loginPwd"); 391 | return (Criteria) this; 392 | } 393 | 394 | public Criteria andLoginPwdGreaterThanOrEqualTo(String value) { 395 | addCriterion("login_pwd >=", value, "loginPwd"); 396 | return (Criteria) this; 397 | } 398 | 399 | public Criteria andLoginPwdLessThan(String value) { 400 | addCriterion("login_pwd <", value, "loginPwd"); 401 | return (Criteria) this; 402 | } 403 | 404 | public Criteria andLoginPwdLessThanOrEqualTo(String value) { 405 | addCriterion("login_pwd <=", value, "loginPwd"); 406 | return (Criteria) this; 407 | } 408 | 409 | public Criteria andLoginPwdLike(String value) { 410 | addCriterion("login_pwd like", value, "loginPwd"); 411 | return (Criteria) this; 412 | } 413 | 414 | public Criteria andLoginPwdNotLike(String value) { 415 | addCriterion("login_pwd not like", value, "loginPwd"); 416 | return (Criteria) this; 417 | } 418 | 419 | public Criteria andLoginPwdIn(List values) { 420 | addCriterion("login_pwd in", values, "loginPwd"); 421 | return (Criteria) this; 422 | } 423 | 424 | public Criteria andLoginPwdNotIn(List values) { 425 | addCriterion("login_pwd not in", values, "loginPwd"); 426 | return (Criteria) this; 427 | } 428 | 429 | public Criteria andLoginPwdBetween(String value1, String value2) { 430 | addCriterion("login_pwd between", value1, value2, "loginPwd"); 431 | return (Criteria) this; 432 | } 433 | 434 | public Criteria andLoginPwdNotBetween(String value1, String value2) { 435 | addCriterion("login_pwd not between", value1, value2, "loginPwd"); 436 | return (Criteria) this; 437 | } 438 | 439 | public Criteria andStatusIsNull() { 440 | addCriterion("status is null"); 441 | return (Criteria) this; 442 | } 443 | 444 | public Criteria andStatusIsNotNull() { 445 | addCriterion("status is not null"); 446 | return (Criteria) this; 447 | } 448 | 449 | public Criteria andStatusEqualTo(Integer value) { 450 | addCriterion("status =", value, "status"); 451 | return (Criteria) this; 452 | } 453 | 454 | public Criteria andStatusNotEqualTo(Integer value) { 455 | addCriterion("status <>", value, "status"); 456 | return (Criteria) this; 457 | } 458 | 459 | public Criteria andStatusGreaterThan(Integer value) { 460 | addCriterion("status >", value, "status"); 461 | return (Criteria) this; 462 | } 463 | 464 | public Criteria andStatusGreaterThanOrEqualTo(Integer value) { 465 | addCriterion("status >=", value, "status"); 466 | return (Criteria) this; 467 | } 468 | 469 | public Criteria andStatusLessThan(Integer value) { 470 | addCriterion("status <", value, "status"); 471 | return (Criteria) this; 472 | } 473 | 474 | public Criteria andStatusLessThanOrEqualTo(Integer value) { 475 | addCriterion("status <=", value, "status"); 476 | return (Criteria) this; 477 | } 478 | 479 | public Criteria andStatusLike(Integer value) { 480 | addCriterion("status like", value, "status"); 481 | return (Criteria) this; 482 | } 483 | 484 | public Criteria andStatusNotLike(Integer value) { 485 | addCriterion("status not like", value, "status"); 486 | return (Criteria) this; 487 | } 488 | 489 | public Criteria andStatusIn(List values) { 490 | addCriterion("status in", values, "status"); 491 | return (Criteria) this; 492 | } 493 | 494 | public Criteria andStatusNotIn(List values) { 495 | addCriterion("status not in", values, "status"); 496 | return (Criteria) this; 497 | } 498 | 499 | public Criteria andStatusBetween(Integer value1, Integer value2) { 500 | addCriterion("status between", value1, value2, "status"); 501 | return (Criteria) this; 502 | } 503 | 504 | public Criteria andStatusNotBetween(Integer value1, Integer value2) { 505 | addCriterion("status not between", value1, value2, "status"); 506 | return (Criteria) this; 507 | } 508 | 509 | } 510 | 511 | /** 512 | * This class corresponds to the database table user 513 | */ 514 | public static class Criteria extends GeneratedCriteria{ 515 | protected Criteria() { 516 | super(); 517 | } 518 | } 519 | 520 | 521 | 522 | @Override 523 | public String toString(){ 524 | return ReflectionToStringBuilder.toString(this, ToStringStyle.DEFAULT_STYLE); 525 | } 526 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/manager/UserManager.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.manager; 2 | 3 | import com.leno.elastic.dal.common.PageResult; 4 | import com.leno.elastic.dal.model.UserDO; 5 | import com.leno.elastic.dal.query.UserQuery; 6 | 7 | import java.util.List; 8 | 9 | 10 | /** 11 | * Manager for User. 12 | */ 13 | public interface UserManager { 14 | /** 15 | * query count by query condition. 16 | */ 17 | int countByQuery(UserQuery query); 18 | 19 | /** 20 | * delete by query condition. 21 | */ 22 | int deleteByQuery(UserQuery query); 23 | 24 | /** 25 | * delete by primary key. 26 | */ 27 | int deleteByPrimaryKey(UserDO record); 28 | 29 | /** 30 | * insert selective. 31 | */ 32 | long insertSelective(UserDO record); 33 | 34 | /** 35 | * select by query condition. 36 | */ 37 | List selectByQuery(UserQuery query); 38 | 39 | 40 | /** 41 | * select by query condition with page. 42 | */ 43 | PageResult selectByQueryWithPage(UserQuery query); 44 | 45 | /** 46 | * select by primary key. 47 | */ 48 | UserDO selectByPrimaryKey(Long id); 49 | 50 | /** 51 | * update by query condition selective. 52 | */ 53 | int updateByQuerySelective( UserDO record, UserQuery query); 54 | 55 | /** 56 | * update by query condition. 57 | */ 58 | int updateByQuery(UserDO record, UserQuery query); 59 | 60 | /** 61 | * update by primary key selective. 62 | */ 63 | int updateByPrimaryKeySelective(UserDO record); 64 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/java/com/leno/elastic/manager/impl/UserManagerImpl.java: -------------------------------------------------------------------------------- 1 | package com.leno.elastic.manager.impl; 2 | 3 | import com.leno.elastic.dal.common.PageResult; 4 | import com.leno.elastic.dal.model.UserDO; 5 | import com.leno.elastic.dal.query.UserQuery; 6 | import com.leno.elastic.dal.mapper.ext.UserExtMapper; 7 | import com.leno.elastic.manager.UserManager; 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Component; 11 | 12 | import java.util.List; 13 | 14 | 15 | /** 16 | * Manager for User. 17 | */ 18 | 19 | @Component 20 | public class UserManagerImpl implements UserManager{ 21 | 22 | 23 | @Autowired 24 | protected UserExtMapper userExtMapper; 25 | /** 26 | * query count by query condition. 27 | */ 28 | @Override 29 | public int countByQuery(UserQuery query){ 30 | return userExtMapper.countByQuery(query); 31 | } 32 | 33 | /** 34 | * delete by query condition. 35 | */ 36 | @Override 37 | public int deleteByQuery(UserQuery query){ 38 | return userExtMapper.deleteByQuery(query); 39 | } 40 | 41 | /** 42 | * delete by primary key. 43 | */ 44 | @Override 45 | public int deleteByPrimaryKey(UserDO record){ 46 | return userExtMapper.deleteByPrimaryKey(record); 47 | } 48 | 49 | /** 50 | * insert selective. 51 | */ 52 | @Override 53 | public long insertSelective(UserDO record){ 54 | return userExtMapper.insertSelective(record); 55 | } 56 | 57 | /** 58 | * select by query condition. 59 | */ 60 | @Override 61 | public List selectByQuery(UserQuery query){ 62 | return userExtMapper.selectByQuery(query); 63 | } 64 | 65 | /** 66 | * select by query condition with page. 67 | */ 68 | @Override 69 | public PageResult selectByQueryWithPage(UserQuery query) { 70 | PageResult result = new PageResult(); 71 | result.setPageSize(query.getPageSize()); 72 | result.setPageNo(query.getPageNo()); 73 | result.setTotalCount(this.countByQuery(query)); 74 | result.setResult(this.selectByQuery(query)); 75 | return result; 76 | } 77 | 78 | /** 79 | * select by primary key. 80 | */ 81 | @Override 82 | public UserDO selectByPrimaryKey(Long id){ 83 | return userExtMapper.selectByPrimaryKey(id); 84 | } 85 | 86 | /** 87 | * update by query condition selective. 88 | */ 89 | @Override 90 | public int updateByQuerySelective( UserDO record, UserQuery query){ 91 | return userExtMapper.updateByQuerySelective(record, query); 92 | } 93 | 94 | /** 95 | * update by query condition. 96 | */ 97 | @Override 98 | public int updateByQuery( UserDO record, UserQuery query){ 99 | 100 | return userExtMapper.updateByQuery(record, query); 101 | } 102 | 103 | /** 104 | * update by primary key selective. 105 | */ 106 | @Override 107 | public int updateByPrimaryKeySelective(UserDO record){ 108 | return userExtMapper.updateByPrimaryKeySelective(record); 109 | } 110 | } -------------------------------------------------------------------------------- /elastic-dal/src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | and ${criterion.condition} 23 | 24 | 25 | and ${criterion.condition} #{criterion.value} 26 | 27 | 28 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 29 | 30 | 31 | and ${criterion.condition} 32 | 33 | #{listItem} 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | and ${criterion.condition} 53 | 54 | 55 | and ${criterion.condition} #{criterion.value} 56 | 57 | 58 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 59 | 60 | 61 | and ${criterion.condition} 62 | 63 | #{listItem} 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | user_id,gmt_create,gmt_modified,user_name,login_name,login_pwd,status 76 | 77 | 78 | 93 | 94 | 97 | 98 | 99 | delete from user where user_id = #{userId,jdbcType=BIGINT} 100 | 101 | 102 | 103 | delete from user 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | SELECT LAST_INSERT_ID() 112 | 113 | insert into user 114 | 115 | gmt_create,gmt_modified, 116 | 117 | user_name, 118 | 119 | 120 | login_name, 121 | 122 | 123 | login_pwd, 124 | 125 | 126 | status, 127 | 128 | 129 | 130 | now(),now(), 131 | 132 | #{userName,jdbcType=VARCHAR}, 133 | 134 | 135 | #{loginName,jdbcType=VARCHAR}, 136 | 137 | 138 | #{loginPwd,jdbcType=VARCHAR}, 139 | 140 | 141 | #{status,jdbcType=INTEGER}, 142 | 143 | 144 | 145 | 146 | 152 | 153 | 154 | update user 155 | 156 | gmt_modified=now(), 157 | 158 | user_name = #{record.userName,jdbcType=VARCHAR}, 159 | 160 | 161 | login_name = #{record.loginName,jdbcType=VARCHAR}, 162 | 163 | 164 | login_pwd = #{record.loginPwd,jdbcType=VARCHAR}, 165 | 166 | 167 | status = #{record.status,jdbcType=INTEGER}, 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | update user set 177 | user_name = #{record.userName,jdbcType=VARCHAR}, 178 | login_name = #{record.loginName,jdbcType=VARCHAR}, 179 | login_pwd = #{record.loginPwd,jdbcType=VARCHAR}, 180 | status = #{record.status,jdbcType=INTEGER}, 181 | gmt_modified = now() 182 | 183 | 184 | 185 | 186 | 187 | 188 | update user 189 | 190 | gmt_modified=now(), 191 | 192 | user_name = #{userName,jdbcType=VARCHAR}, 193 | 194 | 195 | login_name = #{loginName,jdbcType=VARCHAR}, 196 | 197 | 198 | login_pwd = #{loginPwd,jdbcType=VARCHAR}, 199 | 200 | 201 | status = #{status,jdbcType=INTEGER}, 202 | 203 | 204 | where user_id = #{userId,jdbcType=BIGINT} 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | -------------------------------------------------------------------------------- /elastic-dal/src/main/resources/mapper/ext/UserExtMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /elastic-generator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | elastic-generator 7 | jar 8 | 9 | mybatis generator 10 | 11 | 12 | com.leno 13 | elastic-demo 14 | 1.0-SNAPSHOT 15 | 16 | 17 | 18 | 19 | org.apache.commons 20 | commons-lang3 21 | 22 | 23 | commons-collections 24 | commons-collections 25 | 26 | 27 | mysql 28 | mysql-connector-java 29 | 30 | 31 | org.apache.velocity 32 | velocity 33 | 34 | 35 | commons-io 36 | commons-io 37 | 38 | 39 | com.google.guava 40 | guava 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /elastic-generator/src/main/java/com/leno/mybatisgen/ColumnInfo.java: -------------------------------------------------------------------------------- 1 | package com.leno.mybatisgen; 2 | 3 | /** 4 | * Created by houenxun on 16/3/14. 5 | */ 6 | public class ColumnInfo { 7 | private String name; 8 | private String calssName; 9 | private String typeName; 10 | private String precision; 11 | private String scale; 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public String getCalssName() { 22 | return calssName; 23 | } 24 | 25 | public void setCalssName(String calssName) { 26 | this.calssName = calssName; 27 | } 28 | 29 | public String getTypeName() { 30 | return typeName; 31 | } 32 | 33 | public void setTypeName(String typeName) { 34 | this.typeName = typeName; 35 | } 36 | 37 | public String getPrecision() { 38 | return precision; 39 | } 40 | 41 | public void setPrecision(String precision) { 42 | this.precision = precision; 43 | } 44 | 45 | public String getScale() { 46 | return scale; 47 | } 48 | 49 | public void setScale(String scale) { 50 | this.scale = scale; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /elastic-generator/src/main/java/com/leno/mybatisgen/Main.java: -------------------------------------------------------------------------------- 1 | package com.leno.mybatisgen; 2 | 3 | 4 | import org.apache.commons.collections.CollectionUtils; 5 | 6 | import java.net.URL; 7 | import java.nio.charset.StandardCharsets; 8 | import java.nio.file.Files; 9 | import java.nio.file.Paths; 10 | import java.util.List; 11 | 12 | /** 13 | * mybatisgen main. 14 | * 15 | * @author bleedfly. 16 | */ 17 | public class Main { 18 | 19 | public static void main(String[] args) throws Exception { 20 | try { 21 | 22 | // 需要生成的表配置文件在generate_tables.config文件中,每每行一个表名 23 | URL url = Thread.currentThread().getContextClassLoader().getResource("generate_tables.config"); 24 | if (url != null) { 25 | String path = url.getPath(); 26 | List tables = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8); 27 | if (CollectionUtils.isEmpty(tables)) { 28 | System.out.println("generate_tables.config is empty."); 29 | return; 30 | } 31 | MyBatisGenCore.batchGen(tables); 32 | System.out.print("all done"); 33 | } 34 | } catch (Exception e) { 35 | e.printStackTrace(); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/java/com/leno/mybatisgen/MyBatisGenConst.java: -------------------------------------------------------------------------------- 1 | package com.leno.mybatisgen; 2 | 3 | import java.io.File; 4 | 5 | @SuppressWarnings("all") 6 | public class MyBatisGenConst { 7 | 8 | /** 9 | * 项目名称 10 | */ 11 | private static final String PROJECT_NAME = "elastic"; 12 | 13 | /** 14 | * 组 15 | */ 16 | private static final String GROUP_NAME = "leno"; 17 | 18 | // module name 19 | private static final String MODULE_NAME = PROJECT_NAME + "-dal"; 20 | // group name 21 | private static final String MODULE_GROUP_NAME = "com." + GROUP_NAME + "." + PROJECT_NAME; 22 | // 工作目录 23 | public static final String WORK_DIR = System.getProperty("user.dir") + File.separator; 24 | // module目录 25 | private static final String MODULE_DIR = WORK_DIR + MODULE_NAME + File.separator; 26 | // src 目录 27 | private static final String MODULE_CODE_DIR = MODULE_DIR + "src/main/java/com" + File.separator + GROUP_NAME + File.separator + PROJECT_NAME + File.separator; 28 | 29 | 30 | // dal 31 | public static final String MAIN_PACKAGE = MODULE_GROUP_NAME + ".dal"; 32 | public static final String DO_PACKAGE = MAIN_PACKAGE + ".model"; 33 | public static final String QUERY_PACKAGE = MAIN_PACKAGE + ".query"; 34 | 35 | // mapper 36 | public static final String MAPPER_PACKAGE = MAIN_PACKAGE + ".mapper"; 37 | public static final String MAPPER_EXT_PACKAGE = MAPPER_PACKAGE + ".ext"; 38 | 39 | 40 | // manager 41 | public static final String MANAGER_PACKAGE = MODULE_GROUP_NAME + ".manager"; 42 | public static final String MANAGER_IMPL_PACKAGE = MANAGER_PACKAGE + ".impl"; 43 | 44 | 45 | public static final String TABLE_PREFIX = "cc_"; 46 | public static final String QUERY_PREFIX = "Query"; 47 | public static final String MAPPER_SUFFIX = "Mapper"; 48 | public static final String MANAGER_SUFFIX = "Manager"; 49 | public static final String MANAGER_IMPL_SUFFIX = "ManagerImpl"; 50 | public static final String MAPPER_EXT_SUFFIX = "ExtMapper"; 51 | public static final String DO_SUFFIX = "DO"; 52 | 53 | 54 | // mapper xml 输出目录 55 | public static final String MAPPER_XML_DIR = MODULE_DIR + "src/main/resources/mapper/"; 56 | // mapper-ext xml 输出目录 57 | public static final String MAPPER_EXT_XML_DIR = MODULE_DIR + "src/main/resources/mapper/ext/"; 58 | // do/model 输出目录 59 | public static final String MAPPER_DO_DIR = MODULE_CODE_DIR + "dal/model"; 60 | // query 输出目录 61 | public static final String MAPPER_QUERY_DIR = MODULE_CODE_DIR + "dal/query"; 62 | // mapper java 输出目录 63 | public static final String MAPPER_JAVA_DIR = MODULE_CODE_DIR + "dal/mapper"; 64 | 65 | public static final String MANAGER_JAVA_DIR = MODULE_CODE_DIR + "manager"; 66 | public static final String MANAGER_IMPL_JAVA_DIR = MANAGER_JAVA_DIR + "/impl"; 67 | 68 | // mapper-ext java 输出目录 69 | public static final String MAPPER_EXT_JAVA_DIR = MODULE_CODE_DIR + "dal/mapper/ext"; 70 | // java数据对象类模板 71 | public static final String DO_TEMPLATE = ClassLoader.getSystemResource("template/do.txt").getPath();// "resources/template/do.txt"; 72 | // query 模板 73 | public static final String QUERY_TEMPLATE = ClassLoader.getSystemResource("template/query.txt").getPath(); 74 | // sqlmap模板 75 | public static final String SQLMAP_TEMPLATE = ClassLoader.getSystemResource("template/sqlmap.txt").getPath(); 76 | // mapper模板 77 | public static final String MAPPER_TEMPLATE = ClassLoader.getSystemResource("template/mapper.txt").getPath(); 78 | // manager模板 79 | public static final String MANAGER_TEMPLATE = ClassLoader.getSystemResource("template/manager.txt").getPath(); 80 | // manager impl 模板 81 | public static final String MANAGER_IMPL_TEMPLATE = ClassLoader.getSystemResource("template/managerImpl.txt").getPath(); 82 | 83 | // mapper-ext模板 84 | public static final String MAPPER_EXT_TEMPLATE = ClassLoader.getSystemResource("template/mapper-ext.txt").getPath(); 85 | // sqlmap-ext模板 86 | public static final String SQLMAP_EXT_TEMPLATE = ClassLoader.getSystemResource("template/sqlmap-ext.txt").getPath(); 87 | 88 | public static final String COMMON_COLUMN_STR = "gmt_create,gmt_modified,"; 89 | 90 | // jdbc result set metadata collumn name 91 | public static final String RSMD_COLUMN_NAME = "rsmdColumnName"; 92 | public static final String RSMD_COLUMN_CLASS_NAME = "columnClassName"; 93 | public static final String RSMD_COLUMN_TYPE_NAME = "columnTypeName"; 94 | public static final String RSMD_COLUMN_PRECISION = "Precision"; 95 | public static final String RSMD_COLUMN_SCALE = "Scale"; 96 | public static final String RSMD_COLUMN_PRIMARY_KEY = "PrimaryKey"; 97 | public static final String RSMD_COLUMN_REMARKS = "Remarks"; 98 | 99 | public static final String COLUMN_NAME = "COLUMN_NAME"; 100 | 101 | // velocity param 102 | public static final String VP_LIST = "list"; 103 | public static final String VP_QUERY_PREFIX = "queryPrefix"; 104 | public static final String VP_DO_SUFFIX = "doSuffix"; 105 | public static final String VP_MAPPER_SUFFIX = "mapperSuffix"; 106 | public static final String VP_MANAGER_SUFFIX = "managerSuffix"; 107 | 108 | public static final String VP_MANAGER_IMPL_SUFFIX = "managerImplSuffix"; 109 | public static final String VP_MAPPER_EXT_SUFFIX = "extMapperSuffix"; 110 | public static final String VP_CLASS_NAME = "className"; 111 | public static final String VP_MAPPER_PROPERTY_NAME = "mapperPropertyName"; 112 | public static final String VP_MAIN_PACKAGE = "mainPackage"; 113 | public static final String VP_DO_PACKAGE = "doPackage"; 114 | public static final String VP_QUERY_PACKAGE = "queryPackage"; 115 | public static final String VP_MAPPER_PACKAGE = "mapperPackage"; 116 | public static final String VP_MANAGER_PACKAGE = "managerPackage"; 117 | public static final String VP_MANAGER_IMPL_PACKAGE = "managerImplPackage"; 118 | public static final String VP_MAPPER_EXT_PACKAGE = "mapperExtPackage"; 119 | public static final String VP_JAVA_TYPE = "javaType"; 120 | public static final String VP_PROP_NAME = "propName"; 121 | public static final String VP_GET_METHOD = "getMethod"; 122 | 123 | public static final String VP_SET_METHOD = "setMethod"; 124 | public static final String VP_COLUMN_NAME = "columnName"; 125 | public static final String VP_COLUMN_REMARKS = "columnRemarks"; 126 | public static final String VP_TABLE_NAME = "tableName"; 127 | public static final String VP_JDBC_TYPE = "jdbcType"; 128 | public static final String VP_COLS = "cols"; 129 | public static final String VP_COLS_WITHOUT_COMMON_COLUMNS = "colsWithoutCommColumns"; 130 | public static final String VP_SERIAL_VERSION_UID = "serialVersionUID"; 131 | 132 | public static final String VP_SERIAL_VERSION_UID2 = "serialVersionUID2"; 133 | public static final String VP_PRIMARY_KEY = "primaryKey"; 134 | public static final String VP_PROP_PRIMARY_KEY = "propPrimaryKey"; 135 | 136 | //分库分表 表后缀regex 137 | public static final String SHARDING_SUFFIX_REG = "_[\\d]{4}"; 138 | 139 | } 140 | -------------------------------------------------------------------------------- /elastic-generator/src/main/java/com/leno/mybatisgen/MyBatisGenCore.java: -------------------------------------------------------------------------------- 1 | package com.leno.mybatisgen; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.apache.commons.lang3.math.NumberUtils; 5 | import org.apache.velocity.VelocityContext; 6 | import org.apache.velocity.app.Velocity; 7 | 8 | import java.io.*; 9 | import java.nio.file.Files; 10 | import java.nio.file.Path; 11 | import java.nio.file.Paths; 12 | import java.sql.Connection; 13 | import java.sql.ResultSet; 14 | import java.sql.Statement; 15 | import java.sql.DatabaseMetaData; 16 | import java.sql.ResultSetMetaData; 17 | import java.sql.SQLException; 18 | import java.sql.DriverManager; 19 | import java.util.ArrayList; 20 | import java.util.HashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | import java.util.regex.Pattern; 24 | import java.util.Properties; 25 | 26 | /** 27 | * mybatisgen generate core. 28 | * 29 | * @author bleedfly. 30 | */ 31 | class MyBatisGenCore { 32 | 33 | /** 34 | * 根据表名获取字段信息 35 | * 36 | * @param cn 数据库连接 37 | * @param table 数据库表名 38 | * @return 列infoList 39 | * @throws Exception 抛出错误 40 | */ 41 | private static List> getColInfoList(Connection cn, String table) throws Exception { 42 | String sql = "select * from " + table + " where 1>2"; 43 | Statement stmt = null; 44 | ResultSet rs = null; 45 | try { 46 | //获取主键串 47 | DatabaseMetaData dbmd = cn.getMetaData(); 48 | ResultSet primaryKeys = dbmd.getPrimaryKeys(null, null, table); 49 | String pks = getstrPimaryKeys(primaryKeys); 50 | 51 | stmt = cn.createStatement(); 52 | rs = stmt.executeQuery(sql); 53 | // 获取结果集元数据信息 54 | ResultSetMetaData rsmd = rs.getMetaData(); 55 | 56 | int num = rsmd.getColumnCount(); 57 | Map map; 58 | List> list = new ArrayList>(); 59 | for (int i = 1; i <= num; i++) { 60 | map = new HashMap<>(); 61 | map.put(MyBatisGenConst.RSMD_COLUMN_NAME, rsmd.getColumnName(i)); 62 | map.put(MyBatisGenConst.RSMD_COLUMN_CLASS_NAME, rsmd.getColumnClassName(i)); 63 | map.put(MyBatisGenConst.RSMD_COLUMN_TYPE_NAME, rsmd.getColumnTypeName(i)); 64 | map.put(MyBatisGenConst.RSMD_COLUMN_PRECISION, rsmd.getPrecision(i) + ""); 65 | map.put(MyBatisGenConst.RSMD_COLUMN_SCALE, rsmd.getScale(i) + ""); 66 | String remarks = getColRemarks(cn, table, rsmd.getColumnName(i)); 67 | map.put(MyBatisGenConst.RSMD_COLUMN_REMARKS, remarks); 68 | list.add(map); 69 | } 70 | //主键串放入list 71 | map = new HashMap<>(); 72 | map.put(MyBatisGenConst.RSMD_COLUMN_PRIMARY_KEY, pks); 73 | list.add(map); 74 | return list; 75 | } catch (Exception e) { 76 | throw new Exception(e + ",table=" + table, e); 77 | } finally { 78 | try { 79 | if (stmt != null) { 80 | stmt.close(); 81 | } 82 | } catch (Exception e2) { 83 | e2.printStackTrace(); 84 | } 85 | try { 86 | if(rs!=null){ 87 | rs.close(); 88 | } 89 | } catch (Exception e2) { 90 | e2.printStackTrace(); 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * 根据表列名获取字段注释 97 | * 98 | * @param cn 数据库连接 99 | * @param table 数据库表名 100 | * @return 字段注释 101 | * @throws Exception 抛出异常 102 | */ 103 | private static String getColRemarks(Connection cn, String table, String columnName) throws Exception { 104 | //获取主键串 105 | DatabaseMetaData dbmd = cn.getMetaData(); 106 | ResultSet resultSet = dbmd.getTables(null, "%", "%", new String[]{"TABLE"}); 107 | while (resultSet.next()) { 108 | String tableName = resultSet.getString("TABLE_NAME"); 109 | if (table.equals(tableName)) { 110 | ResultSet rs = dbmd.getColumns(null, "%", tableName, "%"); 111 | while (rs.next()) { 112 | if (rs.getString("COLUMN_NAME").equals(columnName)) { 113 | return rs.getString("REMARKS"); 114 | } 115 | } 116 | } 117 | } 118 | return ""; 119 | } 120 | 121 | /** 122 | * 获取列信息 123 | * 124 | * @param table 表名 125 | * @return 列信息 126 | * @throws Exception 抛出错误 127 | */ 128 | private static List> getColInfoList(String table) throws Exception { 129 | Connection cn = getConnection(); 130 | try { 131 | return getColInfoList(cn, table); 132 | } finally { 133 | try { 134 | cn.close(); 135 | } catch (Exception e2) { 136 | e2.printStackTrace(); 137 | } 138 | } 139 | } 140 | 141 | /** 142 | * 获取参数列表 143 | * 144 | * @param colInfoList 列信息列表 145 | * @return 参数列表 146 | * @throws Exception 抛出异常 147 | */ 148 | private static List> makeParamList(List> colInfoList) throws Exception { 149 | List> list = new ArrayList<>(); 150 | Map map; 151 | Map mapNew; 152 | for (Map aColInfoList : colInfoList) { 153 | map = aColInfoList; 154 | mapNew = new HashMap<>(); 155 | String columnName = map.get(MyBatisGenConst.RSMD_COLUMN_NAME); 156 | String columnClassName = map.get(MyBatisGenConst.RSMD_COLUMN_CLASS_NAME); 157 | String columnTypeName = map.get(MyBatisGenConst.RSMD_COLUMN_TYPE_NAME); 158 | String scaleStr = map.get(MyBatisGenConst.RSMD_COLUMN_SCALE); 159 | int scale = NumberUtils.toInt(scaleStr); 160 | String precisionStr = map.get(MyBatisGenConst.RSMD_COLUMN_PRECISION); 161 | String remarks = map.get(MyBatisGenConst.RSMD_COLUMN_REMARKS); 162 | int precision = NumberUtils.toInt(precisionStr); 163 | String javaType = getJavaType(columnClassName, columnTypeName, scale, precision); 164 | String jdbcType = getJdbcType(columnClassName, columnTypeName); 165 | String propName = getPropName(columnName); 166 | String setMethod = getSetMethod(propName); 167 | String getMethod = getGetMethod(propName); 168 | mapNew.put(MyBatisGenConst.VP_COLUMN_NAME, columnName.toLowerCase()); 169 | mapNew.put(MyBatisGenConst.VP_COLUMN_REMARKS, remarks); 170 | mapNew.put(MyBatisGenConst.VP_PROP_NAME, propName); 171 | mapNew.put(MyBatisGenConst.VP_JAVA_TYPE, javaType); 172 | mapNew.put(MyBatisGenConst.VP_JDBC_TYPE, jdbcType); 173 | mapNew.put(MyBatisGenConst.VP_GET_METHOD, getMethod); 174 | mapNew.put(MyBatisGenConst.VP_SET_METHOD, setMethod); 175 | list.add(mapNew); 176 | } 177 | return list; 178 | } 179 | 180 | /** 181 | * 获取字段的java类型 182 | * 183 | * @param columnClassName 字段类名 184 | * @param columnTypeName 字段类型名称 185 | * @param scale 精度 小数位数 186 | * @param precision 精度 187 | * @return java类型 188 | */ 189 | private static String getJavaType(String columnClassName, String columnTypeName, int scale, int precision) { 190 | if (columnClassName.equals("java.sql.Timestamp")) { 191 | return "Date"; 192 | } 193 | if (columnClassName.equals("java.lang.String")) { 194 | return "String"; 195 | } 196 | if (columnTypeName.equals("DECIMAL") && scale < 1) { 197 | return "Long"; 198 | } 199 | if (columnTypeName.equals("DECIMAL") && scale > 0) { 200 | return "java.math.BigDecimal"; 201 | } 202 | if (columnTypeName.startsWith("BIGINT")) { 203 | return "Long"; 204 | } 205 | if (columnTypeName.startsWith("INT")) { 206 | return "Integer"; 207 | } 208 | if (columnTypeName.startsWith("TINYINT") && precision == 1) { 209 | return "Boolean"; 210 | } 211 | if (columnTypeName.startsWith("TINYINT") && precision != 1) { 212 | return "Integer"; 213 | } 214 | if (columnTypeName.startsWith("BIT") && precision > 1) { 215 | return "Integer"; 216 | } 217 | if (columnTypeName.startsWith("BIT") && precision == 1) { 218 | return "Boolean"; 219 | } 220 | if (columnTypeName.startsWith("SMALLINT")) { 221 | return "Integer"; 222 | } 223 | return columnClassName; 224 | } 225 | 226 | /** 227 | * 获取jdbc类型 228 | * 229 | * @param columnClassName 字段类名 230 | * @param columnTypeName 字段类型名称 231 | * @return jdbc类型 232 | */ 233 | private static String getJdbcType(String columnClassName, String columnTypeName) { 234 | if (columnClassName.equals("java.lang.String")) { 235 | return "VARCHAR"; 236 | } 237 | if (columnClassName.startsWith("java.sql.")) { 238 | return "TIMESTAMP"; 239 | } 240 | if (columnTypeName.startsWith("NUMBER")) { 241 | return "DECIMAL"; 242 | } 243 | if (columnTypeName.startsWith("INT")) { 244 | return "INTEGER"; 245 | } 246 | return columnTypeName; 247 | } 248 | 249 | /** 250 | * 根据表名获取java类型 251 | * 252 | * @param tableName 表名 253 | * @return java类型 254 | */ 255 | private static String getClassName(String tableName) { 256 | String t = tableName.toLowerCase(); 257 | t = t.replace(MyBatisGenConst.TABLE_PREFIX, ""); 258 | String[] arr = t.split("_"); 259 | StringBuilder s = new StringBuilder(); 260 | for (String anArr : arr) { 261 | s.append(StringUtils.capitalize(anArr)); 262 | } 263 | return s.toString(); 264 | } 265 | 266 | /** 267 | * 根据字段名获取java数据对象属性名 268 | * 269 | * @param columnName 字段名 270 | * @return 属性名 271 | */ 272 | private static String getPropName(String columnName) { 273 | String t = columnName.toLowerCase(); 274 | String[] arr = t.split("_"); 275 | int num = arr.length; 276 | StringBuilder s = new StringBuilder(); 277 | for (int i = 0; i < num; i++) { 278 | if (i > 0) { 279 | s.append(StringUtils.capitalize(arr[i])); 280 | } else { 281 | s.append(arr[i]); 282 | } 283 | } 284 | return s.toString(); 285 | } 286 | 287 | private static String getSetMethod(String propName) { 288 | return "set" + StringUtils.capitalize(propName); 289 | } 290 | 291 | private static String getGetMethod(String propName) { 292 | return "get" + StringUtils.capitalize(propName); 293 | } 294 | 295 | private static String getColsStr(List> list) { 296 | int num = list.size(); 297 | Map map; 298 | String colName; 299 | StringBuilder stringBuilder = new StringBuilder(); 300 | for (int i = 0; i < num; i++) { 301 | map = list.get(i); 302 | colName = map.get(MyBatisGenConst.VP_COLUMN_NAME); 303 | if (i > 0) { 304 | stringBuilder.append(","); 305 | } 306 | stringBuilder.append(colName); 307 | } 308 | return stringBuilder.toString(); 309 | } 310 | 311 | private static String getstrPimaryKeys(ResultSet primaryKeys) throws SQLException { 312 | StringBuilder sb = new StringBuilder(); 313 | String pks; 314 | while (primaryKeys.next()) { 315 | sb.append(primaryKeys.getString(MyBatisGenConst.COLUMN_NAME)).append(","); 316 | } 317 | pks = sb.toString(); 318 | return StringUtils.isBlank(pks) ? "" : pks.substring(0, pks.length() - 1); 319 | } 320 | 321 | /** 322 | * velocity模板合并 323 | * 324 | * @param template 模板字符串 如 hello,${name} 325 | * @param paramMap 参数 326 | * @return 合并结果 327 | * @throws Exception 抛出错误 328 | */ 329 | private static String merge(String template, Map paramMap) throws Exception { 330 | VelocityContext vc = new VelocityContext(paramMap); 331 | StringWriter writer = new StringWriter(); 332 | Velocity.evaluate(vc, writer, "mybatis_code_gen", template); 333 | return writer.getBuffer().toString(); 334 | } 335 | 336 | /** 337 | * 获取sqlmap 参数列表 去掉 主键 GMT_CREATE GMT_MODIFIED 字段 338 | * 339 | * @param paramList 参数列表 Map 340 | * @param pks 主键 341 | * @return sql 参数列表 342 | * @throws Exception 抛出错误 343 | */ 344 | private static List> getSqlmapParamList(List> paramList, String pks) throws Exception { 345 | List> list = new ArrayList<>(); 346 | Map tmp; 347 | Map map; 348 | for (Map aParamList : paramList) { 349 | tmp = aParamList; 350 | String columnName = tmp.get(MyBatisGenConst.VP_COLUMN_NAME); 351 | if (columnName.equalsIgnoreCase(pks)) { 352 | continue; 353 | } 354 | if (columnName.equalsIgnoreCase("GMT_CREATE")) { 355 | continue; 356 | } 357 | if (columnName.equalsIgnoreCase("GMT_MODIFIED")) { 358 | continue; 359 | } 360 | map = new HashMap<>(); 361 | map.putAll(tmp); 362 | list.add(map); 363 | } 364 | return list; 365 | } 366 | 367 | /** 368 | * 根据表名生成java数据对象类文件和sqlmap文件 369 | * 370 | * @param table 表名 371 | * @throws Exception 抛出错误 372 | */ 373 | private static void gen(String table) throws Exception { 374 | 375 | List> colInfoList = getColInfoList(table); 376 | String pks = colInfoList.remove(colInfoList.size() - 1).get(MyBatisGenConst.RSMD_COLUMN_PRIMARY_KEY); 377 | List> paramList = makeParamList(colInfoList); 378 | 379 | boolean isSharding = Pattern.compile(MyBatisGenConst.SHARDING_SUFFIX_REG).matcher(table).find(); 380 | 381 | if (isSharding) { 382 | // 去掉分库分表后面的表后缀,如_0001 383 | table = table.replaceAll(MyBatisGenConst.SHARDING_SUFFIX_REG, ""); 384 | } 385 | 386 | String className = getClassName(table); 387 | 388 | String doTemplate = readFileToString(new File(MyBatisGenConst.DO_TEMPLATE)); 389 | String queryTemplate = readFileToString(new File(MyBatisGenConst.QUERY_TEMPLATE)); 390 | String sqlmapTemplate = readFileToString(new File(MyBatisGenConst.SQLMAP_TEMPLATE)); 391 | String mapperTemplate = readFileToString(new File(MyBatisGenConst.MAPPER_TEMPLATE)); 392 | String managerTemplate = readFileToString(new File(MyBatisGenConst.MANAGER_TEMPLATE)); 393 | String managerImplTemplate = readFileToString(new File(MyBatisGenConst.MANAGER_IMPL_TEMPLATE)); 394 | String sqlmapExtTemplate = readFileToString(new File(MyBatisGenConst.SQLMAP_EXT_TEMPLATE)); 395 | String mapperExtTemplate = readFileToString(new File(MyBatisGenConst.MAPPER_EXT_TEMPLATE)); 396 | 397 | Map param = new HashMap<>(); 398 | 399 | param.put(MyBatisGenConst.VP_MAIN_PACKAGE, MyBatisGenConst.MAIN_PACKAGE); 400 | param.put(MyBatisGenConst.VP_DO_PACKAGE, MyBatisGenConst.DO_PACKAGE); 401 | param.put(MyBatisGenConst.VP_QUERY_PACKAGE, MyBatisGenConst.QUERY_PACKAGE); 402 | param.put(MyBatisGenConst.VP_MAPPER_PACKAGE, MyBatisGenConst.MAPPER_PACKAGE); 403 | param.put(MyBatisGenConst.VP_MANAGER_PACKAGE, MyBatisGenConst.MANAGER_PACKAGE); 404 | param.put(MyBatisGenConst.VP_MANAGER_IMPL_PACKAGE, MyBatisGenConst.MANAGER_IMPL_PACKAGE); 405 | param.put(MyBatisGenConst.VP_MAPPER_EXT_PACKAGE, MyBatisGenConst.MAPPER_EXT_PACKAGE); 406 | param.put(MyBatisGenConst.VP_CLASS_NAME, className); 407 | param.put(MyBatisGenConst.VP_MAPPER_PROPERTY_NAME, className.substring(0, 1).toLowerCase() + className.substring(1) + MyBatisGenConst.MAPPER_EXT_SUFFIX); 408 | 409 | param.put(MyBatisGenConst.VP_LIST, paramList); 410 | param.put(MyBatisGenConst.VP_QUERY_PREFIX, MyBatisGenConst.QUERY_PREFIX); 411 | param.put(MyBatisGenConst.VP_DO_SUFFIX, MyBatisGenConst.DO_SUFFIX); 412 | param.put(MyBatisGenConst.VP_MAPPER_SUFFIX, MyBatisGenConst.MAPPER_SUFFIX); 413 | param.put(MyBatisGenConst.VP_MANAGER_SUFFIX, MyBatisGenConst.MANAGER_SUFFIX); 414 | param.put(MyBatisGenConst.VP_MANAGER_IMPL_SUFFIX, MyBatisGenConst.MANAGER_IMPL_SUFFIX); 415 | 416 | 417 | param.put(MyBatisGenConst.VP_MAPPER_EXT_SUFFIX, MyBatisGenConst.MAPPER_EXT_SUFFIX); 418 | 419 | String vpTableName = table.toLowerCase(); 420 | if (isSharding) { 421 | vpTableName += "_$tabNum$"; 422 | } 423 | 424 | param.put(MyBatisGenConst.VP_TABLE_NAME, vpTableName); 425 | param.put(MyBatisGenConst.VP_SERIAL_VERSION_UID, "" + (long) (Math.random() * 1000000000000000000L)); 426 | 427 | param.put(MyBatisGenConst.VP_SERIAL_VERSION_UID2, "" + (long) (Math.random() * 1000000000000000000L)); 428 | 429 | String doResult = merge(doTemplate, param); 430 | 431 | // 获取字段名不包含 id gmt_create gmt_modified 去掉主键 432 | List> sqlmapParamList = getSqlmapParamList(paramList, pks); 433 | param.put(MyBatisGenConst.VP_LIST, sqlmapParamList); 434 | 435 | String colsWithoutCommColumns = getColsStr(sqlmapParamList); 436 | param.put(MyBatisGenConst.VP_COLS_WITHOUT_COMMON_COLUMNS, colsWithoutCommColumns); 437 | String cols = pks + "," + MyBatisGenConst.COMMON_COLUMN_STR + colsWithoutCommColumns; 438 | param.put(MyBatisGenConst.VP_COLS, cols); 439 | param.put(MyBatisGenConst.VP_PRIMARY_KEY, pks); 440 | param.put(MyBatisGenConst.VP_PROP_PRIMARY_KEY, getPropName(pks)); 441 | String sqlmapResult = merge(sqlmapTemplate, param); 442 | String mapperResult = merge(mapperTemplate, param); 443 | String managerResult = merge(managerTemplate, param); 444 | String managerImplResult = merge(managerImplTemplate, param); 445 | String queryResult = merge(queryTemplate, param); 446 | String mapperExtResult = merge(mapperExtTemplate, param); 447 | String sqlmapExtResult = merge(sqlmapExtTemplate, param); 448 | 449 | String doOutFilePath = MyBatisGenConst.MAPPER_DO_DIR + "/" + className + MyBatisGenConst.DO_SUFFIX + ".java"; 450 | String queryOutFilePath = MyBatisGenConst.MAPPER_QUERY_DIR + "/" + className + MyBatisGenConst.QUERY_PREFIX + ".java"; 451 | String sqlmapOutFilePath = MyBatisGenConst.MAPPER_XML_DIR + "/" + className + MyBatisGenConst.MAPPER_SUFFIX + ".xml"; 452 | String mapperOutFilePath = MyBatisGenConst.MAPPER_JAVA_DIR + "/" + className + MyBatisGenConst.MAPPER_SUFFIX + ".java"; 453 | String managerOutFilePath = MyBatisGenConst.MANAGER_JAVA_DIR + "/" + className + MyBatisGenConst.MANAGER_SUFFIX + ".java"; 454 | String managerImplOutFilePath = MyBatisGenConst.MANAGER_IMPL_JAVA_DIR + "/" + className + MyBatisGenConst.MANAGER_IMPL_SUFFIX + ".java"; 455 | String sqlmapExtOutFilePath = MyBatisGenConst.MAPPER_EXT_XML_DIR + "/" + className + MyBatisGenConst.MAPPER_EXT_SUFFIX + ".xml"; 456 | String mapperExtOutFilePath = MyBatisGenConst.MAPPER_EXT_JAVA_DIR + "/" + className + MyBatisGenConst.MAPPER_EXT_SUFFIX + ".java"; 457 | 458 | boolean success = new File(MyBatisGenConst.MAPPER_DO_DIR).mkdirs(); 459 | if (!success) { 460 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MAPPER_DO_DIR)); 461 | } 462 | success = new File(MyBatisGenConst.MAPPER_QUERY_DIR).mkdirs(); 463 | if (!success) { 464 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MAPPER_QUERY_DIR)); 465 | } 466 | 467 | success = new File(MyBatisGenConst.MANAGER_JAVA_DIR).mkdirs(); 468 | if (!success) { 469 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MANAGER_JAVA_DIR)); 470 | } 471 | 472 | success = new File(MyBatisGenConst.MANAGER_IMPL_JAVA_DIR).mkdirs(); 473 | if (!success) { 474 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MANAGER_IMPL_JAVA_DIR)); 475 | } 476 | 477 | 478 | success = new File(MyBatisGenConst.MAPPER_XML_DIR).mkdirs(); 479 | if (!success) { 480 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MAPPER_XML_DIR)); 481 | } 482 | success = new File(MyBatisGenConst.MAPPER_JAVA_DIR).mkdirs(); 483 | if (!success) { 484 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MAPPER_JAVA_DIR)); 485 | } 486 | success = new File(MyBatisGenConst.MAPPER_EXT_XML_DIR).mkdirs(); 487 | if (!success) { 488 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MAPPER_EXT_XML_DIR)); 489 | } 490 | success = new File(MyBatisGenConst.MAPPER_EXT_JAVA_DIR).mkdirs(); 491 | if (!success) { 492 | System.out.println(String.format("mkdir %s error", MyBatisGenConst.MAPPER_EXT_JAVA_DIR)); 493 | } 494 | 495 | File sqlmapOutFile = new File(sqlmapOutFilePath); 496 | if (!sqlmapOutFile.exists()) { 497 | success = sqlmapOutFile.createNewFile(); 498 | if (!success) { 499 | System.out.println(String.format("createNewFile %s error", sqlmapOutFilePath)); 500 | } 501 | } 502 | File doOutFile = new File(doOutFilePath); 503 | if (!doOutFile.exists()) { 504 | success = doOutFile.createNewFile(); 505 | if (!success) { 506 | System.out.println(String.format("createNewFile %s error", doOutFilePath)); 507 | } 508 | } 509 | File mapperOutFile = new File(mapperOutFilePath); 510 | if (!mapperOutFile.exists()) { 511 | success = mapperOutFile.createNewFile(); 512 | if (!success) { 513 | System.out.println(String.format("createNewFile %s error", mapperOutFilePath)); 514 | } 515 | } 516 | 517 | 518 | File queryOutFile = new File(queryOutFilePath); 519 | if (!queryOutFile.exists()) { 520 | success = queryOutFile.createNewFile(); 521 | if (!success) { 522 | System.out.println(String.format("createNewFile %s error", queryOutFilePath)); 523 | } 524 | } 525 | 526 | writeStringToFile(sqlmapOutFile, sqlmapResult); 527 | writeStringToFile(doOutFile, doResult); 528 | writeStringToFile(queryOutFile, queryResult); 529 | writeStringToFile(mapperOutFile, mapperResult); 530 | 531 | 532 | File mapperExtOutFile = new File(mapperExtOutFilePath); 533 | if (!mapperExtOutFile.exists()) { 534 | try { 535 | if(mapperExtOutFile.createNewFile()){ 536 | writeStringToFile(mapperExtOutFile, mapperExtResult); 537 | } 538 | } catch (IOException e) { 539 | System.out.print("create file error"); 540 | } 541 | } 542 | 543 | File sqlmapExtOutFile = new File(sqlmapExtOutFilePath); 544 | if (!sqlmapExtOutFile.exists()) { 545 | 546 | if (sqlmapExtOutFile.createNewFile()) { 547 | writeStringToFile(sqlmapExtOutFile, sqlmapExtResult); 548 | } 549 | } 550 | 551 | 552 | File managerOutFile = new File(managerOutFilePath); 553 | if (!managerOutFile.exists()) { 554 | createAndWriteFile(managerResult, managerOutFilePath, managerOutFile); 555 | } 556 | 557 | File managerImplOutFile = new File(managerImplOutFilePath); 558 | if (!managerImplOutFile.exists()) { 559 | createAndWriteFile(managerImplResult, managerImplOutFilePath, managerImplOutFile); 560 | } 561 | } 562 | 563 | private static void createAndWriteFile(String managerResult, String managerOutFilePath, File managerOutFile) throws Exception { 564 | boolean success; 565 | success = managerOutFile.createNewFile(); 566 | if (!success) { 567 | System.out.println(String.format("createNewFile %s error", managerOutFilePath)); 568 | } else { 569 | writeStringToFile(managerOutFile, managerResult); 570 | } 571 | } 572 | 573 | private static void writeStringToFile(File file, String data) throws Exception { 574 | Path path = Paths.get(file.getAbsolutePath()); 575 | try (BufferedWriter writer = Files.newBufferedWriter(path)) { 576 | writer.write(data); 577 | } 578 | } 579 | 580 | private static String readFileToString(File file) throws Exception { 581 | Path path = Paths.get(file.getAbsolutePath()); 582 | return new String(Files.readAllBytes(path),"UTF8"); 583 | } 584 | 585 | /** 586 | * 获取数据库连接 587 | * 588 | * @return 数据库连接 589 | * @throws ClassNotFoundException 类找不到异常 590 | * @throws SQLException SQL 异常 591 | * @throws IOException IO 异常 592 | */ 593 | private static Connection getConnection() throws ClassNotFoundException, SQLException, IOException { 594 | Properties prop = new Properties(); 595 | prop.load(ClassLoader.getSystemResourceAsStream("db.config")); 596 | Class.forName(prop.getProperty("driver")); 597 | String url = prop.getProperty("url"); 598 | String user = prop.getProperty("user"); 599 | String psw = prop.getProperty("pwd"); 600 | return DriverManager.getConnection(url, user, psw); 601 | } 602 | 603 | /** 604 | * 批量生成java数据对象类文件和sqlmap文件 605 | * 606 | * @param tables 表 多个表用逗号分隔 table1,table2,table3 607 | * @throws Exception 抛出异常 608 | */ 609 | static void batchGen(List tables) throws Exception { 610 | System.out.println("table num " + tables.size()); 611 | Connection cn = getConnection(); 612 | try { 613 | for (String table : tables) { 614 | MyBatisGenCore.gen(table.trim()); 615 | System.out.println(table.trim() + " done"); 616 | } 617 | } finally { 618 | try { 619 | cn.close(); 620 | } catch (Exception e) { 621 | System.out.println("batch error!"); 622 | } 623 | } 624 | } 625 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/java/com/leno/mybatisgen/enums/ColumnTypeEnum.java: -------------------------------------------------------------------------------- 1 | package com.leno.mybatisgen.enums; 2 | 3 | /** 4 | * Created by houenxun on 16/3/14. 5 | * 列类型 6 | */ 7 | public enum ColumnTypeEnum { 8 | normal, //普通列 9 | primary, //主键 10 | gmtCreate,// 创建日期 11 | gmtModified, // 最后修改时间 12 | lockVersion, // 乐观锁版本 13 | status, //状态 14 | isDeleted,// 逻辑删除 15 | ; 16 | 17 | 18 | private ColumnTypeEnum() { 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/db.config: -------------------------------------------------------------------------------- 1 | driver=com.mysql.jdbc.Driver 2 | url=jdbc:mysql://localhost:3306/moyu_db?useUnicode=true&characterEncoding=utf-8&useSSL=true 3 | user=root 4 | pwd=alixian -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/generate_tables.config: -------------------------------------------------------------------------------- 1 | user -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/do.txt: -------------------------------------------------------------------------------- 1 | package ${doPackage}; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import lombok.Data; 6 | 7 | @Data 8 | public class ${className}${doSuffix} implements Serializable { 9 | 10 | private static final long serialVersionUID = 1L; 11 | 12 | #foreach($item in $list) 13 | /** 14 | * ${item.columnRemarks} 15 | * ${tableName}.${item.columnName} 16 | */ 17 | private ${item.javaType} ${item.propName}; 18 | 19 | #end 20 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/manager.txt: -------------------------------------------------------------------------------- 1 | package ${managerPackage}; 2 | 3 | import ${mainPackage}.common.PageResult; 4 | import ${doPackage}.${className}${doSuffix}; 5 | import ${queryPackage}.${className}${queryPrefix}; 6 | 7 | import java.util.List; 8 | 9 | 10 | /** 11 | * Manager for ${className}. 12 | */ 13 | public interface ${className}${managerSuffix} { 14 | /** 15 | * query count by query condition. 16 | */ 17 | int countBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 18 | 19 | /** 20 | * delete by query condition. 21 | */ 22 | int deleteBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 23 | 24 | /** 25 | * delete by primary key. 26 | */ 27 | int deleteByPrimaryKey(${className}${doSuffix} record); 28 | 29 | /** 30 | * insert selective. 31 | */ 32 | long insertSelective(${className}${doSuffix} record); 33 | 34 | /** 35 | * select by query condition. 36 | */ 37 | List<${className}${doSuffix}> selectBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 38 | 39 | 40 | /** 41 | * select by query condition with page. 42 | */ 43 | PageResult<${className}${doSuffix}> selectBy${queryPrefix}WithPage(${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 44 | 45 | /** 46 | * select by primary key. 47 | */ 48 | ${className}${doSuffix} selectByPrimaryKey(Long id); 49 | 50 | /** 51 | * update by query condition selective. 52 | */ 53 | int updateBy${queryPrefix}Selective( ${className}${doSuffix} record, ${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 54 | 55 | /** 56 | * update by query condition. 57 | */ 58 | int updateBy${queryPrefix}(${className}${doSuffix} record, ${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 59 | 60 | /** 61 | * update by primary key selective. 62 | */ 63 | int updateByPrimaryKeySelective(${className}${doSuffix} record); 64 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/managerImpl.txt: -------------------------------------------------------------------------------- 1 | package ${managerImplPackage}; 2 | 3 | import ${mainPackage}.common.PageResult; 4 | import ${doPackage}.${className}${doSuffix}; 5 | import ${queryPackage}.${className}${queryPrefix}; 6 | import ${mapperExtPackage}.${className}${extMapperSuffix}; 7 | import ${managerPackage}.${className}${managerSuffix}; 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Component; 11 | 12 | import java.util.List; 13 | 14 | 15 | /** 16 | * Manager for ${className}. 17 | */ 18 | 19 | @Component 20 | public class ${className}${managerImplSuffix} implements ${className}${managerSuffix}{ 21 | 22 | #set($managerMapperName= ${mapperPropertyName}) 23 | 24 | @Autowired 25 | protected ${className}${extMapperSuffix} ${managerMapperName}; 26 | /** 27 | * query count by query condition. 28 | */ 29 | @Override 30 | public int countBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}){ 31 | return ${managerMapperName}.countBy${queryPrefix}(${queryPrefix.toLowerCase()}); 32 | } 33 | 34 | /** 35 | * delete by query condition. 36 | */ 37 | @Override 38 | public int deleteBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}){ 39 | return ${managerMapperName}.deleteBy${queryPrefix}(${queryPrefix.toLowerCase()}); 40 | } 41 | 42 | /** 43 | * delete by primary key. 44 | */ 45 | @Override 46 | public int deleteByPrimaryKey(${className}${doSuffix} record){ 47 | return ${managerMapperName}.deleteByPrimaryKey(record); 48 | } 49 | 50 | /** 51 | * insert selective. 52 | */ 53 | @Override 54 | public long insertSelective(${className}${doSuffix} record){ 55 | return ${managerMapperName}.insertSelective(record); 56 | } 57 | 58 | /** 59 | * select by query condition. 60 | */ 61 | @Override 62 | public List<${className}${doSuffix}> selectBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}){ 63 | return ${managerMapperName}.selectBy${queryPrefix}(${queryPrefix.toLowerCase()}); 64 | } 65 | 66 | /** 67 | * select by query condition with page. 68 | */ 69 | @Override 70 | public PageResult<${className}${doSuffix}> selectBy${queryPrefix}WithPage(${className}${queryPrefix} ${queryPrefix.toLowerCase()}) { 71 | PageResult<${className}${doSuffix}> result = new PageResult<${className}${doSuffix}>(); 72 | result.setPageSize(query.getPageSize()); 73 | result.setPageNo(query.getPageNo()); 74 | result.setTotalCount(this.countBy${queryPrefix}(${queryPrefix.toLowerCase()})); 75 | result.setResult(this.selectBy${queryPrefix}(${queryPrefix.toLowerCase()})); 76 | return result; 77 | } 78 | 79 | /** 80 | * select by primary key. 81 | */ 82 | @Override 83 | public ${className}${doSuffix} selectByPrimaryKey(Long id){ 84 | return ${managerMapperName}.selectByPrimaryKey(id); 85 | } 86 | 87 | /** 88 | * update by query condition selective. 89 | */ 90 | @Override 91 | public int updateBy${queryPrefix}Selective( ${className}${doSuffix} record, ${className}${queryPrefix} ${queryPrefix.toLowerCase()}){ 92 | return ${managerMapperName}.updateBy${queryPrefix}Selective(record, ${queryPrefix.toLowerCase()}); 93 | } 94 | 95 | /** 96 | * update by query condition. 97 | */ 98 | @Override 99 | public int updateBy${queryPrefix}( ${className}${doSuffix} record, ${className}${queryPrefix} ${queryPrefix.toLowerCase()}){ 100 | 101 | return ${managerMapperName}.updateBy${queryPrefix}(record, ${queryPrefix.toLowerCase()}); 102 | } 103 | 104 | /** 105 | * update by primary key selective. 106 | */ 107 | @Override 108 | public int updateByPrimaryKeySelective(${className}${doSuffix} record){ 109 | return ${managerMapperName}.updateByPrimaryKeySelective(record); 110 | } 111 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/mapper-ext.txt: -------------------------------------------------------------------------------- 1 | package ${mapperExtPackage}; 2 | 3 | import ${mapperPackage}.${className}${mapperSuffix}; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.springframework.stereotype.Repository; 6 | 7 | /** 8 | * MyBatis Ext Mapper for ${className}. 9 | */ 10 | 11 | @Mapper 12 | @Repository 13 | public interface ${className}${extMapperSuffix} extends ${className}${mapperSuffix} { 14 | 15 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/mapper.txt: -------------------------------------------------------------------------------- 1 | package ${mapperPackage}; 2 | 3 | import ${doPackage}.${className}${doSuffix}; 4 | import ${queryPackage}.${className}${queryPrefix}; 5 | 6 | import java.util.List; 7 | 8 | import org.apache.ibatis.annotations.Mapper; 9 | import org.apache.ibatis.annotations.Param; 10 | import org.springframework.stereotype.Repository; 11 | /** 12 | * MyBatis Mapper for ${className}. 13 | */ 14 | @Mapper 15 | @Repository 16 | public interface ${className}${mapperSuffix} { 17 | /** 18 | * query count by query condition. 19 | */ 20 | int countBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 21 | 22 | /** 23 | * delete by query condition. 24 | */ 25 | int deleteBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 26 | 27 | /** 28 | * delete by primary key. 29 | */ 30 | int deleteByPrimaryKey(${className}${doSuffix} record); 31 | 32 | /** 33 | * insert selective. 34 | */ 35 | int insertSelective(${className}${doSuffix} record); 36 | 37 | /** 38 | * select by query condition. 39 | */ 40 | List<${className}${doSuffix}> selectBy${queryPrefix}(${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 41 | 42 | /** 43 | * select by primary key. 44 | */ 45 | ${className}${doSuffix} selectByPrimaryKey(Long id); 46 | 47 | /** 48 | * update by query condition selective. 49 | */ 50 | int updateBy${queryPrefix}Selective(@Param("record") ${className}${doSuffix} record, @Param("${queryPrefix.toLowerCase()}") ${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 51 | 52 | /** 53 | * update by query condition. 54 | */ 55 | int updateBy${queryPrefix}(@Param("record") ${className}${doSuffix} record, @Param("${queryPrefix.toLowerCase()}") ${className}${queryPrefix} ${queryPrefix.toLowerCase()}); 56 | 57 | /** 58 | * update by primary key selective. 59 | */ 60 | int updateByPrimaryKeySelective(${className}${doSuffix} record); 61 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/query.txt: -------------------------------------------------------------------------------- 1 | package ${queryPackage}; 2 | 3 | 4 | import ${mainPackage}.common.BaseCriteria; 5 | import ${mainPackage}.common.BaseQuery; 6 | import org.apache.commons.lang.builder.ReflectionToStringBuilder; 7 | import org.apache.commons.lang.builder.ToStringStyle; 8 | 9 | import java.io.Serializable; 10 | 11 | import java.util.Date; 12 | import java.util.List; 13 | 14 | public class ${className}${queryPrefix} extends BaseQuery implements Serializable { 15 | private static final long serialVersionUID = 1L; 16 | 17 | public ${className}${queryPrefix}() { 18 | super(); 19 | } 20 | 21 | public Criteria or() { 22 | Criteria criteria = createCriteriaInternal(); 23 | super.oredCriteria.add(criteria); 24 | return criteria; 25 | } 26 | 27 | public Criteria createCriteria() { 28 | Criteria criteria = createCriteriaInternal(); 29 | if (oredCriteria.size() == 0) { 30 | oredCriteria.add(criteria); 31 | } 32 | return criteria; 33 | } 34 | 35 | protected Criteria createCriteriaInternal() { 36 | Criteria criteria = new Criteria(); 37 | return criteria; 38 | } 39 | 40 | /** 41 | * This class corresponds to the database table ${tableName} 42 | */ 43 | #set($propPrimaryKey=$propPrimaryKey.substring(0, 1).toUpperCase() + $propPrimaryKey.substring(1)) 44 | protected abstract static class GeneratedCriteria extends BaseCriteria { 45 | 46 | protected GeneratedCriteria() { 47 | super(); 48 | } 49 | 50 | public Criteria and${propPrimaryKey}IsNull() { 51 | addCriterion("${primaryKey} is null"); 52 | return (Criteria) this; 53 | } 54 | 55 | public Criteria and${propPrimaryKey}IsNotNull() { 56 | addCriterion("${primaryKey} is not null"); 57 | return (Criteria) this; 58 | } 59 | 60 | public Criteria and${propPrimaryKey}EqualTo(Long value) { 61 | addCriterion("${primaryKey} =", value, "id"); 62 | return (Criteria) this; 63 | } 64 | 65 | public Criteria and${propPrimaryKey}NotEqualTo(Long value) { 66 | addCriterion("${primaryKey} <>", value, "id"); 67 | return (Criteria) this; 68 | } 69 | 70 | public Criteria an${propPrimaryKey}GreaterThan(Long value) { 71 | addCriterion("${primaryKey} >", value, "id"); 72 | return (Criteria) this; 73 | } 74 | 75 | public Criteria and${propPrimaryKey}GreaterThanOrEqualTo(Long value) { 76 | addCriterion("${primaryKey} >=", value, "id"); 77 | return (Criteria) this; 78 | } 79 | 80 | public Criteria and${propPrimaryKey}LessThan(Long value) { 81 | addCriterion("${primaryKey} <", value, "id"); 82 | return (Criteria) this; 83 | } 84 | 85 | public Criteria and${propPrimaryKey}LessThanOrEqualTo(Long value) { 86 | addCriterion("${primaryKey} <=", value, "id"); 87 | return (Criteria) this; 88 | } 89 | 90 | public Criteria and${propPrimaryKey}In(List values) { 91 | addCriterion("${primaryKey} in", values, "id"); 92 | return (Criteria) this; 93 | } 94 | 95 | public Criteria and${propPrimaryKey}NotIn(List values) { 96 | addCriterion("${primaryKey} not in", values, "id"); 97 | return (Criteria) this; 98 | } 99 | 100 | public Criteria and${propPrimaryKey}Between(Long value1, Long value2) { 101 | addCriterion("${primaryKey} between", value1, value2, "id"); 102 | return (Criteria) this; 103 | } 104 | 105 | public Criteria and${propPrimaryKey}NotBetween(Long value1, Long value2) { 106 | addCriterion("${primaryKey} not between", value1, value2, "id"); 107 | return (Criteria) this; 108 | } 109 | 110 | public Criteria andGmtCreateIsNull() { 111 | addCriterion("gmt_create is null"); 112 | return (Criteria) this; 113 | } 114 | 115 | public Criteria andGmtCreateIsNotNull() { 116 | addCriterion("gmt_create is not null"); 117 | return (Criteria) this; 118 | } 119 | 120 | public Criteria andGmtCreateEqualTo(Date value) { 121 | addCriterion("gmt_create =", value, "gmtCreate"); 122 | return (Criteria) this; 123 | } 124 | 125 | public Criteria andGmtCreateNotEqualTo(Date value) { 126 | addCriterion("gmt_create <>", value, "gmtCreate"); 127 | return (Criteria) this; 128 | } 129 | 130 | public Criteria andGmtCreateGreaterThan(Date value) { 131 | addCriterion("gmt_create >", value, "gmtCreate"); 132 | return (Criteria) this; 133 | } 134 | 135 | public Criteria andGmtCreateGreaterThanOrEqualTo(Date value) { 136 | addCriterion("gmt_create >=", value, "gmtCreate"); 137 | return (Criteria) this; 138 | } 139 | 140 | public Criteria andGmtCreateLessThan(Date value) { 141 | addCriterion("gmt_create <", value, "gmtCreate"); 142 | return (Criteria) this; 143 | } 144 | 145 | public Criteria andGmtCreateLessThanOrEqualTo(Date value) { 146 | addCriterion("gmt_create <=", value, "gmtCreate"); 147 | return (Criteria) this; 148 | } 149 | 150 | public Criteria andGmtCreateIn(List values) { 151 | addCriterion("gmt_create in", values, "gmtCreate"); 152 | return (Criteria) this; 153 | } 154 | 155 | public Criteria andGmtCreateNotIn(List values) { 156 | addCriterion("gmt_create not in", values, "gmtCreate"); 157 | return (Criteria) this; 158 | } 159 | 160 | public Criteria andGmtCreateBetween(Date value1, Date value2) { 161 | addCriterion("gmt_create between", value1, value2, "gmtCreate"); 162 | return (Criteria) this; 163 | } 164 | 165 | public Criteria andGmtCreateNotBetween(Date value1, Date value2) { 166 | addCriterion("gmt_create not between", value1, value2, "gmtCreate"); 167 | return (Criteria) this; 168 | } 169 | 170 | public Criteria andGmtModifiedIsNull() { 171 | addCriterion("gmt_modified is null"); 172 | return (Criteria) this; 173 | } 174 | 175 | public Criteria andGmtModifiedIsNotNull() { 176 | addCriterion("gmt_modified is not null"); 177 | return (Criteria) this; 178 | } 179 | 180 | public Criteria andGmtModifiedEqualTo(Date value) { 181 | addCriterion("gmt_modified =", value, "gmtModified"); 182 | return (Criteria) this; 183 | } 184 | 185 | public Criteria andGmtModifiedNotEqualTo(Date value) { 186 | addCriterion("gmt_modified <>", value, "gmtModified"); 187 | return (Criteria) this; 188 | } 189 | 190 | public Criteria andGmtModifiedGreaterThan(Date value) { 191 | addCriterion("gmt_modified >", value, "gmtModified"); 192 | return (Criteria) this; 193 | } 194 | 195 | public Criteria andGmtModifiedGreaterThanOrEqualTo(Date value) { 196 | addCriterion("gmt_modified >=", value, "gmtModified"); 197 | return (Criteria) this; 198 | } 199 | 200 | public Criteria andGmtModifiedLessThan(Date value) { 201 | addCriterion("gmt_modified <", value, "gmtModified"); 202 | return (Criteria) this; 203 | } 204 | 205 | public Criteria andGmtModifiedLessThanOrEqualTo(Date value) { 206 | addCriterion("gmt_modified <=", value, "gmtModified"); 207 | return (Criteria) this; 208 | } 209 | 210 | public Criteria andGmtModifiedIn(List values) { 211 | addCriterion("gmt_modified in", values, "gmtModified"); 212 | return (Criteria) this; 213 | } 214 | 215 | public Criteria andGmtModifiedNotIn(List values) { 216 | addCriterion("gmt_modified not in", values, "gmtModified"); 217 | return (Criteria) this; 218 | } 219 | 220 | public Criteria andGmtModifiedBetween(Date value1, Date value2) { 221 | addCriterion("gmt_modified between", value1, value2, "gmtModified"); 222 | return (Criteria) this; 223 | } 224 | 225 | public Criteria andGmtModifiedNotBetween(Date value1, Date value2) { 226 | addCriterion("gmt_modified not between", value1, value2, "gmtModified"); 227 | return (Criteria) this; 228 | } 229 | 230 | #foreach($item in $list) 231 | #set($colName=$item.propName.substring(0, 1).toUpperCase() + $item.propName.substring(1)) 232 | public Criteria and${colName}IsNull() { 233 | addCriterion("${item.columnName} is null"); 234 | return (Criteria) this; 235 | } 236 | 237 | public Criteria and${colName}IsNotNull() { 238 | addCriterion("${item.columnName} is not null"); 239 | return (Criteria) this; 240 | } 241 | 242 | public Criteria and${colName}EqualTo(${item.javaType} value) { 243 | addCriterion("${item.columnName} =", value, "${item.propName}"); 244 | return (Criteria) this; 245 | } 246 | 247 | public Criteria and${colName}NotEqualTo(${item.javaType} value) { 248 | addCriterion("${item.columnName} <>", value, "${item.propName}"); 249 | return (Criteria) this; 250 | } 251 | 252 | public Criteria and${colName}GreaterThan(${item.javaType} value) { 253 | addCriterion("${item.columnName} >", value, "${item.propName}"); 254 | return (Criteria) this; 255 | } 256 | 257 | public Criteria and${colName}GreaterThanOrEqualTo(${item.javaType} value) { 258 | addCriterion("${item.columnName} >=", value, "${item.propName}"); 259 | return (Criteria) this; 260 | } 261 | 262 | public Criteria and${colName}LessThan(${item.javaType} value) { 263 | addCriterion("${item.columnName} <", value, "${item.propName}"); 264 | return (Criteria) this; 265 | } 266 | 267 | public Criteria and${colName}LessThanOrEqualTo(${item.javaType} value) { 268 | addCriterion("${item.columnName} <=", value, "${item.propName}"); 269 | return (Criteria) this; 270 | } 271 | 272 | public Criteria and${colName}Like(${item.javaType} value) { 273 | addCriterion("${item.columnName} like", value, "${item.propName}"); 274 | return (Criteria) this; 275 | } 276 | 277 | public Criteria and${colName}NotLike(${item.javaType} value) { 278 | addCriterion("${item.columnName} not like", value, "${item.propName}"); 279 | return (Criteria) this; 280 | } 281 | 282 | public Criteria and${colName}In(List<${item.javaType}> values) { 283 | addCriterion("${item.columnName} in", values, "${item.propName}"); 284 | return (Criteria) this; 285 | } 286 | 287 | public Criteria and${colName}NotIn(List<${item.javaType}> values) { 288 | addCriterion("${item.columnName} not in", values, "${item.propName}"); 289 | return (Criteria) this; 290 | } 291 | 292 | public Criteria and${colName}Between(${item.javaType} value1, ${item.javaType} value2) { 293 | addCriterion("${item.columnName} between", value1, value2, "${item.propName}"); 294 | return (Criteria) this; 295 | } 296 | 297 | public Criteria and${colName}NotBetween(${item.javaType} value1, ${item.javaType} value2) { 298 | addCriterion("${item.columnName} not between", value1, value2, "${item.propName}"); 299 | return (Criteria) this; 300 | } 301 | 302 | #end 303 | } 304 | 305 | /** 306 | * This class corresponds to the database table ${tableName} 307 | */ 308 | public static class Criteria extends GeneratedCriteria{ 309 | protected Criteria() { 310 | super(); 311 | } 312 | } 313 | 314 | 315 | 316 | @Override 317 | public String toString(){ 318 | return ReflectionToStringBuilder.toString(this, ToStringStyle.DEFAULT_STYLE); 319 | } 320 | } -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/sqlmap-ext.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /elastic-generator/src/main/resources/template/sqlmap.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | #foreach($item in $list) 9 | 10 | #end 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | and ${criterion.condition} 21 | 22 | 23 | and ${criterion.condition} #{criterion.value} 24 | 25 | 26 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 27 | 28 | 29 | and ${criterion.condition} 30 | 31 | #{listItem} 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | and ${criterion.condition} 51 | 52 | 53 | and ${criterion.condition} #{criterion.value} 54 | 55 | 56 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 57 | 58 | 59 | and ${criterion.condition} 60 | 61 | #{listItem} 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | ${cols} 74 | 75 | 76 | 91 | 92 | 95 | 96 | 97 | delete from ${tableName} where ${primaryKey} = #{${propPrimaryKey},jdbcType=BIGINT} 98 | 99 | 100 | 101 | delete from ${tableName} 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | SELECT LAST_INSERT_ID() 110 | 111 | insert into ${tableName} 112 | 113 | gmt_create,gmt_modified, 114 | #foreach($item in $list) 115 | 116 | ${item.columnName}, 117 | 118 | #end 119 | 120 | 121 | now(),now(), 122 | #foreach($item in $list) 123 | 124 | #{${item.propName},jdbcType=${item.jdbcType}}, 125 | 126 | #end 127 | 128 | 129 | 130 | 136 | 137 | 138 | update ${tableName} 139 | 140 | gmt_modified=now(), 141 | #foreach($item in $list) 142 | 143 | ${item.columnName} = #{record.${item.propName},jdbcType=${item.jdbcType}}, 144 | 145 | #end 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | update ${tableName} set 154 | #foreach($item in $list) 155 | ${item.columnName} = #{record.${item.propName},jdbcType=${item.jdbcType}}, 156 | #end 157 | gmt_modified = now() 158 | 159 | 160 | 161 | 162 | 163 | 164 | update ${tableName} 165 | 166 | gmt_modified=now(), 167 | #foreach($item in $list) 168 | 169 | ${item.columnName} = #{${item.propName},jdbcType=${item.jdbcType}}, 170 | 171 | #end 172 | 173 | where ${primaryKey} = #{${propPrimaryKey},jdbcType=BIGINT} 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /elastic-search/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elastic-demo 7 | com.leno 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | elastic-search 13 | 14 | 15 | 16 | 17 | 18 | org.elasticsearch 19 | elasticsearch 20 | 21 | 22 | 23 | org.elasticsearch.client 24 | elasticsearch-rest-high-level-client 25 | 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter 35 | 36 | 37 | 38 | com.alibaba 39 | fastjson 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/common/ESClientDecorator.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.common; 2 | 3 | import org.apache.http.HttpHost; 4 | import org.elasticsearch.client.RestClient; 5 | import org.elasticsearch.client.RestHighLevelClient; 6 | import org.springframework.beans.factory.DisposableBean; 7 | import org.springframework.beans.factory.InitializingBean; 8 | 9 | /** 10 | *

ES客户端工程类

11 | * 12 | * @author: XianGuo 13 | * @date: 2018年02月07日 14 | */ 15 | public class ESClientDecorator implements InitializingBean, DisposableBean { 16 | 17 | private RestHighLevelClient restHighLevelClient; 18 | 19 | private HttpHost httpHost; 20 | 21 | public ESClientDecorator(HttpHost httpHost) { 22 | this.httpHost = httpHost; 23 | } 24 | 25 | public RestHighLevelClient getRestHighLevelClient() { 26 | if (restHighLevelClient == null) { 27 | restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost)); 28 | } 29 | return restHighLevelClient; 30 | } 31 | 32 | 33 | @Override 34 | public void destroy() throws Exception { 35 | restHighLevelClient.close(); 36 | } 37 | 38 | @Override 39 | public void afterPropertiesSet() throws Exception { 40 | restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/common/EsSearchClient.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.common; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.elasticsearch.action.search.SearchRequest; 7 | import org.elasticsearch.action.search.SearchResponse; 8 | import org.elasticsearch.client.RestHighLevelClient; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | *

用户搜索客户端

17 | * 18 | * @author: XianGuo 19 | * @date: 2018年01月31日 20 | */ 21 | @Service("searchClient") 22 | @Slf4j 23 | public class EsSearchClient implements SearchClient { 24 | 25 | @Autowired 26 | private RestHighLevelClient client; 27 | 28 | @Override 29 | public List search(SearchRequest request) { 30 | try { 31 | SearchResponse response = client.search(request); 32 | if (response.getHits() == null) { 33 | return null; 34 | } 35 | List list = new ArrayList<>(); 36 | response.getHits().forEach(item -> list.add(JSON.parseObject(item.getSourceAsString()))); 37 | return list; 38 | } catch (Exception e) { 39 | e.printStackTrace(); 40 | } 41 | return null; 42 | } 43 | 44 | @Override 45 | public List search(SearchRequest request, Class tClass) { 46 | List searchResponse = this.search(request); 47 | if (searchResponse == null) { 48 | return null; 49 | } 50 | List list = new ArrayList<>(searchResponse.size()); 51 | searchResponse.forEach(item -> list.add(JSON.parseObject(JSON.toJSONString(item), tClass))); 52 | return list; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/common/IESEntity.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.common; 2 | 3 | /** 4 | *

搜索实体类

5 | * 6 | * @author: XianGuo 7 | * @date: 2018年01月31日 8 | */ 9 | public interface IESEntity { 10 | 11 | String getIndex(); 12 | 13 | String getId(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/common/LuSearchClient.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.common; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import org.elasticsearch.action.search.SearchRequest; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | *

TODO

11 | * 12 | * @author: XianGuo 13 | * @date: 2018年02月10日 14 | */ 15 | @Service("luSearchClient") 16 | public class LuSearchClient implements SearchClient { 17 | @Override 18 | public List search(SearchRequest request) { 19 | return null; 20 | } 21 | 22 | @Override 23 | public List search(SearchRequest request, Class tClass) { 24 | return null; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/common/SearchClient.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.common; 2 | 3 | 4 | import com.alibaba.fastjson.JSONObject; 5 | import org.elasticsearch.action.search.SearchRequest; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | *

搜索相关接口

11 | * 12 | * @author: XianGuo 13 | * @date: 2018年01月31日 14 | */ 15 | public interface SearchClient { 16 | 17 | /** 18 | * 搜索结果 19 | */ 20 | List search(SearchRequest request); 21 | 22 | /** 23 | * 搜索 24 | */ 25 | List search(SearchRequest request, Class tClass); 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/common/SimpleESEntity.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.common; 2 | 3 | /** 4 | *

TODO

5 | * 6 | * @author: XianGuo 7 | * @date: 2018年01月31日 8 | */ 9 | public class SimpleESEntity implements IESEntity { 10 | 11 | /** 12 | * 索引 13 | */ 14 | private String index; 15 | 16 | /** 17 | * id 18 | */ 19 | private String id; 20 | 21 | public SimpleESEntity() { 22 | } 23 | 24 | public SimpleESEntity(String index) { 25 | this.index = index; 26 | } 27 | 28 | public SimpleESEntity(String index, String id) { 29 | this.index = index; 30 | this.id = id; 31 | } 32 | 33 | @Override 34 | public String getIndex() { 35 | return this.index; 36 | } 37 | 38 | @Override 39 | public String getId() { 40 | return this.id; 41 | } 42 | 43 | public void setIndex(String index) { 44 | this.index = index; 45 | } 46 | 47 | public void setId(String id) { 48 | this.id = id; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/config/ElasticsConfig.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.config; 2 | 3 | import com.leno.search.common.ESClientDecorator; 4 | import org.apache.http.HttpHost; 5 | import org.elasticsearch.client.RestHighLevelClient; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.context.annotation.Scope; 11 | 12 | /** 13 | *

搜索连接客户端

14 | * 15 | * @author: XianGuo 16 | * @date: 2018年01月23日 17 | */ 18 | @Configuration 19 | @EnableConfigurationProperties(ElasticsProperties.class) 20 | public class ElasticsConfig { 21 | 22 | 23 | @Autowired 24 | private ElasticsProperties elasticsProperties; 25 | 26 | /** 27 | * 初始化 28 | */ 29 | @Bean 30 | public RestHighLevelClient restHighLevelClient() { 31 | return getEsClientDecorator().getRestHighLevelClient(); 32 | } 33 | 34 | @Bean 35 | @Scope("singleton") 36 | public ESClientDecorator getEsClientDecorator() { 37 | return new ESClientDecorator(new HttpHost(elasticsProperties.getClusterNodes(), elasticsProperties.getPort())); 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/config/ElasticsProperties.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.config; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | /** 7 | *

elastics配置

8 | * 9 | * @author: XianGuo 10 | * @date: 2018年01月23日 11 | */ 12 | @ConfigurationProperties(prefix = "spring.data.elasticsearch") 13 | @Data 14 | public class ElasticsProperties { 15 | 16 | /** 17 | * 名称 18 | */ 19 | private String clusterName; 20 | 21 | /** 22 | * 节点 23 | */ 24 | private String clusterNodes; 25 | 26 | /** 27 | * 端口号 28 | */ 29 | private int port = 9200; 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/entity/BaseEntity.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | *

基础实体

7 | * 8 | * @author: XianGuo 9 | * @date: 2018年02月09日 10 | */ 11 | public class BaseEntity implements Serializable { 12 | private static final long serialVersionUID = 8051510411401307163L; 13 | } 14 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/entity/UserESEntity.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.entity; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.Date; 6 | 7 | /** 8 | *

用户搜索结果

9 | * 10 | * @author: XianGuo 11 | * @date: 2018年01月31日 12 | */ 13 | @Data 14 | public class UserESEntity extends BaseEntity { 15 | 16 | 17 | private static final long serialVersionUID = -750071967756810272L; 18 | /** 19 | * 主键 20 | */ 21 | private Long userId; 22 | 23 | /** 24 | * 用户名 25 | */ 26 | private String userName; 27 | 28 | /** 29 | * 登录名 30 | */ 31 | private String loginName; 32 | 33 | /** 34 | * 密码 35 | */ 36 | private String loginPwd; 37 | 38 | /** 39 | * 状态 40 | */ 41 | private Integer status; 42 | 43 | private Date gmtCreate; 44 | 45 | private Date gmtModified; 46 | } 47 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/entity/enums/ESQueryTypeEnum.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.entity.enums; 2 | 3 | /** 4 | *

es查询类型

5 | * 6 | * @author: XianGuo 7 | * @date: 2018年02月09日 8 | */ 9 | public enum ESQueryTypeEnum { 10 | MATCH(1, "match", "全文匹配"), MATCH_PHRASE(2, "match_phrase", "紧邻搜索"), TERM(3, "term", "精确匹配"); 11 | 12 | private Integer code; 13 | private String englishCode; 14 | private String desc; 15 | 16 | ESQueryTypeEnum(Integer code, String englishCode, String desc) { 17 | this.code = code; 18 | this.desc = desc; 19 | this.englishCode = englishCode; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/entity/request/CommonSearchRequest.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.entity.request; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | *

封装的搜索请求

7 | * 8 | * @author: XianGuo 9 | * @date: 2018年02月09日 10 | */ 11 | public class CommonSearchRequest { 12 | 13 | private List searchWords; 14 | 15 | /** 16 | * 添加搜索关键字 17 | * @param keyword 18 | * @param esQueryType 19 | */ 20 | private void addSearchWord(String keyword,int esQueryType){ 21 | SearchWord searchWord = new SearchWord(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/entity/request/SearchWord.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.entity.request; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | *

搜索词

7 | * 8 | * @author: XianGuo 9 | * @date: 2018年02月09日 10 | */ 11 | @Data 12 | public class SearchWord { 13 | 14 | /** 15 | * 搜索关键字 16 | */ 17 | private String keyword; 18 | 19 | /** 20 | * 查询类型 21 | */ 22 | private int queryType; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/repository/UserSearchRepository.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.repository; 2 | 3 | import com.leno.search.entity.UserESEntity; 4 | import com.leno.search.entity.request.CommonSearchRequest; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | *

用户搜索数据提供

10 | * 11 | * @author: XianGuo 12 | * @date: 2018年02月09日 13 | */ 14 | public interface UserSearchRepository { 15 | 16 | 17 | /** 18 | * 搜索 19 | */ 20 | List searchUserByRequest(CommonSearchRequest request); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /elastic-search/src/main/java/com/leno/search/repository/impl/UserSearchRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.leno.search.repository.impl; 2 | 3 | import com.leno.search.entity.UserESEntity; 4 | import com.leno.search.entity.request.CommonSearchRequest; 5 | import com.leno.search.repository.UserSearchRepository; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | *

搜索数据提供实现

12 | * 13 | * @author: XianGuo 14 | * @date: 2018年02月09日 15 | */ 16 | @Service 17 | public class UserSearchRepositoryImpl implements UserSearchRepository { 18 | 19 | 20 | @Override 21 | public List searchUserByRequest(CommonSearchRequest request) { 22 | return null; 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /elastic-search/src/main/resources/conf/jdbc.conf: -------------------------------------------------------------------------------- 1 | input { 2 | stdin { 3 | } 4 | jdbc { 5 | # mysql jdbc connection string to our backup databse 后面的test对应mysql中的test数据库 6 | jdbc_connection_string => "jdbc:mysql://localhost:3306/moyu_db?useUnicode=true&characterEncoding=utf-8&useSSL=true" 7 | # the user we wish to excute our statement as 8 | jdbc_user => "root" 9 | jdbc_password => alixian 10 | # the path to our downloaded jdbc driver 11 | jdbc_driver_library => "~/.m2/repository/mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar" 12 | # the name of the driver class for mysql 13 | jdbc_driver_class => "com.mysql.jdbc.Driver" 14 | jdbc_paging_enabled => "true" 15 | jdbc_page_size => "50000" 16 | #以下对应着要执行的sql的绝对路径。 17 | statement_filepath => "/Users/xianguo1/leno/elastic-demo/elastic-search/src/main/resources/conf/user.sql" 18 | #定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新(测试结果,不同的话请留言指出) 19 | schedule => "* * * * *" 20 | } 21 | } 22 | 23 | filter { 24 | json { 25 | source => "message" 26 | remove_field => ["message"] 27 | } 28 | } 29 | 30 | output { 31 | elasticsearch { 32 | #ESIP地址与端口 33 | hosts => "localhost" 34 | #ES索引名称(自己定义的) 35 | index => "moyu_index2" 36 | #自增ID编号 37 | document_id => "%{user_id}" 38 | } 39 | stdout { 40 | #以JSON格式输出 41 | codec => json_lines 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /elastic-search/src/main/resources/conf/user.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM user 2 | WHERE gmt_modified>date_add(:sql_last_value,INTERVAL 8 HOUR) -------------------------------------------------------------------------------- /moyu_index_sync.sh: -------------------------------------------------------------------------------- 1 | # !bin/bash 2 | pwd 3 | logstash -f elastic-search/src/main/resources/conf/jdbc.conf & -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.leno 8 | elastic-demo 9 | 1.0-SNAPSHOT 10 | pom 11 | 12 | 13 | UTF-8 14 | UTF-8 15 | 1.8 16 | 6.1.2 17 | 18 | 19 | elastic-core 20 | elastic-generator 21 | elastic-dal 22 | elastic-search 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-parent 27 | 1.5.9.RELEASE 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | com.leno 36 | elastic-dal 37 | ${project.version} 38 | 39 | 40 | 41 | com.leno 42 | elastic-search 43 | ${project.version} 44 | 45 | 46 | 47 | org.elasticsearch 48 | elasticsearch 49 | ${elasticsearch.version} 50 | 51 | 52 | 53 | org.elasticsearch.client 54 | transport 55 | ${elasticsearch.version} 56 | 57 | 58 | 59 | org.elasticsearch.client 60 | elasticsearch-rest-high-level-client 61 | ${elasticsearch.version} 62 | 63 | 64 | 65 | org.mybatis.spring.boot 66 | mybatis-spring-boot-starter 67 | 1.2.0 68 | 69 | 70 | 71 | com.alibaba 72 | fastjson 73 | 1.2.37 74 | 75 | 76 | 77 | mysql 78 | mysql-connector-java 79 | 5.1.44 80 | 81 | 82 | 83 | org.apache.commons 84 | commons-lang3 85 | 3.5 86 | 87 | 88 | 89 | commons-collections 90 | commons-collections 91 | 3.2.2 92 | 93 | 94 | 95 | org.apache.velocity 96 | velocity 97 | 1.7 98 | 99 | 100 | 101 | commons-io 102 | commons-io 103 | 2.5 104 | 105 | 106 | 107 | com.google.guava 108 | guava 109 | 22.0 110 | 111 | 112 | 113 | commons-lang 114 | commons-lang 115 | 2.6 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | maven-public 124 | maven-public 125 | http://maven.aliyun.com/nexus/content/groups/public 126 | 127 | 128 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | mvn clean package -Dmaven.test.skip -U 3 | java -jar elastics-core/target/elastics.jar --spring.profiles.active=local --spring.dubbo.protocol.port=20881 --server.port=8080 & --------------------------------------------------------------------------------