瀏覽代碼

import lombok

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

+ 14 - 0
pom.xml

@@ -25,6 +25,10 @@
25 25
             <groupId>org.springframework.boot</groupId>
26 26
             <artifactId>spring-boot-starter-data-jpa</artifactId>
27 27
         </dependency>
28
+        <dependency>
29
+            <groupId>org.springframework.boot</groupId>
30
+            <artifactId>spring-boot-starter-data-jdbc</artifactId>
31
+        </dependency>
28 32
         <dependency>
29 33
             <groupId>org.projectlombok</groupId>
30 34
             <artifactId>lombok</artifactId>
@@ -34,6 +38,16 @@
34 38
             <artifactId>spring-boot-starter-test</artifactId>
35 39
             <scope>test</scope>
36 40
         </dependency>
41
+        <dependency>
42
+            <groupId>org.mybatis.spring.boot</groupId>
43
+            <artifactId>mybatis-spring-boot-starter</artifactId>
44
+            <version>2.1.4</version>
45
+        </dependency>
46
+        <dependency>
47
+            <groupId>com.alibaba</groupId>
48
+            <artifactId>druid-spring-boot-starter</artifactId>
49
+            <version>1.1.10</version>
50
+        </dependency>
37 51
         <dependency>
38 52
             <groupId>mysql</groupId>
39 53
             <artifactId>mysql-connector-java</artifactId>

+ 11 - 4
src/main/java/com/example/springbootdemo/controller/HelloController.java

@@ -5,6 +5,7 @@ import com.example.springbootdemo.zxyTeST.sql.dao.ColorRepository;
5 5
 import com.example.springbootdemo.zxyTeST.sql.entity.ArmGrabEntity;
6 6
 import com.example.springbootdemo.zxyTeST.sql.entity.Color;
7 7
 import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.dao.EmptyResultDataAccessException;
8 9
 import org.springframework.jdbc.core.JdbcTemplate;
9 10
 import org.springframework.web.bind.annotation.RequestMapping;
10 11
 import org.springframework.web.bind.annotation.RestController;
@@ -32,10 +33,16 @@ public class HelloController {
32 33
     }
33 34
     @RequestMapping("/jdbc")
34 35
     public String jdbc() {
35
-        String sql = "select color_name from wb_tbl_color where color_id = ?";
36
-        String color_name = (String)jdbcTemplate.queryForObject(
37
-                sql, new Object[] { 2314 }, String.class);
38
-        return color_name;
36
+        String sql = "select color_name from wb_tbl_color where color_id = ? and color_type = ?";
37
+        try {
38
+            String color_name = jdbcTemplate.queryForObject(
39
+                    sql, new Object[] { 2314, 1 }, String.class);
40
+            return color_name;
41
+        } catch (EmptyResultDataAccessException e) {
42
+            return "hha我snull";
43
+        }
44
+
45
+
39 46
     }
40 47
 
41 48
 

+ 24 - 0
src/main/java/com/example/springbootdemo/entity/FootScanEntity.java

@@ -0,0 +1,24 @@
1
+package com.example.springbootdemo.entity;
2
+
3
+import lombok.Data;
4
+
5
+import javax.persistence.Entity;
6
+import javax.persistence.GeneratedValue;
7
+import javax.persistence.Id;
8
+import javax.persistence.Table;
9
+import java.io.Serializable;
10
+
11
+/**
12
+ * @author zhangxiaoyu
13
+ * @version 1.0
14
+ * @date 2021/5/31 下午5:14
15
+ */
16
+@Entity
17
+@Data
18
+@Table(name = "wb_tbl_foot_scan")
19
+public class FootScanEntity implements Serializable {
20
+    @Id
21
+    @GeneratedValue
22
+    private Integer footScanId;
23
+    private String deviceCode;
24
+}

+ 12 - 0
src/main/java/com/example/springbootdemo/mapper/FootScanMapper.java

@@ -0,0 +1,12 @@
1
+package com.example.springbootdemo.mapper;
2
+
3
+import org.springframework.stereotype.Repository;
4
+
5
+/**
6
+ * @author zhangxiaoyu
7
+ * @version 1.0
8
+ * @date 2021/5/31 下午5:20
9
+ */
10
+@Repository
11
+public interface FootScanMapper {
12
+}

