ソースを参照

配置双数据源

zhuoning 4 年 前
コミット
16a33c2fc8

+ 2 - 0
shiwan/env/src/main/java/com/huimv/env/EnvApplication.java

@@ -2,7 +2,9 @@ package com.huimv.env;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
+@EnableScheduling
 @SpringBootApplication
 public class EnvApplication {
 

+ 46 - 0
shiwan/env/src/main/java/com/huimv/env/controller/ZsDaController.java

@@ -0,0 +1,46 @@
+package com.huimv.env.controller;
+
+import com.huimv.env.service.IEnvService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+//@RestController("/")
+@RestController
+@RequestMapping(value = "/da")
+public class ZsDaController {
+
+    @Autowired
+    private  IEnvService envService;
+
+    /**
+     * @Method      : saveAccountGroup
+     * @Description : 
+     * @Params      : []
+     * @Return      : void
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2020/12/26       
+     * @Time        : 18:12
+     */
+    @RequestMapping("/getZsDa")
+    public void getZsDa(){
+        //
+        envService.getEnv();
+    }
+
+    @RequestMapping("/getZsDa2")
+    public void getZsDa2(){
+        //
+        envService.getEnv2();
+    }
+}

+ 444 - 0
shiwan/env/src/main/java/com/huimv/env/dao/entity/DaZsdaEntity.java

@@ -0,0 +1,444 @@
+package com.huimv.env.dao.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+/**
+ * 档案-猪舍信息
+ */
+//@Proxy(lazy = false)
+@Entity
+@Table(name = "da_zsda")
+public class DaZsdaEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * ID
+     */
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "ID", nullable = false)
+    private Integer ID;
+
+    /**
+     * 牧场编号
+     */
+    @Column(name = "MCID", nullable = false)
+    private Integer MCID;
+
+    /**
+     * 猪舍名称
+     */
+    @Column(name = "ZSMC")
+    private String ZSMC;
+
+    /**
+     * 采食区1
+     */
+    @Column(name = "CSQ1")
+    private String CSQ1;
+
+    /**
+     * 采食区2
+     */
+    @Column(name = "CSQ2")
+    private String CSQ2;
+
+    /**
+     * 分离区
+     */
+    @Column(name = "FLQ")
+    private String FLQ;
+
+    /**
+     * 当前栏期
+     */
+    @Column(name = "DQLQ")
+    private String DQLQ;
+
+    /**
+     * 养殖阶段
+     */
+    @Column(name = "YZJD")
+    private String YZJD;
+
+    /**
+     * 管理员ID
+     */
+    @Column(name = "GLY")
+    private Integer GLY;
+
+    /**
+     * 管理员姓名
+     */
+    @Column(name = "GLYXM")
+    private String GLYXM;
+
+    /**
+     * 修改人
+     */
+    @Column(name = "XGR")
+    private String XGR;
+
+    /**
+     * 修改日期
+     */
+    @Column(name = "XGSJ")
+    private Timestamp XGSJ;
+
+    /**
+     * 作废标识
+     */
+    @Column(name = "ZFBZ")
+    private Integer ZFBZ;
+
+    /**
+     * 猪舍标志
+     */
+    @Column(name = "ZSBZ")
+    private Integer ZSBZ;
+
+    /**
+     * 所属区域
+     */
+    @Column(name = "SSQY")
+    private Integer SSQY;
+
+    /**
+     * 料槽标志 1-智能料槽 2-普通料槽
+     */
+    @Column(name = "LCBZ")
+    private Integer LCBZ;
+
+    /**
+     * 环控标志 1-智能环控 2-普通环控
+     */
+    @Column(name = "HKBZ")
+    private Integer HKBZ;
+
+    /**
+     * 置顶标志 0-不置顶 1-置顶
+     */
+    @Column(name = "ZDBZ")
+    private Integer ZDBZ;
+
+    /**
+     * 猪舍区域
+     */
+    @Column(name = "ZSQY")
+    private Integer ZSQY;
+
+    /**
+     * 视频监控
+     */
+    @Column(name = "SPJK")
+    private String SPJK;
+
+    /**
+     * ID
+     */
+    public void setID(Integer ID) {
+        this.ID = ID;
+    }
+
+    /**
+     * ID
+     */
+    public Integer getID() {
+        return ID;
+    }
+
+    /**
+     * 牧场编号
+     */
+    public void setMCID(Integer MCID) {
+        this.MCID = MCID;
+    }
+
+    /**
+     * 牧场编号
+     */
+    public Integer getMCID() {
+        return MCID;
+    }
+
+    /**
+     * 猪舍名称
+     */
+    public void setZSMC(String ZSMC) {
+        this.ZSMC = ZSMC;
+    }
+
+    /**
+     * 猪舍名称
+     */
+    public String getZSMC() {
+        return ZSMC;
+    }
+
+    /**
+     * 采食区1
+     */
+    public void setCSQ1(String CSQ1) {
+        this.CSQ1 = CSQ1;
+    }
+
+    /**
+     * 采食区1
+     */
+    public String getCSQ1() {
+        return CSQ1;
+    }
+
+    /**
+     * 采食区2
+     */
+    public void setCSQ2(String CSQ2) {
+        this.CSQ2 = CSQ2;
+    }
+
+    /**
+     * 采食区2
+     */
+    public String getCSQ2() {
+        return CSQ2;
+    }
+
+    /**
+     * 分离区
+     */
+    public void setFLQ(String FLQ) {
+        this.FLQ = FLQ;
+    }
+
+    /**
+     * 分离区
+     */
+    public String getFLQ() {
+        return FLQ;
+    }
+
+    /**
+     * 当前栏期
+     */
+    public void setDQLQ(String DQLQ) {
+        this.DQLQ = DQLQ;
+    }
+
+    /**
+     * 当前栏期
+     */
+    public String getDQLQ() {
+        return DQLQ;
+    }
+
+    /**
+     * 养殖阶段
+     */
+    public void setYZJD(String YZJD) {
+        this.YZJD = YZJD;
+    }
+
+    /**
+     * 养殖阶段
+     */
+    public String getYZJD() {
+        return YZJD;
+    }
+
+    /**
+     * 管理员ID
+     */
+    public void setGLY(Integer GLY) {
+        this.GLY = GLY;
+    }
+
+    /**
+     * 管理员ID
+     */
+    public Integer getGLY() {
+        return GLY;
+    }
+
+    /**
+     * 管理员姓名
+     */
+    public void setGLYXM(String GLYXM) {
+        this.GLYXM = GLYXM;
+    }
+
+    /**
+     * 管理员姓名
+     */
+    public String getGLYXM() {
+        return GLYXM;
+    }
+
+    /**
+     * 修改人
+     */
+    public void setXGR(String XGR) {
+        this.XGR = XGR;
+    }
+
+    /**
+     * 修改人
+     */
+    public String getXGR() {
+        return XGR;
+    }
+
+    /**
+     * 修改日期
+     */
+    public void setXGSJ(Timestamp XGSJ) {
+        this.XGSJ = XGSJ;
+    }
+
+    /**
+     * 修改日期
+     */
+    public Timestamp getXGSJ() {
+        return XGSJ;
+    }
+
+    /**
+     * 作废标识
+     */
+    public void setZFBZ(Integer ZFBZ) {
+        this.ZFBZ = ZFBZ;
+    }
+
+    /**
+     * 作废标识
+     */
+    public Integer getZFBZ() {
+        return ZFBZ;
+    }
+
+    /**
+     * 猪舍标志
+     */
+    public void setZSBZ(Integer ZSBZ) {
+        this.ZSBZ = ZSBZ;
+    }
+
+    /**
+     * 猪舍标志
+     */
+    public Integer getZSBZ() {
+        return ZSBZ;
+    }
+
+    /**
+     * 所属区域
+     */
+    public void setSSQY(Integer SSQY) {
+        this.SSQY = SSQY;
+    }
+
+    /**
+     * 所属区域
+     */
+    public Integer getSSQY() {
+        return SSQY;
+    }
+
+    /**
+     * 料槽标志 1-智能料槽 2-普通料槽
+     */
+    public void setLCBZ(Integer LCBZ) {
+        this.LCBZ = LCBZ;
+    }
+
+    /**
+     * 料槽标志 1-智能料槽 2-普通料槽
+     */
+    public Integer getLCBZ() {
+        return LCBZ;
+    }
+
+    /**
+     * 环控标志 1-智能环控 2-普通环控
+     */
+    public void setHKBZ(Integer HKBZ) {
+        this.HKBZ = HKBZ;
+    }
+
+    /**
+     * 环控标志 1-智能环控 2-普通环控
+     */
+    public Integer getHKBZ() {
+        return HKBZ;
+    }
+
+    /**
+     * 置顶标志 0-不置顶 1-置顶
+     */
+    public void setZDBZ(Integer ZDBZ) {
+        this.ZDBZ = ZDBZ;
+    }
+
+    /**
+     * 置顶标志 0-不置顶 1-置顶
+     */
+    public Integer getZDBZ() {
+        return ZDBZ;
+    }
+
+    /**
+     * 猪舍区域
+     */
+    public void setZSQY(Integer ZSQY) {
+        this.ZSQY = ZSQY;
+    }
+
+    /**
+     * 猪舍区域
+     */
+    public Integer getZSQY() {
+        return ZSQY;
+    }
+
+    /**
+     * 视频监控
+     */
+    public void setSPJK(String SPJK) {
+        this.SPJK = SPJK;
+    }
+
+    /**
+     * 视频监控
+     */
+    public String getSPJK() {
+        return SPJK;
+    }
+
+    @Override
+    public String toString() {
+        return "DaZsdaEntity{" +
+                "ID=" + ID + '\'' +
+                "MCID=" + MCID + '\'' +
+                "ZSMC=" + ZSMC + '\'' +
+                "CSQ1=" + CSQ1 + '\'' +
+                "CSQ2=" + CSQ2 + '\'' +
+                "FLQ=" + FLQ + '\'' +
+                "DQLQ=" + DQLQ + '\'' +
+                "YZJD=" + YZJD + '\'' +
+                "GLY=" + GLY + '\'' +
+                "GLYXM=" + GLYXM + '\'' +
+                "XGR=" + XGR + '\'' +
+                "XGSJ=" + XGSJ + '\'' +
+                "ZFBZ=" + ZFBZ + '\'' +
+                "ZSBZ=" + ZSBZ + '\'' +
+                "SSQY=" + SSQY + '\'' +
+                "LCBZ=" + LCBZ + '\'' +
+                "HKBZ=" + HKBZ + '\'' +
+                "ZDBZ=" + ZDBZ + '\'' +
+                "ZSQY=" + ZSQY + '\'' +
+                "SPJK=" + SPJK + '\'' +
+                '}';
+    }
+}

