Browse Source

1、mongo提交

17358629955 2 năm trước cách đây
mục cha
commit
33b48e7c02

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "sckw-common/sckw-service-platform"]
+	path = sckw-common/sckw-service-platform
+	url = http://git.sckaiwu.cn/17358629955/sckw-service-platform.git

+ 8 - 2
pom.xml

@@ -37,8 +37,7 @@
         <fastjson.version>2.0.32</fastjson.version>
         <hutool.version>5.8.18</hutool.version>
         <pagehelper.version>5.3.2</pagehelper.version>
-        <pagehelper-spring-boot-starter.version>1.4.7</pagehelper-spring-boot-starter.version>
-
+        <junit.version>4.13.2</junit.version>
     </properties>
     <dependencyManagement>
         <dependencies>
@@ -118,6 +117,13 @@
                 <artifactId>pagehelper-spring-boot-starter</artifactId>
                 <version>${pagehelper-spring-boot-starter.version}</version>
             </dependency>
+	    
+	    <dependency>
+                <groupId>junit</groupId>
+                <artifactId>junit</artifactId>
+                <version>${junit.version}</version>
+                <scope>test</scope>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 

+ 2 - 2
sckw-auth/src/main/resources/bootstrap-dev.yml

@@ -3,14 +3,14 @@ spring:
     nacos:
       discovery:
         # 服务注册地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 命名空间
         namespace: sckw-service-platform-dev
         # 共享配置
         group: sckw-service-platform
       config:
         # 配置中心地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 配置文件格式
         file-extension: yaml
         # 命名空间

+ 32 - 0
sckw-common/sckw-common-mongo/pom.xml

@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.sckw</groupId>
+        <artifactId>sckw-common</artifactId>
+        <version>1.0.0</version>
+    </parent>
+
+    <artifactId>sckw-common-mongo</artifactId>
+    <version>1.0.0</version>
+
+    <description>mongodb</description>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <spring-boot.version>3.0.5</spring-boot.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-mongodb</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>

+ 1 - 0
sckw-common/sckw-service-platform

@@ -0,0 +1 @@
+Subproject commit 5ed055664e0636361700eac3e3da1bbd58226415

+ 2 - 2
sckw-gateway/src/main/resources/bootstrap.yml

@@ -14,14 +14,14 @@ spring:
     nacos:
       discovery:
         # 服务注册地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 命名空间
         namespace: sckw-service-platform-dev
         # 共享配置
         group: sckw-service-platform
       config:
         # 配置中心地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 配置文件格式
         file-extension: yaml
         # 命名空间

+ 12 - 0
sckw-modules/sckw-example/pom.xml

@@ -41,6 +41,12 @@
             <version>1.0.0</version>
         </dependency>
 
+        <!--<dependency>
+            <groupId>com.sckw</groupId>
+            <artifactId>sckw-common-elasticsearch</artifactId>
+            <version>1.0.0</version>
+        </dependency>-->
+
         <dependency>
             <groupId>com.sckw</groupId>
             <artifactId>sckw-common-elasticsearch</artifactId>
@@ -82,6 +88,12 @@
             <version>1.0.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.sckw</groupId>
+            <artifactId>sckw-common-mongo</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>

+ 34 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/model/Student.java

@@ -0,0 +1,34 @@
+package com.sckw.example.model;
+
+import lombok.Data;
+import org.springframework.data.annotation.Id;
+
+import java.util.Date;
+
+@Data
+public class Student {
+
+    @Id
+    private String studentId;
+
+    private String studentName;
+
+    private Integer studentAge;
+
+    private Double studentScore;
+
+    private Date studentBirthday;
+
+    public Student(String studentId, String studentName, Integer studentAge, Double studentScore, Date studentBirthday) {
+        this.studentId = studentId;
+        this.studentName = studentName;
+        this.studentAge = studentAge;
+        this.studentScore = studentScore;
+        this.studentBirthday = studentBirthday;
+    }
+
+    public Student() {
+    }
+
+
+}

+ 13 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/service/StudentRepository.java

@@ -0,0 +1,13 @@
+package com.sckw.example.service;
+
+import com.sckw.example.model.Student;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+/**
+ * desc 继承MongoRepository即可,不需要实现类
+ * author zk
+ * date 2023/5/9 0015
+ */
+public interface StudentRepository extends MongoRepository<Student, String> {
+}
+

