瀏覽代碼

init java spring boot

zhangxiaoyu 3 年之前
父節點
當前提交
3be8f570f5

+ 5 - 0
pom.xml

@@ -21,6 +21,10 @@
21 21
             <groupId>org.springframework.boot</groupId>
22 22
             <artifactId>spring-boot-starter-web</artifactId>
23 23
         </dependency>
24
+        <dependency>
25
+            <groupId>org.springframework.boot</groupId>
26
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
27
+        </dependency>
24 28
 
25 29
         <dependency>
26 30
             <groupId>org.springframework.boot</groupId>
@@ -32,6 +36,7 @@
32 36
             <artifactId>mysql-connector-java</artifactId>
33 37
             <version>8.0.22</version>
34 38
         </dependency>
39
+
35 40
     </dependencies>
36 41
 
37 42
     <build>

+ 1 - 1
src/main/java/com/example/demo/config/MyConfig.java

@@ -10,7 +10,7 @@ import org.springframework.context.annotation.ImportResource;
10 10
 public class MyConfig {
11 11
     @Bean
12 12
     public User user01() {
13
-        User zhangsan = new User("zhangsan");
13
+        User zhangsan = new User("zhangsan1");
14 14
         return  zhangsan;
15 15
     }
16 16
     @Bean

+ 20 - 15
src/main/java/com/example/springbootdemo/SpringbootdemoApplication.java

@@ -3,6 +3,7 @@ package com.example.springbootdemo;
3 3
 import com.example.demo.config.MyConfig;
4 4
 import com.example.demo.controller.User;
5 5
 import com.example.springbootdemo.controller.TrainJavaController;
6
+import com.example.springbootdemo.zxyTeST.EnumTest;
6 7
 import com.example.springbootdemo.zxyTeST.SubClass;
7 8
 import org.springframework.boot.SpringApplication;
8 9
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -14,21 +15,25 @@ import org.springframework.context.annotation.ComponentScan;
14 15
 public class SpringbootdemoApplication {
15 16
     public static void main(String[] args) {
16 17
         ConfigurableApplicationContext run  = SpringApplication.run(SpringbootdemoApplication.class, args);
17
-        String[] names = run.getBeanDefinitionNames();
18
-        for(String name:names) {
19
-            System.out.println(name);
20
-        }
21
-        MyConfig bean = run.getBean(MyConfig.class);
22
-        System.out.println(bean);
23
-        User u1 = bean.user01();
24
-        User u2 = bean.user01();
25
-        System.out.println("--"+ (u1 == u2));
26
-        // conditional
27
-        boolean user02 = run.containsBean("user011");
28
-        System.out.println(user02);
29
-        SubClass sc = new SubClass(200);
30
-        SubClass sc1 = new SubClass();
31
-        sc.SuperClass1(11123);
18
+//        String[] names = run.getBeanDefinitionNames();
19
+//        for(String name:names) {
20
+//            System.out.println(name);
21
+//        }
22
+//        MyConfig bean = run.getBean(MyConfig.class);
23
+//        System.out.println(bean);
24
+//        User u1 = bean.user01();
25
+//        User u2 = bean.user01();
26
+//        System.out.println("--"+ (u1 == u2));
27
+//        // conditional
28
+//        boolean user02 = run.containsBean("user011");
29
+//        System.out.println(user02);
30
+//        SubClass sc = new SubClass(200);
31
+//        SubClass sc1 = new SubClass();
32
+//        sc.SuperClass1(11123);
33
+        // enum
34
+        //EnumTest enumTest = EnumTest.BLUE;
35
+      //  System.out.println(EnumTest.NO_START.getName());
36
+
32 37
     }
33 38
 }
34 39
 

+ 12 - 3
src/main/java/com/example/springbootdemo/controller/HelloController.java

@@ -1,15 +1,24 @@
1 1
 package com.example.springbootdemo.controller;
2 2
 
3 3
 import com.example.demo.controller.User;
4
+import com.example.springbootdemo.zxyTeST.sql.dao.ColorRepository;
5
+import com.example.springbootdemo.zxyTeST.sql.entity.Color;
6
+import org.springframework.beans.factory.annotation.Autowired;
4 7
 import org.springframework.web.bind.annotation.RequestMapping;
5 8
 import org.springframework.web.bind.annotation.RestController;
6 9
 
10
+import java.util.List;
11
+
7 12
 @RestController
8 13
 public class HelloController {
9
-
14
+    @Autowired
15
+    ColorRepository colorRepository;
10 16
     @RequestMapping("/hello")
11
-    public String hello() {
12
-        return  "1";
17
+    public void hello() {
18
+        List<Color> colors = colorRepository.findAll();
19
+        for (Color color:colors) {
20
+            System.out.println(color);
21
+        }
13 22
     }
14 23
 
15 24
 

+ 34 - 0
src/main/java/com/example/springbootdemo/zxyTeST/EnumTest.java

@@ -0,0 +1,34 @@
1
+package com.example.springbootdemo.zxyTeST;
2
+
3
+public enum EnumTest {
4
+    //RED,GREEN,BLUE;
5
+    NO_START("未开始", 1),
6
+    READY("准备中", 2),
7
+    PROCESSING("进行中", 3),
8
+    END_NO_FINISH("已结束领取未完成", 4),
9
+    END_YES_FINISH("已结束领取完成", 5),
10
+    END_FORCE("强制结束",6);
11
+
12
+    private String name;
13
+    private Integer value;
14
+    private EnumTest(String name, Integer value) {
15
+        this.name = name;
16
+        this.value = value;
17
+    }
18
+
19
+    public String getName() {
20
+        return name;
21
+    }
22
+
23
+    public void setName(String name) {
24
+        this.name = name;
25
+    }
26
+
27
+    public Integer getValue() {
28
+        return value;
29
+    }
30
+
31
+    public void setValue(Integer value) {
32
+        this.value = value;
33
+    }
34
+}

+ 11 - 0
src/main/java/com/example/springbootdemo/zxyTeST/sql/dao/ColorRepository.java

@@ -0,0 +1,11 @@
1
+package com.example.springbootdemo.zxyTeST.sql.dao;
2
+
3
+import com.example.springbootdemo.zxyTeST.sql.entity.Color;
4
+import org.springframework.data.jpa.repository.JpaRepository;
5
+
6
+import java.util.List;
7
+
8
+public interface ColorRepository extends JpaRepository<Color,Long> {
9
+    List<Color> findColorsByColor_id(String color_id);
10
+    Color findColorsByColor_idAndColor_name(String color_id,String color_name);
11
+}

+ 44 - 0
src/main/java/com/example/springbootdemo/zxyTeST/sql/entity/Color.java

@@ -0,0 +1,44 @@
1
+package com.example.springbootdemo.zxyTeST.sql.entity;
2
+
3
+import javax.persistence.*;
4
+import java.io.Serializable;
5
+
6
+@Entity
7
+@Table(name = "wb_tbl_color")
8
+public class Color implements Serializable {
9
+    @Id
10
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
11
+    private long color_id;
12
+    public long getColor_id() {
13
+        return color_id;
14
+    }
15
+    @Column(name="color_name")
16
+    private String color_name;
17
+    public String getColor_name() {
18
+        return color_name;
19
+    }
20
+
21
+    public String getColor_status() {
22
+        return color_status;
23
+    }
24
+
25
+    public void setColor_status(String color_status) {
26
+        this.color_status = color_status;
27
+    }
28
+    @Column(name = "color_status")
29
+    private String color_status;
30
+
31
+    public String getColor_type() {
32
+        return color_type;
33
+    }
34
+
35
+    public void setColor_type(String color_type) {
36
+        this.color_type = color_type;
37
+    }
38
+
39
+    @Column(name = "color_type")
40
+    private String color_type;
41
+
42
+
43
+
44
+}

+ 6 - 0
src/main/resources/application.properties

@@ -1 +1,7 @@
1 1
 server.port=8081
2
+spring.datasource.url=jdbc:mysql://192.168.2.130:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
3
+spring.datasource.name=root
4
+spring.datasource.password=123456
5
+spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
6
+spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
7
+spring.jpa.open-in-view=false