第
1
集 线上课堂实战之
Mybaits
打通
Mysql
数据库
简介:配置Mybatis连接Mysql数据库
添加数据库配置
server.port=8081#============================== 数据库相关配置========================================spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/online_xdclass?useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.username=rootspring.datasource.password=xdclass.net# 使⽤阿⾥巴巴 druid 数据源,默认使⽤⾃带的#spring.datasource.type =com.alibaba.druid.pool.DruidDataSource# 开启控制台打印 sqlmybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl# mybatis 下划线转驼峰配置 , 两者都可以#mybatis.configuration.mapUnderscoreToCamelCase=truemybatis.configuration.map-underscore-to-camel-case=true# 配置扫描mybatis.mapper-locations=classpath:mapper/*.xml# 配置 xml 的结果别名mybatis.type-aliases-package=net.xdclass.online_xdclass.domain
创建Video相关类
创建VideoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xjclass.oncline_xjclass.Mapper.VideoMapper">
</mapper>
配置扫描
mapper
路径
@SpringBootApplication
@MapperScan("net.xdclass.online_xdclass.mapper")
public class OnlineXdclassApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineXdclassApplication.class, args);
}
}
第2集 ⼩滴课堂实战之视频列表接⼝开发+API权限路径规划
简介:开发视频列表JSON接⼝
- postman调试接⼝
-
浏览器如果要⽀持 json 格化,需要安装⾕歌插件,但是⽹络问题直接安装不了,推荐使⽤插件
-
-
开发视频列表接⼝开发 jsondata ⼯具类规划 api 权限路径/api/v1/pub/AA/BB 这个是不需要登录/api/v1/pri/AA/BB 这个是需要登录
第3集 ⼩滴课堂实战之dev-tool 实现IDEA项⽬热部署
简介:使⽤dev-tool实现热部署
步骤
pom⽂件添加依赖包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork><!--必须添加这个配置--></configuration></plugin></plugins></build>
详细步骤请看第五章!!!
第4集 ⼩滴课堂实战之⾸⻚banner轮播图和视频详情接⼝开发
简介:⾸⻚banner轮播图接⼝开发
开发轮播列表接⼝
VideoBanner实体类:
public class VideoBanner {
private int id;
private String url;
private String img;
private Date createTime;
private int weight; //权重越大,越靠前;
//toString() set() get方法
}
Controller:
@RestController
@RequestMapping("api/v1/pub/index")
public class IndexController {
@Autowired
private VideoBannerService videoBannerService;
@RequestMapping("findVideoBannerList")
public JsonData VideoBannerList(){
List<VideoBanner> videoBannerList = videoBannerService.findVideoBannerList();
return JsonData.JsonDataBuild(videoBannerList);
}
}
Service:
public interface VideoBannerService {
/**
* 按权重的从大到小的顺序查找轮播图
* @return
*/
List<VideoBanner> findVideoBannerList();
}
@Service
public class VideoBannerServiceImpl implements VideoBannerService {
@Autowired
public VideoBannerMapper videoBannerMapperr;
@Override
public List<VideoBanner> findVideoBannerList() {
return videoBannerMapperr.findVideoBannerList();
}
}
Mapper接口:
@Repository
public interface VideoBannerMapper {
List<VideoBanner> findVideoBannerList();
}
启动类:
@SpringBootApplication
@MapperScan("com.xjclass.oncline_xjclass.Mapper")
public class OnclineXjclassApplication {
public static void main(String[] args) {
SpringApplication.run(OnclineXjclassApplication.class, args);
}
}
Mapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xjclass.oncline_xjclass.Mapper.VideoBannerMapper">
<resultMap id="VideoBannerResultMap" type="videoBanner">
<id column="id" property="id" jdbcType="INTEGER"></id>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"></result>
</resultMap>
<select id="findVideoBannerList" resultMap="VideoBannerResultMap">
select * from video_banner order by weight desc
</select>
</mapper>
运行结果:
第5集 ⼩滴课堂实战之视频详情接⼝开发-三表关联查询映射
简介:视频详情接⼝,多表关联开发
- 修改Video-Chapter POJO类, 增加属性(不使⽤DTO)
- 定义ResultMap
- 调试接⼝数据
controller:
@PostMapping("findVideoDetailsByVideoId")
public JsonData findVideoDetailsByVideoId(@RequestBody Map<String,Object> params){
Integer videoId = (Integer)params.get("videoId");
Video videoDetailList = videoService.findVideoDetailsByVideoId(videoId);
return JsonData.JsonDataBuild(videoDetailList);
}
Service:
Video findVideoDetailsByVideoId(int videoId);
@Service
public class VideoServiceImpl implements VideoService {
@Autowired
private VideoMapper videoMapper;
@Override
public Video findVideoDetailsByVideoId(int videoId) {
return videoMapper.findVideoDetailsByVideoId(videoId);
}
}
Mapper:
//三表查询,查询指定视频的详细信息
Video findVideoDetailsByVideoId(@Param("videoId") int videoId);
启动类:
@SpringBootApplication
@MapperScan("com.xjclass.oncline_xjclass.Mapper")
public class OnclineXjclassApplication {
public static void main(String[] args) {
SpringApplication.run(OnclineXjclassApplication.class, args);
}
}
Mapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xjclass.oncline_xjclass.Mapper.VideoMapper">
<!--三表关联查询-->
<resultMap id="VideoDetailResultMap" type="video">
<id column="id" property="id" jdbcType="INTEGER"></id>
<result column="title" property="title" jdbcType="VARCHAR"/>
<result column="summary" property="summary" jdbcType="VARCHAR"/>
<result column="cover_img" property="CoverImg" jdbcType="VARCHAR"/>
<result column="create_time" property="CreateTime" jdbcType="TIMESTAMP"/>
<result column="c_id" property="Cid" jdbcType="INTEGER"/>
<result column="point" property="point" jdbcType="DOUBLE"/>
<collection property="chapters" ofType="chapter">
<id column="chapter_id" jdbcType="INTEGER" property="id"></id>
<result column="chapter_title" property="title" jdbcType="VARCHAR"/>
<result column="ordered" property="ordered" jdbcType="INTEGER"/>
<result column="chapter_create_time" property="createTime" jdbcType="TIMESTAMP"/>
<collection property="episodes" ofType="episode">
<id column="episode_id" property="id" jdbcType="INTEGER"></id>
<result column="episode_title" property="title" jdbcType="VARCHAR"/>
<result column="num" property="num" jdbcType="INTEGER"/>
<result column="play_url" property="playUrl" jdbcType="VARCHAR"/>
<result column="free" property="free" jdbcType="INTEGER"/>
<result column="cover_img" property="coverImg" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
</collection>
</collection>
</resultMap>
<select id="findVideoDetailsByVideoId" resultMap="VideoDetailResultMap">
SELECT
v.id,v.title,v.summary,v.cover_img,v.price,v.point,v.create_time,
c.id as chapter_id,c.title as chapter_title,
c.ordered,c.create_time as chapter_create_time,
e.id as episode_id,e.num,e.title as episode_title,e.ordered as episode_ordered,
e.play_url,e.free,e.create_time as episode_create_time
from video v
LEFT JOIN chapter c on v.id = c.video_id
LEFT JOIN episode e on c.id = e.chapter_id
where v.id = #{videoId}
ORDER BY c.ordered,e.num asc;
</select>
</mapper>
运行结果:
第6集 ⼩滴课堂实战之⾃定义异常开发和配置
简介:开发⾃定义异常和配置
⾃定义异常 继承
RuntimeException
public class XJException extends RuntimeException{
private Integer code; //状态码
private String msg; //错误提示内容
public XJException(Integer code,String msg){
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
开发异常处理器ExceptionHandle
/**
* 处理类
*/
@ControllerAdvice
public class CustomExceptionhandle {
public final static Logger logger = LoggerFactory.getLogger(CustomExceptionhandle.class);
//返回给用户错误信息
@ExceptionHandler(value = Exception.class)
@ResponseBody
public JsonData handle(Exception e) {
logger.error("[ 系统异常 ]{}",e); //打印
if (e instanceof XJException){
XJException exception = (XJException) e;
return JsonData.JsonDataError("",exception.getCode(),exception.getMsg());
}else{
return JsonData.JsonDataError("",-500,"全局异常,未知错误");
}
}
}
科普以下@ControllerAdvice注解:@ControllerAdvice 就是 @Controller 的增强版。@ControllerAdvice 主要用来处理全局数据,一般搭配 @ExceptionHandler、@ModelAttribute、@InitBinder使用,在三个应用场景中使用:全局异常处理、全局数据绑定和全局数据预处理。
@ControllerAdvice是@Component注解的一个延伸注解,Spring会自动扫描并能检测到被@ControllerAdvice所标注的类。