利用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环境结果
@Controllerpublic 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://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.xsdhttp://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 /@Controllerpublic class TestController {private static final String TEST = \公众test\"大众;@Autowiredprivate 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;}}@Servicepublic class VideoTypeServiceImpl implements VideoTypeService {@Autowiredprivate 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_typewhere id = #{id,jdbcType=INTEGER}</select><delete id=\"大众deleteByPrimaryKey\"大众 parameterType=\"大众java.lang.Integer\"大众>delete fromvideo_typewhere 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_typeset 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_typewhere1=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的映射 --><beanclass=\"大众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 configurationPUBLIC \"大众-//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}