瀏覽代碼

mybatis task

zhangxiaoyu 3 年之前
父節點
當前提交
26e7afb193

+ 5 - 9
pom.xml

@@ -25,10 +25,6 @@
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>
32 28
         <dependency>
33 29
             <groupId>org.projectlombok</groupId>
34 30
             <artifactId>lombok</artifactId>
@@ -38,16 +34,16 @@
38 34
             <artifactId>spring-boot-starter-test</artifactId>
39 35
             <scope>test</scope>
40 36
         </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 37
         <dependency>
47 38
             <groupId>com.alibaba</groupId>
48 39
             <artifactId>druid-spring-boot-starter</artifactId>
49 40
             <version>1.1.10</version>
50 41
         </dependency>
42
+        <dependency>
43
+            <groupId>com.baomidou</groupId>
44
+            <artifactId>mybatis-plus-boot-starter</artifactId>
45
+            <version>3.4.1</version>
46
+        </dependency>
51 47
         <dependency>
52 48
             <groupId>mysql</groupId>
53 49
             <artifactId>mysql-connector-java</artifactId>

+ 8 - 7
src/main/java/com/example/springbootdemo/controller/HelloController.java

@@ -12,9 +12,7 @@ import com.example.springbootdemo.zxyTeST.sql.entity.Color;
12 12
 import org.springframework.beans.factory.annotation.Autowired;
13 13
 import org.springframework.dao.EmptyResultDataAccessException;
14 14
 import org.springframework.jdbc.core.JdbcTemplate;
15
-import org.springframework.web.bind.annotation.RequestMapping;
16
-import org.springframework.web.bind.annotation.RequestParam;
17
-import org.springframework.web.bind.annotation.RestController;
15
+import org.springframework.web.bind.annotation.*;
18 16
 
19 17
 import java.util.List;
20 18
 
@@ -59,10 +57,13 @@ public class HelloController {
59 57
 
60 58
     }
61 59
     @RequestMapping("mybatis1")
62
-    public List<FootScanGroup> getFootScanGroup(@RequestParam("foot_scan_group_id") Long foot_scan_group_id) {
63
-       List <FootScanGroup> ff = footScanGroupService.findByFootScanGroupId(foot_scan_group_id);
64
-        return ff;
65
-
60
+    public FootScanGroup getFootScanGroup(@RequestParam("foot_scan_group_id") Long foot_scan_group_id) {
61
+        return footScanGroupService.findByFootScanGroupId(foot_scan_group_id);
62
+    }
63
+    @RequestMapping(value = "/mybatis2",method = RequestMethod.POST)
64
+    public FootScanGroup addFootScanGroup(@RequestBody FootScanGroup footScanGroup) {
65
+        footScanGroupService.addFootScanGroup(footScanGroup);
66
+        return footScanGroup;
66 67
     }
67 68
 
68 69
 }

+ 25 - 0
src/main/java/com/example/springbootdemo/controller/MybatisPlusController.java

@@ -0,0 +1,25 @@
1
+package com.example.springbootdemo.controller;
2
+
3
+import com.example.springbootdemo.entity.Device;
4
+import com.example.springbootdemo.mapper.DeviceMapper;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.RestController;
8
+
9
+import java.util.List;
10
+
11
+/**
12
+ * @author zhangxiaoyu
13
+ * @version 1.0
14
+ * @date 2021/6/1 下午3:24
15
+ */
16
+@RestController
17
+public class MybatisPlusController {
18
+    @Autowired
19
+    DeviceMapper deviceMapper;
20
+    @RequestMapping("/mybatisPlusSelect")
21
+    public List<Device> getDeviceList() {
22
+        List<Device> deviceList = deviceMapper.selectList(null);
23
+        return deviceList;
24
+    }
25
+}

+ 30 - 0
src/main/java/com/example/springbootdemo/entity/Device.java

@@ -0,0 +1,30 @@
1
+package com.example.springbootdemo.entity;
2
+
3
+import lombok.Data;
4
+
5
+import javax.persistence.GeneratedValue;
6
+import javax.persistence.Id;
7
+import javax.persistence.Table;
8
+import java.io.Serializable;
9
+import java.sql.Timestamp;
10
+
11
+/**
12
+ * @author zhangxiaoyu
13
+ * @version 1.0
14
+ * @date 2021/6/1 下午3:50
15
+ */
16
+@Data
17
+@Table(name = "wb_tbl_device")
18
+public class Device implements Serializable {
19
+    @Id
20
+    @GeneratedValue
21
+    private Long deviceId;
22
+    private String deviceCode;
23
+    private Long companyId;
24
+    private Timestamp runDateUtime;
25
+    private Timestamp utime;
26
+    private Timestamp ctime;
27
+    private String extraInfo;
28
+    private String deviceSecret;
29
+    private Long deviceType;
30
+}

