浏览代码

测试mybatis plus

zhangxiaoyu 2 年之前
父节点
当前提交
73ac466534

+ 31 - 0
src/main/java/com/example/Thread/Thread03.java

@@ -0,0 +1,31 @@
1
+package com.example.Thread;
2
+
3
+public class Thread03 {
4
+    public static void main(String[] args) {
5
+        C1 c1 = new C1();
6
+        Thread thread = new Thread(c1);
7
+        thread.start();
8
+    }
9
+}
10
+// 1s输出helloworld
11
+class C1 implements Runnable {
12
+    int i = 0;
13
+    @Override
14
+    public void run() {
15
+        while (true) {
16
+            try {
17
+                Thread.sleep(1000);
18
+                System.out.println("hello1");
19
+                i++;
20
+
21
+            } catch (InterruptedException e) {
22
+                e.printStackTrace();
23
+            }
24
+            if(i>3) {
25
+                break;
26
+            }
27
+
28
+        }
29
+
30
+    }
31
+}

+ 14 - 0
src/main/java/com/example/demo/handler/SpringScheduleHandler.java

@@ -0,0 +1,14 @@
1
+package com.example.demo.handler;
2
+
3
+import org.springframework.scheduling.annotation.EnableScheduling;
4
+import org.springframework.scheduling.annotation.Scheduled;
5
+import org.springframework.stereotype.Component;
6
+
7
+@Component
8
+//@EnableScheduling
9
+public class SpringScheduleHandler {
10
+    @Scheduled(cron = "*/5 * * * * *")
11
+    public void exe() {
12
+        System.out.println("exe");
13
+    }
14
+}

+ 26 - 0
src/main/java/com/example/singletone/LazySingletonOpt.java

@@ -0,0 +1,26 @@
1
+package com.example.singletone;
2
+
3
+public class LazySingletonOpt {
4
+    public static void main(String[] args) {
5
+        LazySingleton singleton1 = LazySingleton.getInstance();
6
+        LazySingleton singleton2 = LazySingleton.getInstance();
7
+        if(singleton1 == singleton2) {
8
+
9
+        }
10
+        System.out.println(singleton1.hashCode());
11
+        System.out.println(singleton2.hashCode());
12
+    }
13
+}
14
+// 懒汉式
15
+class LazySingleton {
16
+    private static LazySingleton lazySingleton = null;
17
+    private LazySingleton() {};
18
+    // 判断使用到该方法时再进行创建 lazySingleton
19
+    public static synchronized LazySingleton getInstance() {
20
+        if(lazySingleton == null) {
21
+            lazySingleton = new LazySingleton();
22
+        }
23
+        return lazySingleton;
24
+    }
25
+
26
+}

+ 21 - 0
src/main/java/com/example/singletone/Singleton1.java

@@ -0,0 +1,21 @@
1
+package com.example.singletone;
2
+public class Singleton1 {
3
+    public static void main(String[] args) {
4
+        HungrySingle single1 = HungrySingle.getInstance();
5
+        HungrySingle single2 = HungrySingle.getInstance();
6
+        if(single1 == single2) {
7
+
8
+        }
9
+    }
10
+}
11
+// 饿汉式(静态变量式)
12
+class HungrySingle {
13
+   // 构造函数私有
14
+    private HungrySingle() {
15
+    }
16
+    private static final HungrySingle hungrySingle = new HungrySingle();
17
+    // 返回instance
18
+    public static HungrySingle getInstance() {
19
+        return hungrySingle;
20
+    }
21
+}

+ 20 - 0
src/main/java/com/example/springbootdemo/controller/ColorController.java

@@ -0,0 +1,20 @@
1
+package com.example.springbootdemo.controller;
2
+
3
+import com.example.springbootdemo.entity.Color;
4
+import com.example.springbootdemo.service.ColorService;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.web.bind.annotation.GetMapping;
7
+import org.springframework.web.bind.annotation.RequestParam;
8
+import org.springframework.web.bind.annotation.RestController;
9
+
10
+@RestController
11
+public class ColorController {
12
+    @Autowired
13
+    ColorService colorService;
14
+
15
+    @GetMapping("/getColorInfo")
16
+    public Color getColorInfo(@RequestParam("color_id") Integer colorId) {
17
+        return colorService.getById(colorId);
18
+    }
19
+
20
+}

+ 18 - 0
src/main/java/com/example/springbootdemo/entity/Color.java

@@ -0,0 +1,18 @@
1
+package com.example.springbootdemo.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.TableId;
4
+import com.baomidou.mybatisplus.annotation.TableName;
5
+import lombok.Data;
6
+
7
+import javax.persistence.GeneratedValue;
8
+@Data
9
+public class Color {
10
+    @TableId
11
+    @GeneratedValue
12
+    private Integer colorId;
13
+    private String colorName;
14
+    private Integer colorStatus;
15
+    private String colorType;
16
+    private String colorCode;
17
+    private Integer companyId;
18
+}

+ 10 - 0
src/main/java/com/example/springbootdemo/mapper/ColorMapper.java

@@ -0,0 +1,10 @@
1
+package com.example.springbootdemo.mapper;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.example.springbootdemo.entity.Color;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+@Mapper
8
+public interface ColorMapper extends BaseMapper<Color> {
9
+
10
+}

+ 7 - 0
src/main/java/com/example/springbootdemo/service/ColorService.java

@@ -0,0 +1,7 @@
1
+package com.example.springbootdemo.service;
2
+
3
+import com.baomidou.mybatisplus.extension.service.IService;
4
+import com.example.springbootdemo.entity.Color;
5
+
6
+public interface ColorService extends IService<Color> {
7
+}

+ 11 - 0
src/main/java/com/example/springbootdemo/service/impl/ColorServiceImpl.java

@@ -0,0 +1,11 @@
1
+package com.example.springbootdemo.service.impl;
2
+
3
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4
+import com.example.springbootdemo.entity.Color;
5
+import com.example.springbootdemo.mapper.ColorMapper;
6
+import com.example.springbootdemo.service.ColorService;
7
+import org.springframework.stereotype.Service;
8
+
9
+@Service
10
+public class ColorServiceImpl extends ServiceImpl<ColorMapper, Color> implements ColorService {
11
+}

+ 6 - 2
src/main/resources/application-dev.yml

@@ -18,7 +18,7 @@ spring:
18 18
         login-username: zxy
19 19
         login-password: 123456
20 20
       driver-class-name: com.mysql.cj.jdbc.Driver
21
-      url: jdbc:mysql://localhost:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
21
+      url: jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
22 22
       username: root
23 23
       password:
24 24
   jpa:
@@ -32,4 +32,8 @@ mybatis:
32 32
     map-underscore-to-camel-case: true
33 33
 #mybatis-plus:
34 34
 #  configuration:
35
-#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
35
+#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
36
+mybatis-plus:
37
+  global-config:
38
+    db-config:
39
+      table-prefix: wb_tbl_

+ 2 - 2
src/main/resources/application.yml

@@ -11,9 +11,9 @@ server:
11 11
   port: 8081
12 12
 spring:
13 13
   datasource:
14
-    url: jdbc:mysql://192.168.2.130:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
14
+    url: jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
15 15
     username: root
16
-    password: 123456
16
+    password:
17 17
     driver-class-name: com.mysql.cj.jdbc.Driver
18 18
   jpa:
19 19
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect