Pārlūkot izejas kodu

数据库优化

xiongzhu 3 gadi atpakaļ
vecāks
revīzija
838d4d0ea5

+ 12 - 1
src/main/java/com/izouma/nineth/domain/TestClass.java

@@ -6,12 +6,23 @@ import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
 
 @Data
 @Entity
 @AllArgsConstructor
 @NoArgsConstructor
 @Builder
-public class TestClass extends BaseEntity {
+public class TestClass {
+    public TestClass(String name) {
+        this.name = name;
+    }
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
     private String name;
 }

+ 3 - 7
src/main/java/com/izouma/nineth/repo/TestClassRepo.java

@@ -3,14 +3,10 @@ package com.izouma.nineth.repo;
 import com.izouma.nineth.domain.TestClass;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
-import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
 
-import javax.transaction.Transactional;
-
 public interface TestClassRepo extends JpaRepository<TestClass, Long>, JpaSpecificationExecutor<TestClass> {
-    @Query("update TestClass t set t.del = true where t.id = ?1")
-    @Modifying
-    @Transactional
-    void softDelete(Long id);
+
+    @Query(value = "insert into test_class (name) values (?1)", nativeQuery = true)
+    public void nativeSave(String name);
 }

+ 1 - 0
src/main/java/com/izouma/nineth/security/WebSecurityConfig.java

@@ -110,6 +110,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/druid/**").permitAll()
                 .antMatchers("/identityAuth/autoAuth").permitAll()
                 .antMatchers("/statistic/weekTop").permitAll()
+                .antMatchers("/testClass/**").permitAll()
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to

+ 9 - 15
src/main/java/com/izouma/nineth/web/TestClassController.java

@@ -1,10 +1,10 @@
 package com.izouma.nineth.web;
+
 import com.izouma.nineth.domain.TestClass;
-import com.izouma.nineth.service.TestClassService;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.TestClassRepo;
-import com.izouma.nineth.utils.ObjUtils;
+import com.izouma.nineth.service.TestClassService;
 import com.izouma.nineth.utils.excel.ExcelUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.data.domain.Page;
@@ -19,19 +19,18 @@ import java.util.List;
 @AllArgsConstructor
 public class TestClassController extends BaseController {
     private TestClassService testClassService;
-    private TestClassRepo testClassRepo;
+    private TestClassRepo    testClassRepo;
 
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/save")
-    public TestClass save(@RequestBody TestClass record) {
-        if (record.getId() != null) {
-            TestClass orig = testClassRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
-            ObjUtils.merge(orig, record);
-            return testClassRepo.save(orig);
-        }
-        return testClassRepo.save(record);
+    public TestClass save() {
+        return testClassRepo.save(new TestClass("aaa"));
     }
 
+    @PostMapping("/save1")
+    public void save1() {
+        testClassRepo.nativeSave("aaa");
+    }
 
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/all")
@@ -44,11 +43,6 @@ public class TestClassController extends BaseController {
         return testClassRepo.findById(id).orElseThrow(new BusinessException("无记录"));
     }
 
-    @PostMapping("/del/{id}")
-    public void del(@PathVariable Long id) {
-        testClassRepo.softDelete(id);
-    }
-
     @GetMapping("/excel")
     @ResponseBody
     public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {