08ssm三大框架整合以前步骤

spring framework Ioc容器
spring mvc web项目
mybatis orm持久层框架 ssm 整合
---------------------------------------------------------------
1、maven web项目
src
main
java 类源码路径
resources 资源配置文件
webapp
img
css
js
WEB-INF
web.xml web项目的配置文件
index.jsp 项目首页
test
java 测试源码路径
pom.xml 项目配置文件

2、pom.xml 文件内容如下
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fz</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>ssm</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>1.8</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.6.RELEASE</spring.version>
<mybatis.version>3.4.2</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- web servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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>

<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
<scope>provided</scope>
</dependency>

<!-- springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>

<!-- 导入dbcp的jar包,beans.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>


<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>

<!-- springmvc 文件上传jar包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>

</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!--将类文件打成jar包-->
<archiveClasses>false</archiveClasses>
<!--将资源文件打到classes目录下-->
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>

<!-- tomcat plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:80/manager/text</url> <!-- tomcat管理路径 -->
<server>tomcat7</server> <!-- 与settings.xml文件中Server的id相同 -->
<uriEncoding>utf-8</uriEncoding>
<port>80</port><!--服务器端口号80可以省去-->
<path>/</path> <!-- 应用的部署位置 -->
<contextReloadable>true</contextReloadable>
<systemProperties>
<java.util.logging.SimpleFormatter.format>[%4$s] %1$tF %1$tT %3$s %5$s %n
</java.util.logging.SimpleFormatter.format>
</systemProperties>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

3、数据库名称及表结构代码
CREATE DATABASE db DEFAULT CHARACTER SET utf8;
use db;
CREATE TABLE `student` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;

mysql> select * from student;
+----+-----------+-----------------+
| id | name | address |
+----+-----------+-----------------+
| 4 | 张三丰 | 郑州市 |
| 6 | 李四 | 北京市 |
| 22 | 李四六 | 郑州文化路 |
| 23 | 赵七 | 北京 |
| 24 | jack | USA |
| 25 | Andy | UK |
+----+-----------+-----------------+
4、src/main/resources 目录下
db.properties jdbc配置文件
#jdbc 参数
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8&useSSL=true
db.user=root
db.pass=
#dbcp 数据库连接池配置参数
#定义初始连接数
db.initialSize=0
#定义最大连接数
db.maxActive=20
#定义最大空闲
db.maxIdle=20
#定义最小空闲
db.minIdle=1
#定义最长等待时间
db.maxWait=60000
springmvc.xml 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: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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.controller" />

<!-- 访问静态资源 -->
<mvc:resources mapping="/img/**" location="/img/" cache-period="2592000"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="2592000"/>
<mvc:resources mapping="/css/**" location="/js/" cache-period="2592000"/>

<!-- spring mvc 文件上传支持 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 104857600 代表100MB -->
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="40960" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>

<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
beans.xml 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"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<context:component-scan base-package="com.entity"/>
<!-- 引入jdbc配置文件db.properties -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"/>
</bean>

<!-- 使用spring jdbc中的类建立数据源 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.pass}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${db.initialSize}"/>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${db.maxActive}"/>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${db.maxIdle}"/>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${db.minIdle}"/>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${db.maxWait}"/>
</bean>
<!-- SqlSessionFactory -->
<bean id="sf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<!-- com.entity.Student 可以写 sutdent -->
<property name="typeAliasesPackage" value="com.entity"/>
<property name="mapperLocations" value="classpath:com/entity/mapper/*Mapper.xml"/>
</bean>
<!-- SqlSession -->
<bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sf"/>
</bean>

<!--创建数据映射器,数据映射器必须为接口 -->
<!--
<bean id="ssm" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.entity.mapper.StudentMapper" />
<property name="sqlSessionFactory" ref="sf" />
</bean>
-->

<!-- 扫描mapper包 自动在spring中生成 xxxxMapper bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.entity.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sf"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

5、web项目的配置文件 /src/main/webapp/WEB-INF/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_3_1.xsd"
version="3.1">
<display-name>ssm</display-name>

<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- spring 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<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>

<!-- Spring MVC -->
<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>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
6、实体类编写com.entity.Student.java
package com.entity;
import lombok.Data;
/**
* Created by webrx on 2017/2/17 0017 10:52.
*/
@Data
public class Student {
private int id;
private String name;
private String address;
}
7、mybatis mapper类及mapper.xml文件
com.entity.mapper.StudentMapper.java 接口 Serverice DAO impl
package com.entity.mapper;
import com.entity.Student;
import org.apache.ibatis.annotations.Insert;
import java.util.List;
/**
* Created by webrx on 2017/2/17 0017 10:51.
*/
public interface StudentMapper {
public List<Student> query();
public int delById(int id);
@Insert("insert into student values(null,#{name},#{address})")
public int insert(Student student);
}

com.entity.mapper.StudentMapper.xml 配置文件
<?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.entity.mapper.StudentMapper">
<select id="query" resultType="student">
select * from student
</select>

<delete id="delById" parameterType="int">
delete from student where id = #{id}
</delete>
</mapper>

8、编写springmvc 控制器类 使用注解方式 com.controller.Hello.java
package com.controller;
import com.entity.Student;
import com.entity.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
* Created by webrx on 2017/2/17 0017 10:46.
*/
@Controller
public class Hello {
@Autowired //自动注入spring ioc 中的 studentMapper
private StudentMapper studentMapper;

@RequestMapping("/test")
public ModelAndView test(){
ModelAndView mv = new ModelAndView("show");
mv.addObject("student",studentMapper.query());
return mv;
}
@RequestMapping("/save")
public String save(Student student){
studentMapper.insert(student);
return "redirect:/test";
}

@RequestMapping("/del")
public String del(int id){
studentMapper.delById(id);
return "redirect:/test";
}
}

9、编写前台网页 /index.jsp springmvc视图文件 /show.jsp
/index.jsp 内容如下:
<%@ page language="java" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>ssm三大框架整合方案</title>
</head>
<body>
<a href="test">springmvc-controller-test</a>
<hr>
<%=request.getRemoteAddr()%>
<c:forEach begin="1" end="5" step="1">
<h2>SSM三大框架整合</h2>
</c:forEach>
</body>
</html>
/show.jsp 视图文件
<%@ page language="java" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>显示学生信息</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<h3>添加学生信息</h3>
<div>
<form action="save" method="post">
&nbsp;&nbsp;姓名: <input type="text" autofocus placeholder="姓名" name="name"><br>
家庭地址: <input type="text" name="address" placeholder="地址"><br>
<input type="submit" value="提交">
</form>
</div>
<h3>显示学生表的信息</h3>
<c:forEach items="${student}" var="st">
<div>${st.name}----<a href="del?id=${st.id}">删除</a></div>
</c:forEach>

</body>
</html>

10、运行项目测试
http://localhost/index.jsp 点击test

http://localhost/test

怕什么真理无穷,进一步有一步的欢喜
原文地址:https://www.cnblogs.com/Mkady/p/7201237.html