+ 15 - 0
shiwan/env/src/main/java/com/huimv/env/dao/repo/DaZsdaEntityRepository.java

@@ -0,0 +1,15 @@
+package com.huimv.env.dao.repo;
+
+import com.huimv.env.dao.entity.DaZsdaEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.Optional;
+
+
+public interface DaZsdaEntityRepository extends JpaRepository<DaZsdaEntity, Integer>, JpaSpecificationExecutor<DaZsdaEntity> {
+
+    @Query(nativeQuery = true , value = "SELECT * FROM da_zsda WHERE id=?1")
+    Optional<DaZsdaEntity> findById2(int i);
+}

+ 33 - 0
shiwan/env/src/main/java/com/huimv/env/scheduler/Scheduler.java

@@ -0,0 +1,33 @@
+package com.huimv.env.scheduler;
+
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class Scheduler {
+    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
+
+    //每隔2秒执行一次
+//    @Scheduled(fixedRate = 2000)
+//    @Scheduled(cron = "02 * * * * *")
+    public void testTasks1() {
+        System.out.println("定时任务执行时间:" + dateFormat.format(new Date()));
+    }
+
+    //每天3:05执行
+//    @Scheduled(cron = "0 05 03 ? * *")
+    public void testTasks2() {
+//        System.out.println("定时任务执行时间:" + dateFormat.format(new Date()));
+    }
+}

