02-Myabtis连接、查询、统一返回协议、异常处理、热部署
02-Myabtis连接、查询、统一返回协议、异常处理、热部署
第1集 小滴课堂实战之Mybaits打通Mysql数据库
简介:配置Mybatis连接Mysql数据库
-
添加数据库配置
server.port=8081 #==============================数据库相关配置======================================== spring.datasource.driver-class-name =com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/online_xdclass?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=xdclass.net #使用阿里巴巴druid数据源,默认使用自带的 #spring.datasource.type =com.alibaba.druid.pool.DruidDataSource #开启控制台打印sql mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl # mybatis 下划线转驼峰配置,两者都可以 #mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.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="net.xdclass.online_xdclass.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权限路径规划
统一返回协议
/**
* 统一返回协议
*/
public class JsonData {
/**
* 状态码:-1(失败)、0(成功)、1(处理中)
*/
private Integer code;
/**
* 返回的结果集
*/
private Object data;
/**
* 返回的描述信息
*/
private String msg;
public JsonData() {}
public JsonData(Integer code, Object data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
/**
* 成功,无返回
* @return
*/
public static JsonData buildSuccess(){
return new JsonData(0,null,null);
}
/**
* 成功,返回结果集
* @param data
* @return
*/
public static JsonData buildSuccess(Object data){
return new JsonData(0,data,null);
}
/**
* 错误,返回描述信息
* @param msg
* @return
*/
public static JsonData buildError(String msg){
return new JsonData(-1,null,msg);
}
/**
* 错误,返回自定义状态码、描述信息
* @param code
* @param msg
* @return
*/
public static JsonData buildError(Integer code,String msg){
return new JsonData(code,null,msg);
}
get、set...
}
简介:开发视频列表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>-
IDEA配置
- idea里面要设置,window和mac一样
![]()
-
使用快捷键打开,选择Registry
注意默认快捷键: window快捷键 Shift+Ctrl+Alt+/ mac快捷键 Shift+Command+Alt+/ 如果自行修改了默认快捷键,则百度搜索相关博文,或者还原快捷键设置![]()
-
选择compiler.automake.allow.when.app.running ,重启idea就行!!!
![]()
第4集 首页banner轮播图和视频详情接口开发
简介:首页banner轮播图接口开发
-
开发轮播列表接口
<select id="listVideoBanner" resultType="VideoBanner"> select * from video_banner order by weight asc </select> -
视频详情接口开发《上》
/**
* 查询视频详情(章、集)
* @param videoId
* @return
*/
@GetMapping("detail")
public JsonData videoDetailById(@RequestParam(value = "video_id",required = true)int videoId){
Video video = videoService.videoDetailById(videoId);
return JsonData.buildSuccess(video);
}
第5集 视频详情接口开发-三表关联查询映射
简介:视频详情接口,多表关联开发
-
修改Video-Chapter POJO类, 增加属性(不使用DTO)
-
定义ResultMap
-
调试接口数据
<resultMap id="VideoDetailResultMap" type="Video"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="title" jdbcType="VARCHAR" property="title"/> <result column="summary" jdbcType="VARCHAR" property="summary"/> <result column="cover_img" jdbcType="VARCHAR" property="coverImg"/> <result column="price" jdbcType="INTEGER" property="price"/> <result column="point" jdbcType="DOUBLE" property="point"/> <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/> <collection property="chapterList" ofType="Chapter"> <id column="chapter_id" jdbcType="INTEGER" property="id"/> <result column="chapter_title" jdbcType="VARCHAR" property="title"/> <result column="ordered" jdbcType="INTEGER" property="ordered"/> <result column="chapter_create_time" jdbcType="TIMESTAMP" property="createTime"/> <collection property="episodeList" ofType="Episode"> <id column="episode_id" jdbcType="INTEGER" property="id"/> <result column="num" jdbcType="INTEGER" property="num"/> <result column="episode_title" jdbcType="VARCHAR" property="title"/> <result column="episode_ordered" jdbcType="INTEGER" property="ordered"/> <result column="play_url" jdbcType="VARCHAR" property="playUrl"/> <result column="free" jdbcType="INTEGER" property="free"/> <result column="episode_create_time" jdbcType="TIMESTAMP" property="createTime"/> </collection> </collection> </resultMap> <select id="findDetailById" 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 = #{video_id} order by c.ordered,e.num asc </select>
第6集 自定义异常开发和配置
简介:开发自定义异常和配置
-
自定义异常 继承 RuntimeException
/** * 小滴课堂 * 自定义异常类 */ public class XDException extends RuntimeException{ private Integer code; private String msg; public XDException(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 CustomExceptionHandler { private final static Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class); @ExceptionHandler(value = Exception.class) @ResponseBody public JsonData handle(Exception e){ logger.error("[ 系统异常 ]{}",e); if( e instanceof XDException ){ XDException xdException = (XDException) e; return JsonData.buildError(xdException.getCode(),xdException.getMsg()); }else { return JsonData.buildError("全局异常,未知错误"); } } }




浙公网安备 33010602011771号