up:: 课程列表模块开发
说明:
选择course-price模块进行开发
[!备注] 此篇博客和上篇博客没多大区别,可以快速浏览 亮点: 虽然整体思路和上篇博客一致,但是调用了上篇博客的模块course-list
1.建包和配置文件
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入mysql链接的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 引入mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.12.RELEASE</version>
</dependency>
</dependencies>
<!--spring的maven项目必备的一个插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<layout>default</layout>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
配置resource文件,新建application.properties
server.port=8082
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud_course?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=20020829
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}
mybatis.configuration.map-underscore-to-camel-case=true
spring.application.name=course-price
说明:
这里修改了端口,每个模块使用不同端口。。。
2.建立controller层
package com.imooc.course.controller;
import com.imooc.course.entity.CoursePrice;
import com.imooc.course.service.CoursePriceService;
import javax.xml.ws.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 描述: 课程价格控制器
*/
@RestController
public class CoursePriceController {
@Autowired
CoursePriceService coursePriceService;
@GetMapping("/price")
public Integer getCoursePrice(Integer courseId) {
CoursePrice coursePrice = coursePriceService.getCoursePrice(courseId);
return coursePrice.getPrice();
}
}
实体类
package com.imooc.course.entity;
import java.io.Serializable;
import javafx.beans.property.IntegerProperty;
/**
* 描述: CoursePrice的实体类
*/
public class CoursePrice implements Serializable {
Integer id;
Integer courseId;
Integer price;
@Override
public String toString() {
return "CoursePrice{" +
"id=" + id +
", courseId=" + courseId +
", price=" + price +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
2.调用service层
接口
package com.imooc.course.service;
import com.imooc.course.entity.CoursePrice;
/**
* 描述: 课程价格服务
*/
public interface CoursePriceService {
CoursePrice getCoursePrice(Integer courseId);
}
实现
package com.imooc.course.service.impl;
import com.imooc.course.dao.CoursePriceMapper;
import com.imooc.course.entity.CoursePrice;
import com.imooc.course.service.CoursePriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 描述: 课程价格的服务实现类
*/
@Service
public class CoursePriceServiceImpl implements CoursePriceService {
@Autowired
CoursePriceMapper coursePriceMapper;
@Override
public CoursePrice getCoursePrice(Integer courseId) {
return coursePriceMapper.findCoursePrice(courseId);
}
}
3.Mapper层开发
package com.imooc.course.dao;
import com.imooc.course.entity.CoursePrice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
/**
* 描述: 课程价格Mapper类
*/
@Mapper
@Repository
public interface CoursePriceMapper {
@Select("SELECT * FROM course_price WHERE course_id = #{courseId}")
CoursePrice findCoursePrice(Integer courseId);
}
4.主启动类
package com.imooc.course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CoursePriceApplication {
public static void main(String[] args) {
SpringApplication.run(CoursePriceApplication.class, args);
}