+ 14 - 0
shiwan/env/src/main/java/com/huimv/env/service/IEnvService.java

@@ -0,0 +1,14 @@
+package com.huimv.env.service;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+public interface IEnvService {
+    public void getEnv();
+    public void getEnv2();
+}

+ 43 - 0
shiwan/env/src/main/java/com/huimv/env/service/impl/EnvServiceImpl.java

@@ -0,0 +1,43 @@
+package com.huimv.env.service.impl;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.huimv.env.dao.entity.DaZsdaEntity;
+import com.huimv.env.dao.repo.DaZsdaEntityRepository;
+import com.huimv.env.service.IEnvService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Optional;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Service
+@DS("slave")
+public class EnvServiceImpl implements IEnvService {
+    @Autowired
+    private DaZsdaEntityRepository daZsdaEntityRepo;
+
+    @Override
+    @DS("slave")
+    public void getEnv() {
+//        DaZsdaEntity daZsdaEntity = daZsdaEntityRepo.getOne(454);
+        Optional<DaZsdaEntity> daZsdaEntity = daZsdaEntityRepo.findById2(454);
+        System.out.println("daZsdaEntity>>"+daZsdaEntity);
+
+
+    }
+
+    @Override
+    @DS("master")
+    public void getEnv2() {
+//        DaZsdaEntity daZsdaEntity = daZsdaEntityRepo.findById(454);
+        Optional<DaZsdaEntity> daZsdaEntity = daZsdaEntityRepo.findById(454);
+        System.out.println("daZsdaEntity>>"+daZsdaEntity);
+    }
+}

