From c7bc8e415fa6cf5e3dec7059e900008b2b2bc1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Fri, 14 Feb 2020 13:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90=20Lombok=20+=20MybatisPlus?= =?UTF-8?q?=203.3.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi/pom.xml | 41 +++- .../main/java/com/ruoyi/RuoYiApplication.java | 11 +- .../ruoyi/framework/config/MyBatisConfig.java | 210 +++++++++--------- .../framework/config/MybatisPlusConfig.java | 38 ++++ ruoyi/src/main/resources/application-dev.yml | 77 +++++++ ...ication-druid.yml => application-prop.yml} | 22 +- ruoyi/src/main/resources/application-test.yml | 77 +++++++ ruoyi/src/main/resources/application.yml | 69 +++--- ruoyi/src/main/resources/banner.txt | 24 +- 9 files changed, 394 insertions(+), 175 deletions(-) create mode 100644 ruoyi/src/main/java/com/ruoyi/framework/config/MybatisPlusConfig.java create mode 100644 ruoyi/src/main/resources/application-dev.yml rename ruoyi/src/main/resources/{application-druid.yml => application-prop.yml} (76%) create mode 100644 ruoyi/src/main/resources/application-test.yml diff --git a/ruoyi/pom.xml b/ruoyi/pom.xml index b317c40b..a3ebef28 100644 --- a/ruoyi/pom.xml +++ b/ruoyi/pom.xml @@ -35,6 +35,8 @@ 3.17 3.9.1 1.7 + 3.3.0 + 5.0.6 @@ -50,7 +52,6 @@ org.springframework.boot - --> @@ -104,18 +105,24 @@ runtime - - - org.mybatis.spring.boot - mybatis-spring-boot-starter - ${mybatis.spring.boot.starter.version} - + + + + + + com.github.pagehelper pagehelper-spring-boot-starter ${pagehelper.spring.boot.starter.version} + + + org.mybatis + mybatis + + @@ -238,7 +245,25 @@ velocity ${velocity.version} - + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + com.baomidou + mybatis-plus-extension + ${mybatis-plus.version} + + + cn.hutool + hutool-all + ${hutool.version} + + + org.projectlombok + lombok + diff --git a/ruoyi/src/main/java/com/ruoyi/RuoYiApplication.java b/ruoyi/src/main/java/com/ruoyi/RuoYiApplication.java index 32eb6f1d..32ae45f7 100644 --- a/ruoyi/src/main/java/com/ruoyi/RuoYiApplication.java +++ b/ruoyi/src/main/java/com/ruoyi/RuoYiApplication.java @@ -16,15 +16,6 @@ public class RuoYiApplication { // System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication.run(RuoYiApplication.class, args); - System.out.println("(♥◠‿◠)ノ゙ 若依启动成功 ლ(´ڡ`ლ)゙ \n" + - " .-------. ____ __ \n" + - " | _ _ \\ \\ \\ / / \n" + - " | ( ' ) | \\ _. / ' \n" + - " |(_ o _) / _( )_ .' \n" + - " | (_,_).' __ ___(_ o _)' \n" + - " | |\\ \\ | || |(_,_)' \n" + - " | | \\ `' /| `-' / \n" + - " | | \\ / \\ / \n" + - " ''-' `'-' `-..-' "); + System.out.println("(♥◠‿◠)ノ゙ 若依启动成功 ლ(´ڡ`ლ)゙"); } } diff --git a/ruoyi/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java b/ruoyi/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java index 7ecfd9d3..fc9841fd 100644 --- a/ruoyi/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java +++ b/ruoyi/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java @@ -1,105 +1,105 @@ -package com.ruoyi.framework.config; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import javax.sql.DataSource; -import org.apache.ibatis.io.VFS; -import org.apache.ibatis.session.SqlSessionFactory; -import org.mybatis.spring.SqlSessionFactoryBean; -import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.core.type.classreading.CachingMetadataReaderFactory; -import org.springframework.core.type.classreading.MetadataReader; -import org.springframework.core.type.classreading.MetadataReaderFactory; -import org.springframework.util.ClassUtils; - -/** - * Mybatis支持*匹配扫描包 - * - * @author ruoyi - */ -@Configuration -public class MyBatisConfig -{ - @Autowired - private Environment env; - - static final String DEFAULT_RESOURCE_PATTERN = "**/*.class"; - - public static String setTypeAliasesPackage(String typeAliasesPackage) - { - ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); - MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver); - List allResult = new ArrayList(); - try - { - for (String aliasesPackage : typeAliasesPackage.split(",")) - { - List result = new ArrayList(); - aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX - + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN; - Resource[] resources = resolver.getResources(aliasesPackage); - if (resources != null && resources.length > 0) - { - MetadataReader metadataReader = null; - for (Resource resource : resources) - { - if (resource.isReadable()) - { - metadataReader = metadataReaderFactory.getMetadataReader(resource); - try - { - result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName()); - } - catch (ClassNotFoundException e) - { - e.printStackTrace(); - } - } - } - } - if (result.size() > 0) - { - HashSet hashResult = new HashSet(result); - allResult.addAll(hashResult); - } - } - if (allResult.size() > 0) - { - typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0])); - } - else - { - throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包"); - } - } - catch (IOException e) - { - e.printStackTrace(); - } - return typeAliasesPackage; - } - - @Bean - public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception - { - String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage"); - String mapperLocations = env.getProperty("mybatis.mapperLocations"); - typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage); - VFS.addImplClass(SpringBootVFS.class); - - final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); - sessionFactory.setDataSource(dataSource); - sessionFactory.setTypeAliasesPackage(typeAliasesPackage); - sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); - return sessionFactory.getObject(); - } -} \ No newline at end of file +//package com.ruoyi.framework.config; +// +//import java.io.IOException; +//import java.util.ArrayList; +//import java.util.HashSet; +//import java.util.List; +//import javax.sql.DataSource; +//import org.apache.ibatis.io.VFS; +//import org.apache.ibatis.session.SqlSessionFactory; +//import org.mybatis.spring.SqlSessionFactoryBean; +//import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.core.env.Environment; +//import org.springframework.core.io.Resource; +//import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +//import org.springframework.core.io.support.ResourcePatternResolver; +//import org.springframework.core.type.classreading.CachingMetadataReaderFactory; +//import org.springframework.core.type.classreading.MetadataReader; +//import org.springframework.core.type.classreading.MetadataReaderFactory; +//import org.springframework.util.ClassUtils; +// +///** +// * Mybatis支持*匹配扫描包 +// * +// * @author ruoyi +// */ +//@Configuration +//public class MyBatisConfig +//{ +// @Autowired +// private Environment env; +// +// static final String DEFAULT_RESOURCE_PATTERN = "**/*.class"; +// +// public static String setTypeAliasesPackage(String typeAliasesPackage) +// { +// ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); +// MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver); +// List allResult = new ArrayList(); +// try +// { +// for (String aliasesPackage : typeAliasesPackage.split(",")) +// { +// List result = new ArrayList(); +// aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +// + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN; +// Resource[] resources = resolver.getResources(aliasesPackage); +// if (resources != null && resources.length > 0) +// { +// MetadataReader metadataReader = null; +// for (Resource resource : resources) +// { +// if (resource.isReadable()) +// { +// metadataReader = metadataReaderFactory.getMetadataReader(resource); +// try +// { +// result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName()); +// } +// catch (ClassNotFoundException e) +// { +// e.printStackTrace(); +// } +// } +// } +// } +// if (result.size() > 0) +// { +// HashSet hashResult = new HashSet(result); +// allResult.addAll(hashResult); +// } +// } +// if (allResult.size() > 0) +// { +// typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0])); +// } +// else +// { +// throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包"); +// } +// } +// catch (IOException e) +// { +// e.printStackTrace(); +// } +// return typeAliasesPackage; +// } +// +// @Bean +// public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception +// { +// String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage"); +// String mapperLocations = env.getProperty("mybatis.mapperLocations"); +// typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage); +// VFS.addImplClass(SpringBootVFS.class); +// +// final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); +// sessionFactory.setDataSource(dataSource); +// sessionFactory.setTypeAliasesPackage(typeAliasesPackage); +// sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); +// return sessionFactory.getObject(); +// } +//} \ No newline at end of file diff --git a/ruoyi/src/main/java/com/ruoyi/framework/config/MybatisPlusConfig.java b/ruoyi/src/main/java/com/ruoyi/framework/config/MybatisPlusConfig.java new file mode 100644 index 00000000..5c56edd7 --- /dev/null +++ b/ruoyi/src/main/java/com/ruoyi/framework/config/MybatisPlusConfig.java @@ -0,0 +1,38 @@ +package com.ruoyi.framework.config; + +import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; +import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@EnableTransactionManagement(proxyTargetClass = true) +@Configuration +public class MybatisPlusConfig { + + /** + * 分页插件,自动识别数据库类型 + */ + @Bean + public PaginationInterceptor paginationInterceptor() { + return new PaginationInterceptor(); + } + + /** + * 乐观锁插件 + */ + @Bean + public OptimisticLockerInterceptor optimisticLockerInterceptor() { + return new OptimisticLockerInterceptor(); + } + + /** + * 如果是对全表的删除或更新操作,就会终止该操作 + */ + @Bean + public SqlExplainInterceptor sqlExplainInterceptor() { + return new SqlExplainInterceptor(); + } + +} diff --git a/ruoyi/src/main/resources/application-dev.yml b/ruoyi/src/main/resources/application-dev.yml new file mode 100644 index 00000000..29bed646 --- /dev/null +++ b/ruoyi/src/main/resources/application-dev.yml @@ -0,0 +1,77 @@ +# 数据源配置 +spring: + datasource: + type: com.alibaba.druid.pool.DruidDataSource + driverClassName: com.mysql.cj.jdbc.Driver + druid: + # 主库数据源 + master: + url: jdbc:mysql://192.168.0.222:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: root + password: root + # 从库数据源 + slave: + # 从数据源开关/默认关闭 + enabled: false + url: + username: + password: + # 初始连接数 + initialSize: 5 + # 最小连接池数量 + minIdle: 10 + # 最大连接池数量 + maxActive: 20 + # 配置获取连接等待超时的时间 + maxWait: 60000 + # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 + timeBetweenEvictionRunsMillis: 60000 + # 配置一个连接在池中最小生存的时间,单位是毫秒 + minEvictableIdleTimeMillis: 300000 + # 配置一个连接在池中最大生存的时间,单位是毫秒 + maxEvictableIdleTimeMillis: 900000 + # 配置检测连接是否有效 + validationQuery: SELECT 1 FROM DUAL + testWhileIdle: true + testOnBorrow: false + testOnReturn: false + webStatFilter: + enabled: true + statViewServlet: + enabled: true + # 设置白名单,不填则允许所有访问 + allow: + url-pattern: /druid/* + # 控制台管理用户名和密码 + login-username: + login-password: + filter: + stat: + enabled: true + # 慢SQL记录 + log-slow-sql: true + slow-sql-millis: 1000 + merge-sql: true + wall: + config: + multi-statement-allow: true + # redis 配置 + redis: + # 地址 + host: 192.168.0.222 + # 端口,默认为6379 + port: 6379 + # 密码 + password: + # 连接超时时间 + timeout: 10s + lettuce: + pool: + # 连接池中的最小空闲连接 + min-idle: 0 + # 连接池中的最大空闲连接 + max-idle: 8 + # 连接池的最大数据库连接数 + max-active: 8 + # #连接池最大阻塞等待时间(使用负值表示没有限制) + max-wait: -1ms \ No newline at end of file diff --git a/ruoyi/src/main/resources/application-druid.yml b/ruoyi/src/main/resources/application-prop.yml similarity index 76% rename from ruoyi/src/main/resources/application-druid.yml rename to ruoyi/src/main/resources/application-prop.yml index 13e1a5c3..cc840f96 100644 --- a/ruoyi/src/main/resources/application-druid.yml +++ b/ruoyi/src/main/resources/application-prop.yml @@ -54,4 +54,24 @@ spring: merge-sql: true wall: config: - multi-statement-allow: true \ No newline at end of file + multi-statement-allow: true + # redis 配置 + redis: + # 地址 + host: localhost + # 端口,默认为6379 + port: 6379 + # 密码 + password: + # 连接超时时间 + timeout: 10s + lettuce: + pool: + # 连接池中的最小空闲连接 + min-idle: 0 + # 连接池中的最大空闲连接 + max-idle: 8 + # 连接池的最大数据库连接数 + max-active: 8 + # #连接池最大阻塞等待时间(使用负值表示没有限制) + max-wait: -1ms \ No newline at end of file diff --git a/ruoyi/src/main/resources/application-test.yml b/ruoyi/src/main/resources/application-test.yml new file mode 100644 index 00000000..cc840f96 --- /dev/null +++ b/ruoyi/src/main/resources/application-test.yml @@ -0,0 +1,77 @@ +# 数据源配置 +spring: + datasource: + type: com.alibaba.druid.pool.DruidDataSource + driverClassName: com.mysql.cj.jdbc.Driver + druid: + # 主库数据源 + master: + url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: root + password: password + # 从库数据源 + slave: + # 从数据源开关/默认关闭 + enabled: false + url: + username: + password: + # 初始连接数 + initialSize: 5 + # 最小连接池数量 + minIdle: 10 + # 最大连接池数量 + maxActive: 20 + # 配置获取连接等待超时的时间 + maxWait: 60000 + # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 + timeBetweenEvictionRunsMillis: 60000 + # 配置一个连接在池中最小生存的时间,单位是毫秒 + minEvictableIdleTimeMillis: 300000 + # 配置一个连接在池中最大生存的时间,单位是毫秒 + maxEvictableIdleTimeMillis: 900000 + # 配置检测连接是否有效 + validationQuery: SELECT 1 FROM DUAL + testWhileIdle: true + testOnBorrow: false + testOnReturn: false + webStatFilter: + enabled: true + statViewServlet: + enabled: true + # 设置白名单,不填则允许所有访问 + allow: + url-pattern: /druid/* + # 控制台管理用户名和密码 + login-username: + login-password: + filter: + stat: + enabled: true + # 慢SQL记录 + log-slow-sql: true + slow-sql-millis: 1000 + merge-sql: true + wall: + config: + multi-statement-allow: true + # redis 配置 + redis: + # 地址 + host: localhost + # 端口,默认为6379 + port: 6379 + # 密码 + password: + # 连接超时时间 + timeout: 10s + lettuce: + pool: + # 连接池中的最小空闲连接 + min-idle: 0 + # 连接池中的最大空闲连接 + max-idle: 8 + # 连接池的最大数据库连接数 + max-active: 8 + # #连接池最大阻塞等待时间(使用负值表示没有限制) + max-wait: -1ms \ No newline at end of file diff --git a/ruoyi/src/main/resources/application.yml b/ruoyi/src/main/resources/application.yml index 4ced5520..fbabd995 100644 --- a/ruoyi/src/main/resources/application.yml +++ b/ruoyi/src/main/resources/application.yml @@ -42,7 +42,7 @@ spring: # 国际化资源文件路径 basename: i18n/messages profiles: - active: druid + active: dev # 文件上传 servlet: multipart: @@ -55,26 +55,6 @@ spring: restart: # 热部署开关 enabled: true - # redis 配置 - redis: - # 地址 - host: localhost - # 端口,默认为6379 - port: 6379 - # 密码 - password: - # 连接超时时间 - timeout: 10s - lettuce: - pool: - # 连接池中的最小空闲连接 - min-idle: 0 - # 连接池中的最大空闲连接 - max-idle: 8 - # 连接池的最大数据库连接数 - max-active: 8 - # #连接池最大阻塞等待时间(使用负值表示没有限制) - max-wait: -1ms # token配置 token: @@ -85,14 +65,47 @@ token: # 令牌有效期(默认30分钟) expireTime: 30 +## MyBatis配置 +#mybatis: +# # 搜索指定包别名 +# typeAliasesPackage: com.ruoyi.project.**.domain +# # 配置mapper的扫描,找到所有的mapper.xml映射文件 +# mapperLocations: classpath*:mybatis/**/*Mapper.xml +# # 加载全局的配置文件 +# configLocation: classpath:mybatis/mybatis-config.xml + # MyBatis配置 -mybatis: - # 搜索指定包别名 - typeAliasesPackage: com.ruoyi.project.**.domain - # 配置mapper的扫描,找到所有的mapper.xml映射文件 - mapperLocations: classpath*:mybatis/**/*Mapper.xml - # 加载全局的配置文件 - configLocation: classpath:mybatis/mybatis-config.xml +mybatis-plus: + mapper-locations: classpath*:mybatis/**/*Mapper.xml + #实体扫描,多个package用逗号或者分号分隔 + typeAliasesPackage: com.ruoyi.project + configuration: + map-underscore-to-camel-case: true + cache-enabled: true + global-config: + banner: false + #刷新mapper 调试神器 + refresh: true + db-config: + #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID"; + id-type: auto + #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断" + field-strategy: not_empty + #驼峰下划线转换 + db-column-underline: true + #数据库大写下划线转换 + #capital-mode: true + #序列接口实现类配置 + #key-generator: com.baomidou.springboot.xxx + #逻辑删除配置 + logic-delete-value: 1 + logic-not-delete-value: 0 + #数据库类型 + db-type: mysql + #自定义SQL注入器 + #sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector + #自定义填充策略接口实现 + #meta-object-handler: com.baomidou.springboot.xxx # PageHelper分页插件 pagehelper: diff --git a/ruoyi/src/main/resources/banner.txt b/ruoyi/src/main/resources/banner.txt index 0931cb84..fee1ce3b 100644 --- a/ruoyi/src/main/resources/banner.txt +++ b/ruoyi/src/main/resources/banner.txt @@ -1,24 +1,2 @@ Application Version: ${ruoyi.version} -Spring Boot Version: ${spring-boot.version} -//////////////////////////////////////////////////////////////////// -// _ooOoo_ // -// o8888888o // -// 88" . "88 // -// (| ^_^ |) // -// O\ = /O // -// ____/`---'\____ // -// .' \\| |// `. // -// / \\||| : |||// \ // -// / _||||| -:- |||||- \ // -// | | \\\ - /// | | // -// | \_| ''\---/'' | | // -// \ .-\__ `-` ___/-. / // -// ___`. .' /--.--\ `. . ___ // -// ."" '< `.___\_<|>_/___.' >'"". // -// | | : `- \`.;`\ _ /`;.`/ - ` : | | // -// \ \ `-. \_ __\ /__ _/ .-` / / // -// ========`-.____`-.___\_____/___.-`____.-'======== // -// `=---=' // -// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // -// 佛祖保佑 永不宕机 永无BUG // -//////////////////////////////////////////////////////////////////// \ No newline at end of file +Spring Boot Version: ${spring-boot.version} \ No newline at end of file