+ 42 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/service/StudentService.java

@@ -0,0 +1,42 @@
+package com.sckw.example.service;
+
+import com.sckw.example.model.Student;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.stereotype.Service;
+import java.util.List;
+
+/**
+ * desc TODO
+ * author zk
+ * date 2023/6/15 0015
+ */
+@Service
+public class StudentService {
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
+    public void addOne(Student student) {
+        mongoTemplate.save(student);
+    }
+
+    public void deleteOneById(String studentId) {
+        Student stu = mongoTemplate.findById(studentId, Student.class);
+        if (stu != null) {
+            mongoTemplate.remove(stu);
+        }
+    }
+
+    public void updateOne(Student student) {
+        mongoTemplate.save(student);
+
+    }
+
+    public Student findOneById(String studentId) {
+        return mongoTemplate.findById(studentId, Student.class);
+    }
+
+    public List<Student> findAll() {
+        return mongoTemplate.findAll(Student.class);
+    }
+}

+ 34 - 19
sckw-modules/sckw-example/src/main/resources/bootstrap-dev.yml

@@ -1,10 +1,4 @@
 spring:
-  rabbitmq:
-    username: admin
-    password: admin
-    host: 10.10.10.138
-    port: 5672
-    virtual-host: /
   cloud:
     nacos:
       discovery:
@@ -23,18 +17,9 @@ spring:
         namespace: sckw-service-platform-dev
         # 共享配置
         group: sckw-service-platform
+    function:
+      definition: sckwSms;sckwMessage
     stream:
-      bindings:
-        sckwSms-out-0:
-          destination: sckw-sms
-          content-type: application/json
-          default-binder: defaultRabbit
-          group: sckw
-        sckwMessage-out-0:
-          destination: sckw-message
-          content-type: application/json
-          default-binder: defaultRabbit
-          group: sckw
       binders:
         defaultRabbit:
           type: rabbit
@@ -43,11 +28,41 @@ spring:
               rabbitmq:
                 virtual-host: /
                 host: 10.10.10.230
-                port: 15672
+                port: 5672
                 username: sckw
                 password: Yy123...
+      bindings:
+        sckwSms-in-0:
+          destination: sckw-sms
+          content-type: application/json
+          binder: defaultRabbit
+          group: sckw
+        sckwSms-out-0:
+          destination: sckw-sms
+          content-type: application/json
+          default-binder: defaultRabbit
+          group: sckw
+        sckwMessage-in-0:
+          destination: sckw-message
+          content-type: application/json
+          binder: defaultRabbit
+          group: sckw
+        sckwMessage-out-0:
+          destination: sckw-message
+          content-type: application/json
+          binder: defaultRabbit
+          group: sckw
+  rabbitmq:
+    username: sckw
+    password: Yy123...
+    host: 10.10.10.230
+    port: 5672
+    virtual-host: /
+  data:
+    mongodb:
+      uri: mongodb://sckw:Yy123...@10.10.10.230:27017/sckw?authSource=admin&authMechanism=SCRAM-SHA-1
 
-
+# dubbo
 dubbo:
   application:
     # 此处没有延用spring.application.name是因为当前项目本身也会注册到nacos中,如果dubbo也延用相同的名称,在nacos服务里会看到注册的producer-server服务数为2

+ 61 - 0
sckw-modules/sckw-example/src/test/java/com/sckw/example/StudentDaoTest.java