+ 31 - 0
shiwan/env/src/main/resources/application-prod.yml

@@ -0,0 +1,31 @@
+server:
+  port: 8085
+spring:
+  #给项目来个名字
+  application:
+    name: env
+
+  datasource:
+    url: jdbc:mysql://192.168.1.7:3306/core_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+    username: root
+    password: hm123456
+    driver-class-name: com.mysql.cj.jdbc.Driver
+  jpa:
+    hibernate:
+      ddl-auto: update
+    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
+    #    dialect: org.hibernate.dialect.MySQL8Dialect
+    open-in-view: false
+
+  redis:
+    database: 0
+    host: 119.3.84.55
+    port: 6379
+    password: hm123456
+    jedis:
+      pool:
+        max-active: 20
+        max-wait: -1
+        max-idle: 10
+        min-idle: 0
+    timeout: 5000

+ 39 - 0
shiwan/env/src/main/resources/application-test.yml

@@ -0,0 +1,39 @@
+server:
+  port: 8085
+spring:
+  #给项目来个名字
+  application:
+    name: env
+
+  datasource:
+    dynamic:
+      datasource:
+        master:
+          url: jdbc:mysql://192.168.1.7:3306/core_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+          username: root
+          password: hm123456
+          driver-class-name: com.mysql.cj.jdbc.Driver
+        slave:
+          url: jdbc:mysql://192.168.1.7:3306/core_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+          username: root
+          password: hm123456
+          driver-class-name: com.mysql.cj.jdbc.Driver
+  jpa:
+    hibernate:
+      ddl-auto: update
+    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
+    #    dialect: org.hibernate.dialect.MySQL8Dialect
+    open-in-view: true
+
+    #  redis:
+#    database: 0
+#    host: 119.3.84.55
+#    port: 6379
+#    password: hm123456
+#    jedis:
+#      pool:
+#        max-active: 20
+#        max-wait: -1
+#        max-idle: 10
+#        min-idle: 0
+#    timeout: 5000

+ 1 - 1
shiwan/env/src/main/resources/application.properties

@@ -1 +1 @@
-
+spring.profiles.active=test

+ 80 - 0
shiwan/env/src/test/java/com/huimv/env/controller/UserTest.java

@@ -0,0 +1,80 @@
+package com.huimv.env.controller;
+
+import com.huimv.env.service.IEnvService;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+
+@RunWith(SpringRunner.class)
+//@SpringBootTest(classes = GeneratorApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@SpringBootTest
+public class UserTest {
+    @Test
+    public void contextLoads() {
+    }
+
+    private MockMvc mockMvc;
+
+    @Autowired
+    private WebApplicationContext wac;
+
+    @Before // 在测试开始前初始化工作
+    public void setup() {
+        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
+    }
+
+    //验证controller是否正常响应并打印返回结果
+    @Test
+    public void getSearchAlarm() throws Exception {
+        //加入类中的url
+        mockMvc.perform(MockMvcRequestBuilders.get("/alarm/searchAlarm").accept(MediaType.APPLICATION_JSON))
+                .andExpect(MockMvcResultMatchers.status().isOk())
+                .andDo(MockMvcResultHandlers.print())
+                .andReturn();
+    }
+
+    @Test
+    public void testDownload() throws Exception {
+        //Get请求
+        MvcResult result = mockMvc.perform(get("/da/getZsDa").content("填写参数"))
+                .andExpect(status().isOk())
+                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
+                .andReturn();
+
+        System.out.println(result.getResponse().getContentAsString());
+    }
+
+    @Autowired
+    private IEnvService envService;
+
+    @Test
+    public void test1(){
+        envService.getEnv();
+        envService.getEnv2();
+    }
+
+}