目录
- 创建一个 module 项目
- 导入依赖
- mybatis
- mybatis-config.xml 配置文件
- spring-mybatis 整合
- spring-mybatis.xml配置文件
- spring-dao.xml 配置文件
- spring-service 配置文件
- spring 整合 springmvc
- 添加 web 模块
- web.xml 配置文件
- spring-mvc配置文件
- jsp
- 运行测试
- 配置服务器
- 例子
- 总结
创建一个 module 项目
导入依赖
思考一下应该导入什么依赖,不要粘,敲一敲
- junit 测试
- mysql 数据库连接
- 数据库连接池(c3p0、druid、dbcp)
- servlet、jsp(servlet-api、jsp-api、jstl)
- mybatis(mybatis、mybatis-spring)
- spring(jdbc)
- sprinmvc(spring-webmvc)
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
mybatis
- 创建 mybatis-config.xml
- 创建 service、controller、dao、pojo 包
mybatis-config.xml 配置文件
- 实体类包扫描 typeAliases
- xml 文件映射 mappers
<?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.common.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/common/dao/UserMapper.xml"/>
</mappers>
</configuration>
spring-mybatis 整合
- 创建 applicationContext 文件,并将配置好的spring-dao.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/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--整合 spring-mybatis、spring-springmvc 文件注册到spirng中-->
<import resource="spring-dao.xml"/>
</beans>
spring-mybatis.xml配置文件
- 创建 dataSource.properties 数据源配置文件
如果你导入的mysql驱动是8.x 版本, 驱动加 cj,url要加时区
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=mysql:jdbc://localhost:3306/ssm?useUnicode=true&useSSL=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
spring-dao.xml 配置文件
1.关联数据库配置文件
2.连接池配置
3.sqlSessionFactory对象
4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中
<?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">
<!-- 1.关联数据库配置文件-->
<context:property-placeholder location="classpath:dataSource.properties"/>
<!-- 2.连接池配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.username}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" /> <!--最大连接数30-->
<property name="initialPoolSize" value="10"/> <!--初始化连接数10-->
<property name="minPoolSize" value="10" /> <!--最小连接数10-->
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.common.dao"/>
</bean>
</beans>
spring-service 配置文件
1.扫描service相关的bean
2.UserServiceImpl(接口实现类)注入到IOC容器中
3.配置事务管理器
配置过程中报 bean 没有注入?
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring整合service层-->
<!-- 扫描service相关的bean -->
<context:component-scan base-package="com.common.service" />
<!--UserServiceImpl注入到IOC容器中-->
<bean id="UserServiceImpl" class="com.common.service.serviceImpl.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
配置完spring-service 注入到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" xmlns:context="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--整合 spring-mybatis、spring-springmvc 文件注册到spirng中-->
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
</beans>
spring 整合 springmvc
添加 web 模块
web.xml 配置文件
- 注册中心控制器
- 乱码处理
- session超时设置
<?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">
<!-- SpringMVC层-->
<!--DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 乱码处理-->
<!--encodingFilter-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
spring-mvc配置文件
- 1.开启SpringMVC注解驱动
- 2.静态资源默认servlet配置
- 3.配置jsp 显示ViewResolver视图解析器
- 4.扫描web相关的bean
<?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: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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.common.controller" />
</beans>
配置完spring-mvc 注入到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" xmlns:context="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--整合 spring-mybatis、spring-springmvc 文件注册到spirng中-->
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
jsp
index.jsp
<%--
Created by IntelliJ IDEA.
User: klz
Date: 2020/4/20
Time: 23:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
<style type="text/css">
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
</style>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/allUser">进入用户页面</a>
</body>
</html>
allUser.jsp
<%--
Created by IntelliJ IDEA.
User: klz
Date: 2020/4/20
Time: 23:12
To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>所有用户</small>
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>id</th>
<th>用户名</th>
<th>用户名密码</th>
<th>地址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${requestScope.get('listUser')}">
<tr>
<td>${user.getId()}</td>
<td>${user.getUserName()}</td>
<td>${user.getPassword()}</td>
<td>${user.getAddress()}</td>
<td>
<a href="#">更改</a> |
<%-- href="${pageContext.request.contextPath}/user/del/${user.getId()}"--%>
<a href="#">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
运行测试
配置服务器
创建一个lib文件夹,存jar
运行服务器
例子
链接:https://pan.baidu.com/s/1IJFTl5NqNStedXofNaxddQ
提取码:w48c
总结
相信大家都看了,ssm 是考spring整合的,考的就是配置文件
- spring-dao.xml 相当于 @Repository
- spring-service.xml 相当于 @Service
- spring-mvc.xml 相当于 @Controller
- applicatonContext.xml 相当于 @Component(将bean注册到spring)、@ComponentScan(报扫描,并且注册到spring中)
在后面我们用 springbot 写项目都是零配置,很快