@@ -0,0 +1,61 @@
+package com.sckw.example;
+
+
+import com.sckw.example.model.Student;
+import com.sckw.example.service.StudentService;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import java.util.Date;
+import java.util.List;
+
+@SpringBootTest
+public class StudentDaoTest {
+
+    @Autowired
+    private StudentService studentDao;
+
+    @Test
+    void addOneStudent(){
+//        插入10行
+        Long begtime = System.currentTimeMillis();
+        for (Integer count = 0; count < 10000; count++) {
+            Student student = new Student();
+            student.setStudentId("study_"+count);
+            student.setStudentName("Echo"+count);
+            student.setStudentAge(count);
+            student.setStudentScore(98.5-count);
+            student.setStudentBirthday(new Date());
+            studentDao.addOne(student);
+        }
+        Long enttime = System.currentTimeMillis();
+        System.out.println("------------------"+(enttime - begtime));
+    }
+
+    @Test
+    void deleteOneStudentByStudentId(){
+//        删除id为study_0的学生
+        studentDao.deleteOneById("study_0");
+    }
+
+    @Test
+    void updateOneStudent(){
+//        修改id为study_1的Student年龄为21
+        Student student = studentDao.findOneById("study_1");
+        student.setStudentAge(21);
+        studentDao.updateOne(student);
+
+    }
+
+    @Test
+    void getOneStudentByStudentId(){
+        System.out.println(studentDao.findOneById("study_1"));
+    }
+
+    @Test
+    void getAllStudent(){
+        List<Student> studentList = studentDao.findAll();
+        studentList.forEach(System.out::println);
+    }
+}
+

+ 152 - 0
sckw-modules/sckw-example/src/test/java/com/sckw/example/StudentRepositoryTest.java

@@ -0,0 +1,152 @@
+package com.sckw.example;
+
+import com.sckw.example.model.Student;
+import com.sckw.example.service.StudentRepository;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.data.domain.*;
+import java.util.*;
+
+@SpringBootTest
+public class StudentRepositoryTest {
+
+    @Autowired
+    private StudentRepository studentRepository;
+
+    /**
+     * 插入单条数据
+     */
+    @Test
+    public void insertOne() {
+        Student student = new Student("009", "tom", 18, 88.2d, new Date());
+        studentRepository.insert(student);
+    }
+
+    /**
+     * 插入多条数据
+     */
+    @Test
+    public void insertMany() {
+        List<Student> list = new ArrayList<>();
+        Long begtime = System.currentTimeMillis();
+        for (int i=0; i<10000; i++) {
+            Student student1 = new Student("7"+i, "jerry", 19, 38.2d, new Date());
+            list.add(student1);
+        }
+        studentRepository.insert(list);
+        Long enttime = System.currentTimeMillis();
+        System.out.println("------------------"+(enttime - begtime));
+    }
+
+    /**
+     * 修改数据
+     */
+    @Test
+    public void update() {
+        Optional<Student> op = studentRepository.findById("009");
+        Student student = op.get();
+        student.setStudentAge(222);
+        studentRepository.save(student);
+    }
+
+    /**
+     * 查询数据
+     */
+    @Test
+    public void query() {
+        Long begtime = System.currentTimeMillis();
+        //根据Id查询单个对象
+        Optional<Student> stuOp = studentRepository.findById("0000");
+        System.out.println(stuOp.get());
+
+        //根据字段查询单个对象
+        Student student = new Student();
+        student.setStudentAge(19);
+        Optional<Student> stuOp1 = studentRepository.findOne(Example.of(student));
+        System.out.println(stuOp1.get());
+
+        //根据id列表查询多个对象
+        Iterable<Student> itStu = studentRepository.findAllById(Arrays.asList(new String[]{"001", "002"}));
+        Iterator<Student> itor = itStu.iterator();
+        while (itor.hasNext())
+            System.out.println(itor.next());
+
+        //查询所有
+        List<Student> all = studentRepository.findAll();
+
+        //查询所有并排序
+        List<Student> all1 = studentRepository.findAll(Sort.by(Sort.Order.desc("studentId"), Sort.Order.asc("studentName")));
+        for (Student stu : all1)
+            System.out.println(stu);
+
+        System.out.println("0------------------:"+all1.size());
+
+        //根据条件查询所有并排序
+        Student student1 = new Student();
+        student1.setStudentName("tom");
+        List<Student> all2 = studentRepository.findAll(Example.of(student1), Sort.by(Sort.Order.desc("studentId"), Sort.Order.asc("studentName")));
+        for (Student stu : all2)
+            System.out.println(stu);
+
+
+
+        //根据条件查询所有并排序,且分页
+        Pageable pageable = PageRequest.of(0, 2);
+        Page<Student> all3 = studentRepository.findAll(pageable);
+        List<Student> content = all3.getContent();
+        for (Student stu : content)
+            System.out.println(stu);
+
+        System.out.println("1------------------:"+content.size());
+        Long enttime = System.currentTimeMillis();
+        System.out.println("2------------------:"+(enttime - begtime));
+    }
+
+    /**
+     * 其他操作
+     */
+    @Test
+    public void other() {
+        //count
+        long count = studentRepository.count();
+        System.out.println(count);
+        Student student = new Student();
+        student.setStudentAge(18);
+        long count1 = studentRepository.count(Example.of(student));
+        System.out.println(count1);
+
+        //exists
+        boolean exists = studentRepository.exists(Example.of(student));
+        System.out.println(exists);
+        boolean b = studentRepository.existsById("001");
+        System.out.println(b);
+    }
+
+
+    /**
+     * 删除操作
+     */
+    @Test
+    public void remove() {
+        //根据id删除单个对象
+        studentRepository.deleteById("001");
+
+        //根据字段删除
+        Student student = new Student();
+        student.setStudentAge(20);
+        studentRepository.delete(student);
+
+        //删除所有
+        studentRepository.deleteAll();
+
+        //根据字段删除多个
+        Student student1 = new Student();
+        student1.setStudentName("jerry");
+        List<Student> list = new ArrayList<>();
+        list.add(student);
+        list.add(student1);
+        studentRepository.deleteAll(list);
+    }
+}
+

