课程目标
使用SpringMVC+Mybatis+Spring+Maven进行分模块整合 简单实现视频网站开发
视频网站构建模式
分为:自建模式和第三方模式
自建模式:需要自己搭建服务器、解决CDN加速问题、提高SDK、防盗链等问题
第三方模式:使用第三方云视频接口、阿里云、保利云(8毛)、乐视云(4毛G)。
讲述一下 蚂蚁课堂网站视频架构
后端:SpringMVC、Spring、Mybatis
前端:jquery
itmayiedu-parent —父类工程
itmayiedu-commons —工具类
itmayiedu-entity —实体类
itmayiedu-dao —数据库访问层
itmayiedu-service —服务层
itmayiedu-web —web层
4.12
4.1.3.RELEASE
3.2.8
1.2.2
1.2.15
5.1.32
1.6.4
2.4.2
1.0.9
4.3.5
1.2
2.5
2.0
2.5
3.3.2
1.3.2
3.3
5.0.0
0.9.1
1.3.1
2.7.2
4.10.3
joda-time
joda-time
${joda-time.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
org.apache.commons
commons-io
${commons-io.version}
commons-net
commons-net
${commons-net.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
org.apache.httpcomponents
httpclient
${httpclient.version}
junit
junit
${junit.version}
test
org.slf4j
slf4j-log4j12
${slf4j.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
com.github.miemiedev
mybatis-paginator
${mybatis.paginator.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
jstl
jstl
${jstl.version}
javax.servlet
servlet-api
${servlet-api.version}
provided
javax.servlet
jsp-api
${jsp-api.version}
provided
commons-fileupload
commons-fileupload
${commons-fileupload.version}
redis.clients
jedis
${jedis.version}
${project.artifactId}
org.apache.maven.plugins
maven-resources-plugin
2.7
UTF-8
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.8
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
3.2.1 引入SpringMVC-Maven依赖
jstl
jstl
javax.servlet
servlet-api
provided
javax.servlet
jsp-api
provided
commons-fileupload
commons-fileupload
org.springframework
spring-webmvc
com.itmayiedu
itmayiedu-entity
0.0.1-SNAPSHOT
com.fasterxml.jackson.core
jackson-annotations
2.5.0
com.fasterxml.jackson.core
jackson-core
2.5.0
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.jr
jackson-jr-all
2.5.0
3.2.2 新增springmvc配置文件
3.2.3 web.xml加载SpringMVC配置文件
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*
itmayiedu-web
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
1
itmayiedu-web
/
3.2.4 创建测试SpringMVC环境结果
@Controller
public class TestController {
private static final String TEST="test";
@RequestMapping("/test")
public String test(){
return TEST;
}
}
3.2.5运行SpringMVC环境结果
Ok springMVC环境搭建成功
3.3.1 表结构说明
--视频类型表
CREATE TABLE `video_type` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键(自增长)',
`type_name` varchar(30) DEFAULT NULL COMMENT '视频类型',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
--视频详情表
CREATE TABLE `video_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键(自增长)',
`video_name` varchar(150) DEFAULT NULL COMMENT '视频名称',
`video_url` varchar(100) DEFAULT NULL COMMENT '封面图片',
`video_html` varchar(500) DEFAULT NULL COMMENT '视频html执行元素',
`video_type_id` int DEFAULT NULL COMMENT '关联typeID',
`video_del` INT DEFAULT 0 COMMENT '是否显示 0显示 1隐藏',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
3.3.2 itmayiedu-dao工程中新增依赖
com.itmayiedu
weixin-entity
0.0.1-SNAPSHOT
org.mybatis
mybatis
org.mybatis
mybatis-spring
com.github.miemiedev
mybatis-paginator
com.github.pagehelper
pagehelper
mysql
mysql-connector-java
com.alibaba
druid
src/main/java
**/*.properties
**/*.xml
false
3.3.2 itmayiedu-service工程中新增依赖
com.itmayiedu
itmayiedu-dao
0.0.1-SNAPSHOT
org.springframework
spring-context
org.springframework
spring-beans
org.springframework
spring-jdbc
org.springframework
spring-aspects
org.slf4j
slf4j-log4j12
3.3.3新增db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.host=localhost
jdbc.database=test
jdbc.userName=root
jdbc.passWord=root
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=1
jdbc.maxWait=1000
3.3.4新增applicationContext-service
3.3.5新增applicationContext-dao
3.3.6新增 applicationContext-trans
3.3.7使用generator生成数据ORM映射文件
3.3.8 实现查询所有视频类型
/**
*
* @classDesc: 功能描述:(测试TestController)
* @author: 余胜军
* @createTime: 2017年9月13日 下午9:01:42
* @version: v1.0
* @copyright:上海每特教育科技有限公司
* @QQ:644064779
*/
@Controller
public class TestController {
private static final String TEST = "test";
@Autowired
private VideoTypeService videoTypeService;
@RequestMapping("/test")
public String test() {
return TEST;
}
@ResponseBody
@RequestMapping("/getViideType")
public List getViideType() {
System.out.println("listVideoType start");
List listVideoType = videoTypeService.showVideoType(null);
System.out.println("listVideoType end");
return listVideoType;
}
}
@Service
public class VideoTypeServiceImpl implements VideoTypeService {
@Autowired
private VideoTypeMapper videoTypeMapper;
public List showVideoType(VideoType record) {
return videoTypeMapper.selectList(record);
}
}
public interface VideoTypeMapper {
List selectList(VideoType record);
}
id, type_name
delete from
video_type
where id = #{id,jdbcType=INTEGER}
insert into video_type (id,
type_name)
values (#{id,jdbcType=INTEGER},
#{typeName,jdbcType=VARCHAR})
insert into video_type
id,
type_name,
#{id,jdbcType=INTEGER},
#{typeName,jdbcType=VARCHAR},
update video_type
type_name = #{typeName,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update video_type
set type_name = #{typeName,jdbcType=VARCHAR}
where id =
#{id,jdbcType=INTEGER}
3.3.9 web.xml配置加载spring
contextConfigLocation
classpath:spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
3.3.10 运行结果解决406
使用SpringMVC返回json格式,返回状态406,表示SpringMVC没有集成JSON转换器
解决办法:
text/html;charset=UTF-8
Ok SpringMVC+Mybatis+Spring+Maven环境整合成功!
3.4.1创建log4j.properties
在resources/properties下创建log4j.properties文件
### set log levels ###
log4j.rootLogger =INFO,DEBUG, stdout , R
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
3.4.2web.xml加载 log4j.properties
log4jConfigLocation
/WEB-INF/classes/properties/log4j.properties
org.springframework.web.util.Log4jConfigListener
使用API
private static Logger log = Logger.getLogger(TestController.class);
步骤:创建JSP页面,引入C标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
4.2.1查询jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
后台管理系统
蚂蚁视频后台管理系统
添加资源
图片
视频名称
视频类型
预览视频
${p.videoName}
${p.typeName}
预览视频
42.2编写XML SQL 语句
4.2.3 完成页面展示
4.3.1 创建上传资源页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
添加视频资源
蚂蚁视频后台添加视频资源
4.3.2 springmvc.xml文件中新增上传文件配置
4.3.3 后端代码上传文件
@RequestMapping("/addVideo")
public String addVideo(@RequestParam(value = "file", required = false) MultipartFile file, VideoInfo videoInfo,
HttpServletRequest req, HttpServletResponse res) {
try {
// 获取当前上下文
String path = req.getSession().getServletContext().getRealPath("/static/imgs");
// 文件名称
String newName = System.currentTimeMillis() + ".png";
File targetFile = new File(path, newName);
// 文件夹不存在,则创建文件夹
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
log.error(e);
}
videoInfo.setVideoUrl(newName);
videoInfoService.addVideoInfo(videoInfo);
req.setAttribute("result", "封面上传成功!");
return "redirect:/videoManag";
} catch (Exception e) {
log.error(e);
req.setAttribute("result", "上传失败!");
return LOCAVIDEO;
}
}
4.4.1视频详情页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
${videoInfo.videoName}视频详情
${videoInfo.videoName}
4.4.2视频详情后台代码
@RequestMapping("/videoDetails")
public String videoDetails(int id, HttpServletRequest request) {
VideoInfo videoInfo = videoInfoService.getVideoInfo(id);
request.setAttribute("videoInfo", videoInfo);
return VIDEODETAILS;
}
4.7.1 将pagehelper版本修改为3.6.4
4.7.2 新增mybatis-config.xml文件
4.7.3 applicationContext-dao.xml加载mybatis-config.xml
4.7.4 Java代码使用
@RequestMapping("/indexVideo")
public String indexVideo(HttpServletRequest request, int pageIndex) {
Page page = PageHelper.startPage(pageIndex, 2);
request.setAttribute("listVideo", videoInfoService.getVoideAll(null));
request.setAttribute("pageSize", page.getPages());
return INDEXVIDEO;
}
4.7.5 前端代码
首页
${p}
尾页