1.配置依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SSM_DEMO 1.1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.16.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.16.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
</dependencies>
</project>
2.连接数据库
3.建立目录结构
基础的dao.service.pojo.controller
resoureces下的mybatis-config.xml.ApplicationContext.xml
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>
<typeAliases>
<package name="com.maaoooo.pojo"/>
</typeAliases>
</configuration>
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
</beans>
创建数据库properties
有可能报错 数据库连接不上,检查是否拼错,或者SSL设置为false
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/videodata?useSSL=true&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
4.写实体类
使用lombok
package com.maaoooo.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author lzr
* @date 2020 06 13 19:23
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Video {
private int id;
private String videoName;
private int episode;
private String type;
private String detail;
private int score;
}
5.写dao接口
package com.maaoooo.dao;
import com.maaoooo.pojo.Video;
import java.util.List;
/**
* @author lzr
* @date 2020 06 13 19:27
* @description dao接口
*/
public interface VideoMapper {
//增加一个视频数据
int addVideo(Video video);
//删除一个视频数据
int deleteVideoById(int id);
//更新一个视频数据
int updateVideo(Video video);
//查询一个视频数据
Video queryVideoById(int id);
//查询全部视频数据
List<Video> queryAllVideo();
}
6.Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.maaoooo.dao.VideoMapper">
<!-- //增加一个视频数据-->
<!-- int addVideo(Video video);-->
<insert id="addVideo" parameterType="Video">
insert into videodata.video(videoname, episode, type, detail, score)
values(#{videoName},#{episode},#{type},#{detail},#{score})
</insert>
<!-- //删除一个视频数据-->
<!-- int deleteVideoById(int id);-->
<delete id="deleteVideoById" parameterType="int">
delete from videodata.video where id=#{id}
</delete>
<!-- //更新一个视频数据-->
<!-- int updateVideo(Video video);-->
<update id="updateVideo" parameterType="Video">
update videodata.video
set videoName=#{videoName},episode=#{episode},type=#{type},detail=#{detail},score=#{score} =
where id=#{id}
</update>
<!-- //查询一个视频数据-->
<!-- Video queryVideoById(int id);-->
<select id="queryVideoById" parameterType="int">
select * from videodata.video where id=#{id}
</select>
<!-- //查询全部视频数据-->
<!-- List<Video> queryAllVideo();-->
<select id="queryAllVideo" resultType="Video">
select * from videodata.video
</select>
</mapper>
7.service层
package com.maaoooo.service;
import com.maaoooo.dao.VideoMapper;
import com.maaoooo.pojo.Video;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* @author lzr
* @date 2020 06 13 21:46
* @description
*/
public class VideoServiceImpl implements VideoService{
private VideoMapper videoMapper;
@Autowired
public void setVideoMapper(VideoMapper videoMapper) {
this.videoMapper = videoMapper;
}
public int addVideo(Video video) {
return videoMapper.addVideo(video);
}
public int deleteVideoById(int id) {
return videoMapper.deleteVideoById(id);
}
public int updateVideo(Video video) {
return videoMapper.updateVideo(video);
}
public Video queryVideoById(int id) {
return videoMapper.queryVideoById(id);
}
public List<Video> queryAllVideo() {
return videoMapper.queryAllVideo();
}
}
8.spring-dao配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置外部properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源-->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="10000"/>
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
<property name="dataSource" ref="datasource"/>
<!-- 绑定配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 配置dao接口扫描包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<!-- 要扫描的包-->
<property name="basePackage" value="com.maaoooo.dao"/>
</bean>
</beans>
9.配置spring-service.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描包-->
<context:component-scan base-package="com.maaoooo.service"/>
<!-- 将业务类注入到spring-->
<bean class="com.maaoooo.service.VideoServiceImpl" id="videoService">
<property name="videoMapper" ref="videoMapper"/>
</bean>
<!-- 声明式事务配置-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!-- aop事务-->
</beans>
10.配置web框架,导入Artifacts
在项目add增加web支持
11.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- DispatcherServlet-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 乱码过滤-->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Session-->
<session-config>
<!-- 15分钟断线-->
<session-timeout>15</session-timeout>
</session-config>
</web-app>
12.配置spring-mvc.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置注解扫描-->
<context:component-scan base-package="com.maaoooo.controller"/>
<!-- 让springmvc不处理静态资源-->
<mvc:default-servlet-handler/>
<!-- 注解驱动 解决JSON乱码-->
<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>
13.确定前端操作,根据前端编写controller
<%--
Created by IntelliJ IDEA.
User: lzr
Date: 2020/6/13
Time: 11:48 下午
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>视频管理系统</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<style>
a{
text-decoration: none;
color: black;
font-size: 36px;
}
h3{
width: 360px;
height: 76px;
margin: 100px auto;
text-align: center;
line-height: 76px;
background: yellowgreen;
border-radius: 20px;
}
</style>
</head>
<body>
<div class="page-header">
<h1>视频管理系统<small> 1.0</small></h1>
</div>
<h3>
<a href="${pageContext.request.contextPath}/video/goAllVideoPage">查询所有内容</a>
</h3>
</body>
</html>
7.10整合thymeleaf
依赖
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- 模板解析器 -->
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean>