ssm框架搭建
1、前言
SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛。
- Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP。
- SpringMVC是Spring实现的一个Web层,相当于Struts的框架,但是比Struts更加灵活和强大!
- Mybatis是 一个持久层的框架,在使用上相比Hibernate更加灵活,可以控制sql的编写,使用 XML或注解进行相关的配置!
2、框架搭建概述
准备工作:- STS编译器,这个一个集成了spring环境的eclicpse,可以在spring的官网去下载。
- maven创建的Javaweb工程
3、框架搭建
3.1 ssm搭建完成整体架构
3.2 maven依赖注入
这里只注入一些ssm框架需要的基本jar,后需需要更多功能需求的可自行添加。
数据库连接池这里使用的是C3P0连接池
注意:spring和springmvc的版本要一致,mybatis版本要选能与spring兼容的版本,关于版本我在网上也没有找到有效的答案,只能多去试几个版本。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
3.2 创建工程的包目录及测试所需类
这一步把mvc模式下所需的包和用来测试的类都写好,有DAO层、model层、service层、controller层,图中Mapper包及对应的DAO层
3.3 配置springmvc
在文件夹下创建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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<mvc:annotation-driven />
<!-- 注解包扫描,只扫描controller层下的注解 -->
<context:component-scan
base-package="com.wfq.ssm.controller" />
<!-- 视图解析器 -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 访问路径前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 访问路径前缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.4 创建数据库连接信息文件和log4j日志文件
jdbc.properties:
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring
log4j.properties:
log4j.rootLogger=INFO,Console,File
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
log4j.appender.File.File = logs/ssm.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
3.5 配置spring
创建 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/context"
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-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.wfq.ssm">
<!-- 不扫描controller包下的注解,这是springmvc的事 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
3.6 整合spring和mybatis
这步是要在spring配置文件applicationContext.xml中完成的
3.6.1 创建mapper文件夹
在 classpath 路径下创建mapper文件,这里要存放每个DAO层接口对应的实现mybatis文件
3.6.2 applicationContext.xml中整合mybatis
<?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: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-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.wfq.ssm">
<!-- 不扫描controller包下的注解,这是springmvc的事 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 导入jdbc.properties -->
<context:property-placeholder
location="classpath:jdbc.properties" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean>
<!-- spring与mybaits整合 -->
<bean id="sqlSessionFactoryBean"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 该属性值为存放 mybatis中mapper文件的位置 -->
<property name="mapperLocations"
value="classpath:mapper/*.xml"></property>
</bean>
<!-- mapper文件对应的dao -->
<bean id="mapperScannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 该属性值为mapper文件对应的DAO层包路径 -->
<property name="basePackage" value="com.wfq.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactoryBean"></property>
</bean>
<!-- 开启事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
</beans>
3.7 web.xml中注册spring和springmvc前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!-- 启动spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置springmvc前端控制器 -->
<servlet>
<servlet-name>springDispatcherServlet</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>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
到这里呢ssm框架的搭建就完了,接下来连编码,走一下mvc的完整流程,测试一下ssm是否搭建成功
4、测试
编写程序,从数据库中获取所有用户的信息,在前端页面中展示
4.1 model层
@Component
public class User {
private int id;
private String username;
private String password;
public User() {
super();
System.out.println("User被实例化");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
4.2 dao层
public interface UserMapper {
public List<User> getAllUser();
}
4.3 service层
@Service("userService")
public class UserService {
@Resource(name="userMapper")
UserMapper userMapper;
public List<User> getAllUser(){
return userMapper.getAllUser();
}
}
4.4 controller层
@Controller
public class UserController {
@Resource(name="userService")
UserService userService;
/*
* 查询所有用户
*/
@RequestMapping(value="/users",method=RequestMethod.GET)
public String index(Model model) {
List<User> users = userService.getAllUser();
model.addAttribute("users", users);
return "users";
}
}
4.5 UserMapper.xml中写入sql
<?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>标签用来使该xml文件与接口对应,'namespace'属性的参数应该是与xml文件相对应的接口的类路径,本例中就是我们第5步所写的'userMapper'接口 -->
<mapper namespace="com.wfq.ssm.mapper.UserMapper">
<select id="getAllUser" resultType="com.wfq.ssm.model.User">
select * from user
</select>
</mapper>
4.6 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>
<html>
<head>
<meta charset="ISO-8859-1">
<title>获取全部用户信息</title>
</head>
<body>
<table border="1" >
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
</tr>
<c:forEach var="user" items="${requestScope.users }" >
<tr>
<td>${user.id }</td>
<td>${user.username }</td>
<td>${user.password }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
4.7测试成功
5、搭建过程中需要注意的问题
- spring与springmvc版本要一致
- mybatis版本要与spring兼容,这个问题我在网上没找到有效的答案,只能通过一个一个去试,有好方法的小伙伴可以在下面留言板知道一下
- mybatis的dao层实现文件mapper尽量要放在资源路径下(classpath),在学mybatis时,mapper是可以放在dao层包下,但是ssm中这种我不懂怎么去配置,有懂的朋友在留言板中知道一下。