+ 3 - 37
sckw-modules/sckw-file/src/main/resouces/bootstrap-dev.yml

@@ -3,55 +3,21 @@ spring:
     nacos:
       discovery:
         # 服务注册地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 命名空间
         namespace: sckw-service-platform-dev
         # 共享配置
         group: sckw-service-platform
       config:
         # 配置中心地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 配置文件格式
         file-extension: yaml
         # 命名空间
         namespace: sckw-service-platform-dev
         # 共享配置
         group: sckw-service-platform
-    function:
-      definition: sckwSms;sckwMessage
-    stream:
-      bindings:
-        sckwSms-in-0:
-          destination: sckw-sms
-          content-type: application/json
-          default-binder: defaultRabbit
-          group: sckw
-        sckwSms-out-0:
-          destination: sckw-sms
-          content-type: application/json
-          default-binder: defaultRabbit
-          group: sckw
-        sckwMessage-in-0:
-          destination: sckw-message
-          content-type: application/json
-          default-binder: defaultRabbit
-          group: sckw
-        sckwMessage-out-0:
-          destination: sckw-message
-          content-type: application/json
-          default-binder: defaultRabbit
-          group: sckw
-      binders:
-        defaultRabbit:
-          type: rabbit
-          environment:
-            spring:
-              rabbitmq:
-                virtual-host: /
-                host: 39.104.134.114
-                port: 5672
-                username: wph
-                password: Yy123...
+
 #限制上传文件大小
   servlet:
     multipart:

+ 3 - 3
sckw-modules/sckw-message/src/main/resources/bootstrap-dev.yml

@@ -27,9 +27,9 @@ spring:
             spring:
               rabbitmq:
                 virtual-host: /
-                host: 39.104.134.114
+                host: 10.10.10.230
                 port: 5672
-                username: wph
+                username: sckw
                 password: Yy123...
       bindings:
         sckwSms-in-0:
@@ -56,7 +56,7 @@ spring:
     username: sckw
     password: Yy123...
     host: 10.10.10.230
-    port: 15672
+    port: 5672
     virtual-host: /
 
 

+ 6 - 6
sckw-modules/sckw-system/src/main/resources/bootstrap-dev.yml

@@ -3,14 +3,14 @@ spring:
     nacos:
       discovery:
         # 服务注册地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 命名空间
         namespace: sckw-service-platform-dev
         # 共享配置
         group: sckw-service-platform
       config:
         # 配置中心地址
-        server-addr: 127.0.0.1:8848
+        server-addr: 10.10.10.230:8848
         # 配置文件格式
         file-extension: yaml
         # 命名空间
@@ -36,14 +36,14 @@ spring:
             spring:
               rabbitmq:
                 virtual-host: /
-                host: 39.104.134.114
+                host: 10.10.10.230
                 port: 5672
-                username: wph
+                username: sckw
                 password: Yy123...
   rabbitmq:
-    username: wph
+    username: sckw
     password: Yy123...
-    host: 39.104.134.114
+    host: 10.10.10.230
     port: 5672
     virtual-host: /