课程目标
使用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层
三、项目整合
3.1 itmayiedu-parent父工程添加依赖
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<mysql.version>5.1.32</mysql.version>
<slf4j.version>1.6.4</slf4j.version>
<jackson.version>2.4.2</jackson.version>
<druid.version>1.0.9</druid.version>
<httpclient.version>4.3.5</httpclient.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<joda-time.version>2.5</joda-time.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<commons-io.version>1.3.2</commons-io.version>
<commons-net.version>3.3</commons-net.version>
<pagehelper.version>5.0.0</pagehelper.version>
<jsqlparser.version>0.9.1</jsqlparser.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<jedis.version>2.7.2</jedis.version>
<solrj.version>4.10.3</solrj.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 时间操作组件 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons-net.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
3.2 Spring整合SpringMVC
3.2.1 引入SpringMVC-Maven依赖
<dependencies>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<!-- springwebmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.itmayiedu</groupId>
<artifactId>itmayiedu-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-all</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
3.2.2 新增springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- springmvc 扫包范围 -->
<context:component-scan base-package="com.itmayiedu.controller" />
<!-- 开启springmvc注解 -->
<mvc:annotation-driven />
<!-- 视图跳转类型 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.2.3 web.xml加载SpringMVC配置文件
<!-- 解决post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>itmayiedu-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>itmayiedu-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
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.3Spring整合Mybatis
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工程中新增依赖
<!-- 依赖管理 -->
<dependencies>
<dependency>
<groupId>com.itmayiedu</groupId>
<artifactId>weixin-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
3.3.2 itmayiedu-service工程中新增依赖
<dependencies>
<dependency>
<groupId>com.itmayiedu</groupId>
<artifactId>itmayiedu-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
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
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 开启注解 -->
<context:component-scan base-package="com.itmayiedu.service"></context:component-scan>
</beans>
3.3.5新增applicationContext-dao
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 数据库连接池 -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url"
value="jdbc:mysql://${jdbc.host}:3306/${jdbc.database}?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull" />
<property name="username" value="${jdbc.userName}" />
<property name="password" value="${jdbc.passWord}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mappings/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itmayiedu.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
3.3.6新增 applicationContext-trans
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.itmayiedu.service.*.*(..))" />
</aop:config>
</beans>
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<VideoType> getViideType() {
System.out.println("listVideoType start");
List<VideoType> listVideoType = videoTypeService.showVideoType(null);
System.out.println("listVideoType end");
return listVideoType;
}
}
@Service
public class VideoTypeServiceImpl implements VideoTypeService {
@Autowired
private VideoTypeMapper videoTypeMapper;
public List<VideoType> showVideoType(VideoType record) {
return videoTypeMapper.selectList(record);
}
}
public interface VideoTypeMapper {
List<VideoType> selectList(VideoType record);
}
<?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.itmayiedu.dao.VideoTypeMapper">
<resultMap id="BaseResultMap" type="com.itmayiedu.entity.VideoType">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="type_name" property="typeName" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
id, type_name
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap"
parameterType="java.lang.Integer">
select
<include refid="Base_Column_List" />
from video_type
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from
video_type
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.itmayiedu.entity.VideoType">
insert into video_type (id,
type_name)
values (#{id,jdbcType=INTEGER},
#{typeName,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.itmayiedu.entity.VideoType">
insert into video_type
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="typeName != null">
type_name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="typeName != null">
#{typeName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.itmayiedu.entity.VideoType">
update video_type
<set>
<if test="typeName != null">
type_name = #{typeName,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.itmayiedu.entity.VideoType">
update video_type
set type_name = #{typeName,jdbcType=VARCHAR}
where id =
#{id,jdbcType=INTEGER}
</update>
<select id="selectList" resultMap="BaseResultMap"
parameterType="com.itmayiedu.entity.VideoType">
select
<include refid="Base_Column_List" />
from video_type
where
1=1
<if test="id != null">
and #{id,jdbcType=INTEGER},
</if>
<if test="typeName != null">
and #{typeName,jdbcType=VARCHAR},
</if>
</select>
</mapper>
3.3.9 web.xml配置加载spring
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.3.10 运行结果解决406
使用SpringMVC返回json格式,返回状态406,表示SpringMVC没有集成JSON转换器
解决办法:
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>
Ok SpringMVC+Mybatis+Spring+Maven环境整合成功!
3.4 Spring整合log4j
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
<!--设置log4j的配置文件位置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/properties/log4j.properties</param-value>
</context-param>
<!--使用监听加载log4j的配置文件 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
使用API
private static Logger log = Logger.getLogger(TestController.class);
四、后台管理
4.1 搭建编写视频查询页面
步骤:创建JSP页面,引入C标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
4.2 实现后台视频查询页面
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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>后台管理系统</title>
</head>
<body>
<center>
<h1>蚂蚁视频后台管理系统</h1>
<a href="locaAddVideo">添加资源</a>
<table style="BORDER-COLLAPSE: collapse; text-align: center;"
borderColor=#000000 height=40 cellPadding=1 width="70%"
align="center" border=1>
<thead>
<tr>
<th>图片</th>
<th>视频名称</th>
<th>视频类型</th>
<th>预览视频</th>
</tr>
</thead>
<tbody>
<c:forEach items="${listVideoInfo}" var="p">
<tr style="font-size: 18px">
<td><img alt="" width="150px;" height="150px;"
src="/static/imgs/${p.videoUrl}"></td>
<td>${p.videoName}</td>
<td>${p.typeName}</td>
<td><a href="videoDetails?id=${p.id}" style='text-decoration:none;'>预览视频</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</center>
</body>
</html>
42.2编写XML SQL 语句
<select id="selectAll" parameterType="com.itmayiedu.entity.VideoInfo"
resultType="com.itmayiedu.entity.VideoInfo">
select a.id as id,a.video_name as videoName, a.video_html as videoHtml ,a.video_url as videoUrl, a.video_del as videoDel
, b.type_name as typeName
from video_info as a inner join video_type as b on a.video_type_id=b.id;
</select>
4.2.3 完成页面展示
4.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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加视频资源</title>
</head>
<body>
<center>
<h1>蚂蚁视频后台添加视频资源</h1>
<form action="addVideo" style="font-size: 14px;" method="post"
ENCTYPE="multipart/form-data">
<table>
<tr>
<td>视频名称:</td>
<td><input type="text" name=videoName></td>
</tr>
<tr>
<td>视频类型:</td>
<td><select name="videoTypeId" style="width: 170px;">
<c:forEach items="${listVideoType}" var="p">
<option value="${p.id}">${p.typeName}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td>优酷播放URL:</td>
<td><textarea rows="10" cols="30" name="videoHtml"></textarea></td>
</tr>
<tr>
<td>上传封面:</td>
<td><input type="file" name="file"></td>
</tr>
<tr> <td colspan="2"><input type="submit" value="提交"></td></tr>
</table>
</form>
</center>
</body>
</html>
4.3.2 springmvc.xml文件中新增上传文件配置
<!-- 支持上传文件 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
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 视频详情页面
4.4.1视频详情页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${videoInfo.videoName}视频详情</title>
</head>
<body>
<center>
<h1>${videoInfo.videoName}</h1>
<iframe height=600 width=600 src='${videoInfo.videoHtml}'
frameborder=0'allowfullscreen'></iframe>
</center>
</body>
</html>
4.4.2视频详情后台代码
@RequestMapping("/videoDetails")
public String videoDetails(int id, HttpServletRequest request) {
VideoInfo videoInfo = videoInfoService.getVideoInfo(id);
request.setAttribute("videoInfo", videoInfo);
return VIDEODETAILS;
}
4.6静态资源访问404原因
<mvc:annotation-driven />
<mvc:resources mapping="/static/imgs/**" location="static/imgs/" />
<mvc:resources mapping="/static/easyui/**" location="static/easyui/" />
<mvc:resources mapping="/static/js/**" location="static/js/" />
<mvc:resources mapping="/static/css/**" location="static/css/" />
4.7整合pagehelper分页查询
4.7.1 将pagehelper版本修改为3.6.4
4.7.2 新增mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
4.7.3 applicationContext-dao.xml加载mybatis-config.xml
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mappings/*.xml"></property>
<property name="configLocation" value="classpath:spring/mybatis-config.xml" />
</bean>
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 前端代码
<a style="font-size: 20px;" href="indexVideo?pageIndex=1">首页</a>
<c:forEach begin="1" end="${pageSize}" var="p">
<a style="font-size: 20px;" href="indexVideo?pageIndex=${p}">${p}</a>
</c:forEach>
<a style="font-size: 20px;" href="indexVideo?pageIndex=${pageSize}">尾页</a>