+ 3 - 0
src/main/java/com/example/springbootdemo/entity/FootScanGroup.java

@@ -1,6 +1,7 @@
1 1
 package com.example.springbootdemo.entity;
2 2
 
3 3
 
4
+import com.fasterxml.jackson.annotation.JsonProperty;
4 5
 import lombok.Data;
5 6
 
6 7
 import javax.persistence.GeneratedValue;
@@ -19,6 +20,8 @@ public class FootScanGroup implements Serializable {
19 20
     @Id
20 21
     @GeneratedValue
21 22
     private Long footScanGroupId;
23
+    @JsonProperty("user_id")
22 24
     private Long userId;
25
+    @JsonProperty("user_name")
23 26
     private String userName;
24 27
 }

+ 14 - 0
src/main/java/com/example/springbootdemo/mapper/DeviceMapper.java

@@ -0,0 +1,14 @@
1
+package com.example.springbootdemo.mapper;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.example.springbootdemo.entity.Device;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+/**
8
+ * @author zhangxiaoyu
9
+ * @version 1.0
10
+ * @date 2021/6/1 下午3:56
11
+ */
12
+@Mapper
13
+public interface DeviceMapper extends BaseMapper<Device> {
14
+}

+ 7 - 5
src/main/java/com/example/springbootdemo/mapper/FootScanGroupMapper.java

@@ -1,10 +1,10 @@
1 1
 package com.example.springbootdemo.mapper;
2 2
 
3 3
 import com.example.springbootdemo.entity.FootScanGroup;
4
+import org.apache.ibatis.annotations.Insert;
4 5
 import org.apache.ibatis.annotations.Mapper;
5
-import org.apache.ibatis.annotations.Param;
6
+import org.apache.ibatis.annotations.Options;
6 7
 import org.apache.ibatis.annotations.Select;
7
-import org.springframework.stereotype.Repository;
8 8
 
9 9
 import java.util.List;
10 10
 
@@ -14,8 +14,10 @@ import java.util.List;
14 14
  * @date 2021/6/1 上午9:48
15 15
  */
16 16
 @Mapper
17
-@Repository
18 17
 public interface FootScanGroupMapper {
19
-    @Select("SELECT foot_scan_group_id,user_id,user_name FROM wb_tbl_foot_scan_group WHERE  foot_scan_group_id = #{footScanGroupId}")
20
-    List <FootScanGroup> findByFootScanGroupId(Long footScanGroupId);
18
+    @Select("SELECT foot_scan_group_id,user_id,user_name FROM wb_tbl_foot_scan_group WHERE  foot_scan_group_id = #{id}")
19
+    FootScanGroup findByFootScanGroupId(Long id);
20
+    @Insert("insert into wb_tbl_foot_scan_group (user_id,user_name) values (#{userId},#{userName})")
21
+    @Options(useGeneratedKeys = true,keyProperty = "footScanGroupId")
22
+    void addFootScanGroup(FootScanGroup footScanGroup);
21 23
 }

+ 6 - 1
src/main/java/com/example/springbootdemo/service/FootScanGroupService.java

@@ -11,8 +11,13 @@ import java.util.List;
11 11
 public class FootScanGroupService {
12 12
     @Autowired
13 13
     FootScanGroupMapper footScanGroupMapper;
14
-    public List<FootScanGroup> findByFootScanGroupId(Long foot_scan_group_id) {
14
+    public FootScanGroup findByFootScanGroupId(Long foot_scan_group_id) {
15 15
         return footScanGroupMapper.findByFootScanGroupId(foot_scan_group_id);
16 16
     }
17 17
 
18
+    public void addFootScanGroup(FootScanGroup footScanGroup) {
19
+        footScanGroupMapper.addFootScanGroup(footScanGroup);
20
+    }
21
+
22
+
18 23
 }

+ 1 - 1
src/main/resources/application-dev.yml

@@ -27,6 +27,6 @@ spring:
27 27
   freemarker:
28 28
     cache: false
29 29
 mybatis:
30
-  mapper-locations: classpath:mapper/*.xml
30
+#  mapper-locations: classpath:mapper/*.xml
31 31
   configuration:
32 32
     map-underscore-to-camel-case: true