002-cloud8-8001子模組-改pom-建yml-建庫建表
阿新 • • 發佈:2020-07-27
1 <dependencies> 2 3 <!--web/actuator這兩個一般一起使用,寫在一起--> 4 <dependency> 5 <groupId>org.springframework.boot</groupId> 6 <artifactId>spring-boot-starter-web</artifactId> 7 </dependency> 8 <!--監控--> 98001專案-pom依賴<dependency> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-actuator</artifactId> 12 </dependency> 13 14 <!--Mybatis和SpringBoot的整合--> 15 <dependency> 16 <groupId>org.mybatis.spring.boot</groupId> 17<artifactId>mybatis-spring-boot-starter</artifactId> 18 </dependency> 19 20 <dependency> 21 <groupId>com.alibaba</groupId> 22 <artifactId>druid</artifactId> 23 <!--如果沒寫版本,從父層面找,找到了就直接用,全域性統一--> 24</dependency> 25 26 <!--mysql-connector-java--> 27 <dependency> 28 <groupId>mysql</groupId> 29 <artifactId>mysql-connector-java</artifactId> 30 </dependency> 31 <!--jdbc--> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-jdbc</artifactId> 35 </dependency> 36 <!--熱部署--> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-devtools</artifactId> 40 <scope>runtime</scope> 41 <optional>true</optional> 42 </dependency> 43 <dependency> 44 <groupId>org.projectlombok</groupId> 45 <artifactId>lombok</artifactId> 46 <optional>true</optional> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-starter-test</artifactId> 51 <scope>test</scope> 52 </dependency> 53 </dependencies>
這裡,需要先去建立一個db2019的資料庫,設定的名字密碼要保持一致。推薦用navicat軟體,輕便簡單。
1 # 微服務埠號 2 server: 3 port: 8001 4 5 # 微服務名稱 6 spring: 7 application: 8 name: cloud-payment-service 9 10 datasource: 11 type: com.alibaba.druid.pool.DruidDataSource # 資料來源 12 driver-class-name: com.mysql.jdbc.Driver # mysql驅動包 這裡報錯就在左側External Libraries欄找mysql下的Driver 13 url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false 14 username: root 15 password: 123456 16 17 18 mybatis: 19 mapper-locations: classpath:mapper/*.xml # 掃描類路徑下mapper資料夾下的.xml配置檔案 20 type-aliases-package: com.atguigu.springcloud.entities # 該包所有Entity類,取預設別名yml檔案內容
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 4 @SpringBootApplication 5 public class PaymentMain8001 { 6 public static void main(String[] args) { 7 SpringApplication.run(PaymentMain8001.class,args); 8 } 9 10 }8001啟動類
CREATE TABLE payment ( id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', serial VARCHAR(200) DEFAULT'', PRIMARY KEY(id) )ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8navicat建表
這裡要注意的是,有些工具是需要單引號,有些是需要雙引號,而navicat裡,欄位,表名都是不需要引號的。
1 import lombok.AllArgsConstructor; 2 import lombok.Data; 3 import lombok.NoArgsConstructor; 4 5 import java.io.Serializable; 6 7 /** 先在IDEA搜lombok外掛安裝,否則這三個用不了*/ 8 @Data 9 @AllArgsConstructor 10 @NoArgsConstructor 11 public class Payment implements Serializable { 12 private Long id; //Long 非long 13 private String serial; 14 }Payment類
1 package com.atguigu.springcloud.entities; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 8 @Data 9 @AllArgsConstructor 10 @NoArgsConstructor 11 public class CommonResult<T> { 12 13 //404 not_found:Integer String 14 private Integer code; 15 private String message; 16 17 //這不是針對某一個實體類的Josn串封裝類 18 private T data; 19 20 //當T是null時,定義一個兩個引數的構造 21 public CommonResult(Integer code, String message){ 22 this(code, message, null); //資料體 預設為null 23 } 24 25 }CommonResult
1 /** 2 * 如果是用mybatis 建議用Mapper註解, repository插入時容易翻車 3 */ 4 @Mapper 5 public interface PaymentDao { 6 // 常用的讀寫方法 7 public int create(Payment payment); 8 public Payment getPaymentById(@Param("id") Long id); 9 10 }PaymentDao介面
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.atguigu.springcloud.dao.PaymentDao"> 7 8 9 </mapper>mapper的xml(初始)
1 <!--這裡 Payment報錯 可以寫全路徑。 似乎是yml中的配置未生效 不確定是否還有其他問題--> 2 <insert id="create" parameterType="com.atguigu.springcloud.entities.Payment" useGeneratedKeys="true" keyProperty="id"> 3 insert into payment(serial) values(#{serial}); 4 </insert> 5 6 <!--定義一個結果接和實體類的對映表--> 7 <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment"> 8 <id column="id" property="id" jdbcType="BIGINT"/> 9 <id column="serial" property="serial" jdbcType="VARCHAR"/> 10 </resultMap> 11 12 <!-- 這裡是long Long會報錯 不知道是為何--> 13 <select id="getPaymentById" parameterType="long" resultMap="BaseResultMap"> 14 select * from payment where id=#{id}; 15 </select>兩個方法對應的sql以及問題處理
這裡 Payment 和Long 報錯 改一下就好,具體見程式碼。
【不過後期測試,即使Payment標紅,專案也可以啟動,也能正常使用該SQL給資料庫插入,暫且擱置】
import com.atguigu.springcloud.entities.Payment; import org.apache.ibatis.annotations.Param; public interface PaymentService { public int create( Payment payment); public Payment getPaymentById(@Param("id") Long id); } import com.atguigu.springcloud.dao.PaymentDao; import com.atguigu.springcloud.entities.Payment; import com.atguigu.springcloud.service.PaymentService; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; public int create( Payment payment){ return paymentDao.create(payment); } public Payment getPaymentById(Long id){ return paymentDao.getPaymentById(id); } }service層介面和實現類
實現類需要新增@Service註解
1 package com.atguigu.springcloud.controller; 2 3 import com.atguigu.springcloud.entities.CommonResult; 4 import com.atguigu.springcloud.entities.Payment; 5 import com.atguigu.springcloud.service.PaymentService; 6 import lombok.extern.slf4j.Slf4j; 7 import org.springframework.web.bind.annotation.*; 8 9 import javax.annotation.Resource; 10 11 @RestController 12 @Slf4j 13 public class PaymentController { 14 @Resource 15 private PaymentService paymentService; 16 17 @PostMapping(value = "/payment/create") 18 public CommonResult create(Payment payment) { 19 int result = paymentService.create(payment); 20 log.info("*****插入結果:" + result); 21 if (result > 0) { 22 return new CommonResult(200, "插入資料庫成功", result); 23 } 24 return new CommonResult(444, "插入資料庫失敗", null); 25 } 26 27 @GetMapping(value = "/payment/get/{id}") //寫操作POST 讀get 還有pull等等 28 public CommonResult getPaymentById(@PathVariable("id") Long id) { 29 30 Payment payment = paymentService.getPaymentById(id); 31 log.info("*****查詢結果:" + payment); 32 if (payment != null) { 33 return new CommonResult(200, "查詢資料庫成功", payment); 34 } else { 35 return new CommonResult(444, "查詢ID:" + id + "沒有對應記錄", null); 36 } 37 } 38 39 }controller類
@RestController=@Controller+@ResponseBody @SLF4J是日誌 這裡有使用到RESFful介面風格設計思想。
關於瀏覽器只有get方式:建議下載utools,它裡面有很多開發必備的小外掛,包括postman。內網穿透等等。簡直神器。
注:我的報錯【At least one base package must be specified】,參考【MapperScan註解的使用】
這裡,似乎是yml中配置的路徑並未生效的緣故。之前在【Payment】那裡也報錯了。也許是IDEA自動識別的問題? 後期深入瞭解。