diff --git a/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java b/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java index 6eff47b..2b1c77e 100644 --- a/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java +++ b/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java @@ -30,10 +30,4 @@ public class Knife4jConfig { public GroupedOpenApi groupedOpenApi() { return GroupedOpenApi.builder().group("web前台接口管理").pathsToMatch("/api/**").build(); } - - // 管理员相关分类接口 - @Bean - public GroupedOpenApi groupedOpenAdminApi() { - return GroupedOpenApi.builder().group("admin管理员接口请求").pathsToMatch("/admin/**").build(); - } } diff --git a/common/service-utils/src/main/java/cn/bunny/common/service/config/MyBatisPlusFieldConfig.java b/common/service-utils/src/main/java/cn/bunny/common/service/config/MyBatisPlusFieldConfig.java deleted file mode 100644 index d32b125..0000000 --- a/common/service-utils/src/main/java/cn/bunny/common/service/config/MyBatisPlusFieldConfig.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.bunny.common.service.config; - -import cn.bunny.common.service.context.BaseContext; -import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; -import org.apache.ibatis.reflection.MetaObject; -import org.springframework.stereotype.Component; - -import java.util.Date; - -/** - * 配置MP在修改和新增时的操作 - */ -@Component -public class MyBatisPlusFieldConfig implements MetaObjectHandler { - - /** - * 使用mp做添加操作时候,这个方法执行 - */ - @Override - public void insertFill(MetaObject metaObject) { - // 设置属性值 - this.setFieldValByName("createTime", new Date(), metaObject); - this.setFieldValByName("updateTime", new Date(), metaObject); - this.setFieldValByName("deleteStatus", 1, metaObject); - if (BaseContext.getUsername() != null) { - this.setFieldValByName("createBy", BaseContext.getUsername(), metaObject); - this.setFieldValByName("updateBy", BaseContext.getUsername(), metaObject); - } - } - - /** - * 使用mp做修改操作时候,这个方法执行 - */ - @Override - public void updateFill(MetaObject metaObject) { - this.setFieldValByName("updateTime", new Date(), metaObject); - this.setFieldValByName("updateBy", BaseContext.getUsername(), metaObject); - } -} diff --git a/service/pom.xml b/service/pom.xml index 3a46a06..c2b3a69 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -58,29 +58,16 @@ org.apache.shardingsphere - shardingsphere-jdbc-core-spring-boot-starter - 5.1.1 + shardingsphere-jdbc-core + 5.4.1 - commons-io - commons-io - 2.15.1 - - - org.apache.calcite.avatica - avatica-core - 1.22.0 - - - org.apache.calcite - calcite-core - 1.32.0 - - - org.apache.commons - commons-collections4 - 4.5.0-M2 + org.apache.shardingsphere + shardingsphere-jdbc + 5.4.1 + pom + diff --git a/service/src/main/java/cn/bunny/service/ServiceApplication.java b/service/src/main/java/cn/bunny/service/ServiceApplication.java index c9022ce..146d006 100644 --- a/service/src/main/java/cn/bunny/service/ServiceApplication.java +++ b/service/src/main/java/cn/bunny/service/ServiceApplication.java @@ -1,21 +1,13 @@ package cn.bunny.service; -import lombok.extern.slf4j.Slf4j; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.transaction.annotation.EnableTransactionManagement; -@ComponentScan(basePackages = {"cn.bunny"}) @MapperScan("cn.bunny.service.mapper") -@EnableScheduling// 定时任务 -@EnableCaching// 开启缓存注解 @EnableTransactionManagement// 开启事务注解 @SpringBootApplication -@Slf4j public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); diff --git a/service/src/main/resources/application.yml b/service/src/main/resources/application.yml index f02052d..f590883 100644 --- a/service/src/main/resources/application.yml +++ b/service/src/main/resources/application.yml @@ -7,56 +7,15 @@ spring: application: name: bunny-service datasource: - type: com.zaxxer.hikari.HikariDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.sqlData}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true - username: ${bunny.master.username} - password: ${bunny.master.password} - shardingsphere: - mode: - type: Memory - datasource: - names: master,slave1,slave2 - master: - type: com.zaxxer.hikari.HikariDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - jdbc-url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.sqlData}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true - username: ${bunny.master.username} - password: ${bunny.master.password} - slave1: - type: com.zaxxer.hikari.HikariDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - jdbc-url: jdbc:mysql://${bunny.slave1.host}:${bunny.slave1.port}/${bunny.slave1.sqlData}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true - username: ${bunny.slave1.username} - password: ${bunny.slave1.password} - slave2: - type: com.zaxxer.hikari.HikariDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - jdbc-url: jdbc:mysql://${bunny.slave2.host}:${bunny.slave2.port}/${bunny.slave2.sqlData}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true - username: ${bunny.slave2.username} - password: ${bunny.slave2.password} - rules: - readwrite-splitting: - data-sources: - myDS: - type: Static - props: - write-data-source-name: master - read-data-source-names: slave1,slave2 - load-balancers: - load-balancer-name: alg_round - load-balancers: - alg_round: - type: ROUND_ROBIN - alg_random: - type: RANDOM - alg_weight: - type: WEIGHT - props: - - slave1: 1 - - slave2: 2 - props: - sql-show: true + driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver + url: jdbc:shardingsphere:classpath:static/shardingsphere.yaml + # type: com.zaxxer.hikari.HikariDataSource + # driver-class-name: com.mysql.cj.jdbc.Driver + # url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.sqlData}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true + # username: ${bunny.master.username} + # password: ${bunny.master.password} + + data: redis: host: ${bunny.redis.host} @@ -75,18 +34,10 @@ spring: mybatis-plus: mapper-locations: classpath:mapper/*.xml - global-config: - db-config: - logic-delete-field: isDelete configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志 logging: - level: - cn.bunny.service.mapper: info - cn.bunny.service.controller: info - cn.bunny.service.service: info - root: info pattern: dateformat: HH:mm:ss:SSS file: diff --git a/service/src/main/resources/static/shardingsphere.yaml b/service/src/main/resources/static/shardingsphere.yaml new file mode 100644 index 0000000..808d26e --- /dev/null +++ b/service/src/main/resources/static/shardingsphere.yaml @@ -0,0 +1,36 @@ +mode: + type: Standalone + repository: + type: JDBC +dataSources: + + master: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.jdbc.Driver + jdbcUrl: jdbc:mysql://192.168.3.98:3306/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true + username: root + password: 02120212 + slave1: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.jdbc.Driver + jdbcUrl: jdbc:mysql://192.168.3.98:3307/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true + username: root + password: 02120212 + slave2: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.jdbc.Driver + jdbcUrl: jdbc:mysql://192.168.3.98:3308/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true + username: root + password: 02120212 +rules: + dataSourceGroups: + readwrite_ds: + writeDataSourceName: master + readDataSourceNames: + - slave1 + - slave2 + transactionalReadQueryStrategy: PRIMARY + loadBalancerName: random + loadBalancers: + random: + type: RANDOM diff --git a/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java b/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java index 205cf12..2e85aee 100644 --- a/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java +++ b/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java @@ -7,11 +7,14 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.List; + @SpringBootTest class UserServiceImplTest { @Autowired private UserMapper userMapper; + // 添加用户 @Test void addUser() { User user = new User(); @@ -19,11 +22,18 @@ class UserServiceImplTest { userMapper.insert(user); } + // 事务使用 @Test @Transactional void addTransactional() { User user = new User(); - user.setUname("喜欢凌艳"); + user.setUname("哈哈哈"); userMapper.insert(user); } + + @Test + void selectUseList() { + List users = userMapper.selectList(null); + users.forEach(System.out::println); + } } \ No newline at end of file