SSM环境搭建

1. 创建 maven 项目

创建 maven 项目,创建简单工程,打 war 包

SSM环境搭建

SSM环境搭建

创建完之后,项目由于缺少 web.xml 配置文件报错。

在 Project Facets 中,取消 Dynamic Web Module 选项,点击 apply,然后重新选中。点击 Further configuration available,配置 content directory。

SSM环境搭建

点确定后,自动生成 web.xml

SSM环境搭建

 

2. 引入需要的 jar

在 pom.xml 导入相关的 jar, jar 的坐标在 https://mvnrepository.com/ 在找到。

<dependencies>
	<!-- spring,springmvc,spring事务控制,aop -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>4.3.7.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>4.3.7.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aspects</artifactId>
		<version>4.3.7.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
		<version>4.3.7.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId> 
		<version>4.3.7.RELEASE</version>
	</dependency>
	<!-- spriingmvc -->
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-core</artifactId>
		<version>2.5.4</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>2.5.4</version>
	</dependency>
	<!-- mybatis和mybatis-spring整合的jar -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.2</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>1.3.1</version>
	</dependency>
	<!-- 数据库连接池,数据库驱动 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.10</version>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.41</version>
	</dependency>
	<!-- jstl,servlet-api,junit -->
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
		<scope>test</scope>
	</dependency>
	<!-- json -->
	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>fastjson</artifactId>
	    <version>1.2.35</version>
	</dependency> 
</dependencies>

 

3. 配置 web.xml 文件

一下配置几乎是所有ssm都需要的,以后直接复制即可

<!-- spring 容器 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:application.xml</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
	<servlet-name>dispatcher</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 如果不配置,默认用 web.xml同级的目录下 {servlet-name}-servlet.xml作为配置文件 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
	<param-value>classpath:servlet.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>dispatcher</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 编码过滤器 -->
<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>
	<!-- 新版本需要的配置,可以通过查看源码,确定是否要配置两个参数 -->
	<init-param>
		<param-name>forceRequestEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
	<init-param>
		<param-name>forceResponseEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

 

4. 创建 servlet.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/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-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- 两个标准配置 -->
	<!-- 将 springmvc 不能处理的请求交给 tomcat -->
	<mvc:default-servlet-handler/>
	<!-- 映射动态请求 -->
	<mvc:annotation-driven />	
	
	<!-- 扫描包,去除默认的过滤规则,只扫描 @Controller的类 -->
	<context:component-scan base-package="com.ws" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>	
	
	<!-- 配置页面视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 这里可以指定其他的文件目录 -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

 

5. 配置 mybatis 数据源

文件结构下图所示

SSM环境搭建

application.xml

这里 application.xml 没有直接写入 mybatis 的配置,只做了包的扫描,properties 的导入和spring其他的文件的导入。

这么做的好处是为了不让spring文件那么臃肿,便于修改,例如数据源的配置文件为 spring-jdbc,如果以后要加入 dubbo 的话,直接在 spring 文件夹下创建一个 spring-dubbo.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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- 扫描包,去除默认的过滤规则,只扫描除 @Controller之外的类,此处和sptingmvc配置不同 -->
	<context:component-scan base-package="com.ws" use-default-filters="false">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>		

	<!-- 引用配置文件 -->
	<context:property-placeholder location="classpath:properties/*-config.properties"/>
	
	<!-- 导入spring配置文件 -->
	<import resource="classpath:spring/spring-*.xml"/>
	
</beans>

spring-jdbc.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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- 扫描包,去除默认的过滤规则,只扫描除 @Controller之外的类,此处和sptingmvc配置不同 -->
	<context:component-scan base-package="com.ws"
		use-default-filters="false">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- 引用配置文件 -->
	<context:property-placeholder
		location="classpath:*.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="${jdbc.url}"></property>
		<property name="driverClassName" value="${jdbc.driverClass}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="${initialSize}"></property>
		<!-- 连接池最大数量 -->
		<property name="maxActive" value="${maxActive}"></property>
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="${maxIdle}"></property>
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="${minIdle}"></property>
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="${maxWait}"></property>
	</bean>

	<!-- 配置 spring,mybatis整合 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定 mapper,mybatis文件路径 -->
		<property name="mapperLocations" value="classpath:mybatis/mappings/*.xml"></property>
		<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
	</bean>

	<!-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->
	<bean id="appMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
		<property name="basePackage" value="com.ws" />
		<property name="annotationClass" value="com.ws.annotation.MyBatisDao" />
	</bean>

	<!-- 定义事务 -->
	<bean id="appTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 -->
	<tx:annotation-driven transaction-manager="appTransactionManager" proxy-target-class="true" />

</beans>

db-config.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/ams
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
#定义初始连接数  
initialSize=0  
#定义最大连接数  
maxActive=20  
#定义最大空闲  
maxIdle=20  
#定义最小空闲  
minIdle=1  
#定义最长等待时间  
maxWait=60000  

创建一个 MyBatisDao 的注解

这个注解是为mybatis加载时候只加载 @MyBatisDao 这个注解的接口。(这个配置:<property name="annotationClass" value="com.ws.annotation.MyBatisDao" />)

import org.springframework.stereotype.Repository;

@MyBatisDao 
public @interface MyBatisDao {
}

 

6. 测试 mybatis 配置

AccountEntity.java

    private Integer id;

	/**
	 * 用户名
	 */
	private String username;

	/**
	 * 密码
	 */
	private String password;

	/**
	 * 姓名
	 */
	private String name;

	/**
	 * 电话号码
	 */
	private String phone;

	/**
	 * 盐
	 */
	private String salt;

	/**
	 * 状态
	 */
	private String status;

	/**
	 * 创建人
	 */
	private String createdBy;

	/**
	 * 创建时间
	 */
	private Date createdDate;

	/**
	 * 创建人
	 */
	private String updatedBy;

	/**
	 * 创建时间
	 */
	private Date updatedDate;

    // 省略 setter 和 getter

AccountEntityMapper.java

@MyBatisDao
public interface AccountEntityMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(AccountEntity record);

    int insertSelective(AccountEntity record);

    AccountEntity selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(AccountEntity record);

    int updateByPrimaryKey(AccountEntity record);
    
    List<AccountEntity> selectAll();
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:application.xml"})
public class AccountMapperTest {
	
	@Autowired
	private AccountEntityMapper accountEntityMapper;

	@Test
	public void testAccountSelectAll() {
		List<AccountEntity> accountList = accountEntityMapper.selectAll();
		System.out.println(JSON.toJSONString(accountList));
	}
	
}

SSM环境搭建

 

7. 测试 springmvc

这里偷懒,直接省略了 service

AccountController.java

@RestController
@RequestMapping("/account")
public class AccountController {
	
	@Resource
	private AccountEntityMapper accountEntityMapper;
	
	@RequestMapping("/list")
	public List<AccountEntity> list() {
		List<AccountEntity> accountList = accountEntityMapper.selectAll();
		return accountList;
	}
	
}

SSM环境搭建

 

另外,这个有一个深坑的问题,在搭建过程中花费了不少时间,由于是在 eclipse 搭建的,eclipse 有自带 jre8,默认的情况下使用 jre8 会报错。换成自己安装的 jdk8 就好了。

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ssm-crud]]