SSM整合步骤
1.建立工程
1.导入所需jar包 配置pom.xml文件 spring+springmvc+mybatis核心包+依赖包 spring与mybaits整合的包,等等
2.创建spring的配置文件 beans.xml文件
2.1扫描装配bean
2.2注入数据源 还需要db.properties
2.3创建SqlSessionFactory
2.4创建Mybatis扫描器
2.5.配置事务
2.6.事务注解生效
3.创建 springMvc.xml文件
3.1扫描装配bean
3.2处理映射器,处理适配器,对json格式支持
3.3视图解析器
3.4 支持文件上传
4.SqlMapConfig.xml
1.开启延迟加载的功能
2.设置别名
5.修改web.xml
5.1需要监听spring程序
5.2需加加载beans.xml
5.3.配置springMvc
5.4编码格式处理
6.创建po com.javacto.po User
7.创建mapper com.javacto.mapper UserMapper
8.创建业务层 接口+实现类 com.javacto.service UserServcie+UserServcieImpl
9.创建控制层 com.javacto.contoller UserAction
10.创建jsp文件
11.测试
1.导入所需jar包 配置pom.xml文件 spring+springmvc+mybatis核心包+依赖包 spring与mybaits整合的包,等等
<?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>com.javacto</groupId>
<artifactId>SSMDay05</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SSMDay05 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- Spring版本号5.2.6.RELEASE -->
<spring.version>5.2.6.RELEASE</spring.version>
<oracle.verson>11.2.0.2.0</oracle.verson>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring相关包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</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-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- AOP相关包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!-- 支持文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--对json格式支持 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<!-- MyBatis相关包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- MySQL相关包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- oracle 包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.verson}</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.20</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<!-- Spring集成MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--LOG4日志 start -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!--LOG4日志 end -->
<!-- 单元测试相关包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.6</version>
</dependency>
</dependencies>
<build>
<finalName>SSMDay05</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.创建spring的配置文件 beans.xml文件
2.1创建Spring的beans.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"
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">
<!-- 2.1扫描装配bean-->
<context:component-scan base-package="com.javacto"></context:component-scan>
<!--获取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 2.2注入数据源 alibaba数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.jdbcUrl}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" /><!--连接池支持的最大连接数-->
<!--连接池中的最小空闲连接数,Druid会定时扫描连接池的连接,如果空闲的连接数大于该值,
则关闭多余的连接,反之则创建更多的连接以满足最小连接数要求。-->
<property name="minIdle" value="5" />
</bean>
<!--2.3创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--3.1必需注入数据源 mybatis需要操作数据库-->
<property name="dataSource" ref="dataSource"></property>
<!--3.2.加载 SqlMapConfig.xml-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!--3.3.如果使用分页,就需要在这里配置,不用就不要配置-->
<!-- 配置分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<!--设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 reasonable:禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<value>
dialect=Mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 2.4创建Mybatis扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--批量配置映射文件 -->
<property name="basePackage" value="com.javacto.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 2.5配置事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--5.1注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2.6事务注解生效 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
2.2创建db.properties数据源
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/javacto?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.user=root
jdbc.password=123456
2.3创建SqlMapConfig.xml的配置Mybatis
<?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>
<settings>
<!-- 二级缓存开关 -->
<setting name="cacheEnabled" value="true" />
<!-- 延迟加载开关 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 积极加载,默认为true,设置为false按需加载 -->
<setting name="aggressiveLazyLoading" value="false"></setting>
</settings>
<typeAliases>
<!--批量别名配置 -->
<package name="com.javacto.po"/>
</typeAliases>
</configuration>
3.创建 springMvc.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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 4.0扫描装配bean -->
<context:component-scan base-package="com.javacto.contoller"></context:component-scan>
<!-- 4.1 处理器映射器,处理器适配器 对json格式支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 4.2 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--后缀 解析出真正的物理视图 前缀+逻辑视图+后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 4.3.支持文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--限制文件大小 字节 10M 10*1024*1024 -->
<property name="maxUploadSize" value="10485760" />
</bean>
<!--4.4. 加入对静态-->
<mvc:resources mapping="/resource/**" location="/resource/"/>
<!-- <mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/scripts/" mapping="/scripts/**"/>-->
<!--如果把所有img css 都放入 resource -->
</beans>
5.修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!-- 6.1需要监听spring程序 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 6.2加载beans.xml 文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 6.3配置视图解析器 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- 6.4编码格式处理 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
6.创建po com.javacto.po User
package com.javacto.po;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/*必需实现序列化*/
public class User implements Serializable {
private int tid;
private String uname;
private Date birthday;
private char sex;
private String address;
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
7.创建mapper com.javacto.mapper UserMapper
public interface UserMapper {
//以一起使用注解,同时可以使用xml 我可以再创建一个 UserMapper2.xml
//根据查询
@Select("SELECT * FROM t_user WHERE tid=#{tid }")
public User findUserById002(@Param("tid") int tid);
//模糊查询
@Select("SELECT * FROM t_user WHERE uname LIKE concat(concat('%',#{uname}),'%')")
public List<User> findByName(String uname);
//查询所有
@Select("SELECT * FROM t_user")
public List<User> findUserList();
//添加
@Insert("INSERT INTO t_user VALUES(#{tid},#{uname},now(),#{sex},#{address})")
public int insertUser(User user);
//删除
@Delete("DELETE FROM t_user WHERE tid=${id}")
public int deleteUser(@Param(("id")) int id);
}
8.创建业务层 接口+实现类 com.javacto.service UserServcie+UserServcieImpl
public interface UserServcie {
public List<User> findUserList();
}
@Service
public class UserServcieImpl implements UserServcie{
@Autowired
private UserMapper userMapper;
public List<User> findUserList(){
return userMapper.findUserList();
}
}
9.创建控制层 com.javacto.contoller UserAction
@Controller
@RequestMapping("/user")
public class UserAction {
@Autowired
private UserServcie userServcie;
/* @RequestMapping("/queryUserList")
public String findUserList(Model model,Integer pageNum){
//如果是第一次访问,是没有值的,必需把pageNum设置为1
if (pageNum == null) {
pageNum=1;
}
List<User> users = userServcie.findUserList();
//分页查询
model.addAttribute("users",users);
return "userList";
}*/
@RequestMapping("/queryUserList2")
public String findUserList(Model model,Integer pageNum){
//如果是第一次访问,是没有值的,必需把pageNum设置为1
if (pageNum == null) {
pageNum=1;
}
List<User> users = userServcie.findUserList();
//分页查询 startPage(当前页,每页显示多少条数据)
PageHelper.startPage(pageNum,2);
//就是比上面多了一句
PageInfo<User> info = new PageInfo<User>(users);
// model.addAttribute("users",users);
model.addAttribute("users",info.getList());
model.addAttribute("info",info);
System.out.println(info.getTotal());
return "userList";
}
}
10.创建jsp文件
在WEB-INF下创建jsp文件夹创建userList.jsp页面
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/6/29
Time: 21:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<form>
<table border="1" width="60%">
<tr>
<td>tid</td>
<td>uname</td>
<td>birthday</td>
<td>address</td>
<td>操作</td>
</tr>
<c:forEach var="li" items="${users}">
<tr>
<td>${li.tid}</td>
<td>${li.uname}</td>
<td>${li.birthday}</td>
<td>${li.address}</td>
<td>
<fmt:formatDate value='${li.birthday }' pattern='yyyy-MM-dd'/>
</td>
<td>
<a>添加</a>
</td>
</tr>
</c:forEach>
</table>
<%-- 复制过来。什么也不要改,只需要修改调用方法--%>
<table>
<tr>
<td>
当页为 第 ${info.pageNum }页 总共${info.pageSize } 页 总共 ${info.total } 条数据
<a href="${pageContext.request.contextPath }/rest//user/queryUserList2?pageNum=1">首页</a>
<a href="${pageContext.request.contextPath }/rest//user/queryUserList2?pageNum= ${info.pageNum-1 }">上一页</a>
<a href="${pageContext.request.contextPath }/rest//user/queryUserList2?pageNum=${info.pageNum+1 }">下一页</a>
<a href="${pageContext.request.contextPath }/rest/user/queryUserList2?pageNum=${info.pageSize }">末页</a>
</td>
</tr>
</table>
</form>
</body>
</html>