+ 38 - 0
src/main/java/com/example/springbootdemo/zxyTeST/druid/MyDataSource.java

@@ -0,0 +1,38 @@
1
+package com.example.springbootdemo.zxyTeST.druid;
2
+
3
+import com.alibaba.druid.pool.DruidDataSource;
4
+import com.alibaba.druid.support.http.StatViewServlet;
5
+import org.springframework.boot.context.properties.ConfigurationProperties;
6
+import org.springframework.boot.web.servlet.ServletRegistrationBean;
7
+import org.springframework.context.annotation.Bean;
8
+import org.springframework.context.annotation.Configuration;
9
+
10
+import javax.sql.DataSource;
11
+import java.sql.SQLException;
12
+
13
+/**
14
+ * @author zhangxiaoyu
15
+ * @version 1.0
16
+ * @date 2021/5/31 下午2:25
17
+ */
18
+//@Configuration
19
+public class MyDataSource {
20
+//    @ConfigurationProperties(prefix = "spring.datasource")
21
+    @Bean
22
+    public DataSource dataSource() throws SQLException {
23
+        DruidDataSource druidDataSource = new DruidDataSource();
24
+        // jiaru jiankong
25
+        druidDataSource.setFilters("stat,wall");
26
+        return  druidDataSource;
27
+    }
28
+    @Bean
29
+    public ServletRegistrationBean servletRegistrationBean() {
30
+        StatViewServlet statViewServlet = new StatViewServlet();
31
+        ServletRegistrationBean registrationBean = new ServletRegistrationBean(statViewServlet,"/druid/*");
32
+        registrationBean.addInitParameter("loginUsername","zxy");
33
+        registrationBean.addInitParameter("loginPassword","111123");
34
+
35
+        return registrationBean;
36
+    }
37
+
38
+}

+ 12 - 4
src/main/resources/application-dev.yml

@@ -11,12 +11,20 @@ server:
11 11
   port: 8082
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
15
-    username: root
16
-    password: 123456
17
-    driver-class-name: com.mysql.cj.jdbc.Driver
14
+    druid:
15
+      filters: stat,wall
16
+      stat-view-servlet:
17
+        enabled: true
18
+        login-username: zxy
19
+        login-password: 123456
20
+      driver-class-name: com.mysql.cj.jdbc.Driver
21
+      url: jdbc:mysql://192.168.2.130:3306/ceshi?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
22
+      username: root
23
+      password: 123456
18 24
   jpa:
19 25
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
20 26
     open-in-view: false
21 27
   freemarker:
22 28
     cache: false
29
+mybatis:
30
+#  config-location:

+ 5 - 0
src/main/resources/mapper/ColorMapper.xml

@@ -0,0 +1,5 @@
1
+<?xml version="1.0" encoding="UTF-8" ?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3
+<mapper namespace="">
4
+    <!--namespace根据自己需要创建的的mapper的路径和名称填写-->
5
+</mapper>

+ 13 - 1
src/test/java/com/example/springbootdemo/SpringbootdemoApplicationTests.java

@@ -1,13 +1,25 @@
1 1
 package com.example.springbootdemo;
2 2
 
3
+import lombok.extern.slf4j.Slf4j;
3 4
 import org.junit.jupiter.api.Test;
5
+import org.springframework.beans.factory.annotation.Autowired;
4 6
 import org.springframework.boot.test.context.SpringBootTest;
7
+import org.springframework.jdbc.core.JdbcTemplate;
5 8
 
9
+import javax.sql.DataSource;
10
+
11
+@Slf4j
6 12
 @SpringBootTest
7 13
 class SpringbootdemoApplicationTests {
8
-
14
+    @Autowired
15
+    JdbcTemplate jdbcTemplate;
16
+    @Autowired
17
+    DataSource dataSource;
9 18
     @Test
10 19
     void contextLoads() {
20
+        Long count = jdbcTemplate.queryForObject("select count(*) from wb_tbl_color",Long.class);
21
+        log.info("记录 all count {}",count);
22
+        log.info("数据源类型 {}",dataSource.getClass());
11 23
     }
12 24
 
13 25
 }