携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第17天,点击查看活动详情
各位老铁,今天我们来学习一下good-price模块的开发,通过商品ID来查找对应价格
1.首先看一下我为大家准备好的数据表结构
表数据填充
2.在java目录创建com.imooc.good包和创建启动类GoodPriceApplication
@SpringBootApplication
public class GoodPriceApplication {
public static void main(String[] args) {
SpringApplication.run(GoodPriceApplication.class,args);
}
}
3.新建并配置application.properties文件
#端口号
server.port=8083
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/course_practice?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
#日志
logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}
#应用明
spring.application.name=good-price
mybatis.configuration.map-underscore-to-camel-case=true
4.配置3个依赖和1个插件,可以直接从上一节good-list拷贝一份过来
<dependencies>
<!-- 1.引入web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 2.引入mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 3.引入mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<!-- 4.引入plugin插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5.在com.imooc.good下新建entity包,并且创建GoodPrice实体类(根据数据表good_price的字段类型分别设置对应的getter和setter方法,重写toString方法,和继承序列化)
public class GoodPrice implements Serializable {
Integer id;
Integer goodId;
Integer price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
//后面还有道多代码,就不一一展示了
6.在com.imooc.good下新建dao包,并且创建GoodPriceMapper接口
@Mapper
@Repository
public interface GoodPriceMapper {
@Select("select * from good_price where good_id=#{goodId}")
GoodPrice findGoodPrice(Integer goodId);
}
注意点:参数传参需要是用#{字段名}
7.在com.imooc.good下新建service包,并且创建GoodPriceService接口
public interface GoodPriceService {
GoodPrice getGoodPrice(Integer goodId);
}
8.在service包下新建impl包,并且创建GoodPriceServiceImpl
@Service
public class GoodPriceServiceImpl implements GoodPriceService {
@Autowired
GoodPriceMapper goodPriceMapper;
@Override
public GoodPrice getGoodPrice(Integer goodId) {
return goodPriceMapper.findGoodPrice(goodId);
}
}
8.在com.imooc.good下新建controller包,并且创建GoodPriceController
@RestController
public class GoodPriceController {
@Autowired
GoodPriceService goodPriceService;
@GetMapping("/price")
public Integer getGoodPrice(Integer goodId){
GoodPrice goodPrice= goodPriceService.getGoodPrice(goodId);
return goodPrice.getPrice();
}
}
9.测试
我们访问地址:http://localhost:8083/price?goodId=362
总结:我们今天学习了如何处理good-price服务,主要流程如下
配置pom.xml
配置application.properties
新建entity并且定义getter和setter方法
新建dao包并且定义接口
新建sevice定义service接口类,并且新建impl和定义实现类
新建controller
测试接口调用