1.1  快速搭建Web项目

注意点:name:archetypeCatalog,value:internal

原因:Intellij IDEA根据maven archetype的本质,执行mvn archetype:generate命令。该命令执行时,需要指定一个archetype-catalog.xml文件。该命令的参数-DarchetypeCatalog,可选值为:remote,internal,local等,用来指定archetype-catalog.xml文件从哪里获取,默认为remote,即从 http://repo1.maven.org/maven2/archetype-catalog.xml路径下载archetype-catalog.xml文件。archetype-catalog.xml 文件约为3-4M,下载速度很慢,导致创建过程卡住。解决的办法很简单,指定-DarchetypeCatalog为internal,即可使用maven默认的archetype-catalog.xml,而不用从remote下载。

1.2  集成Spring框架

1、首先,在ssm-project项目的pom文件中添加spring相关的依赖,具体代码如下:

<properties>
<spring.version>5.0.4.RELEASE</spring.version>
</properties>
<!--spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</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-aspects</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring end -->

2、其次,在/src/main/resources目录下创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.xjx"/> </beans>

3、接着,我们在web.xml配置文件中添加如下的代码:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<displ-name>Archetype Created Web Application</displ-name>
<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>
</web-app>

4:最后,在src/main/test/com.xjx.test目录下创建SpringTest测试类,具体代码如下:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class SpringTest { @Test
public void testSpring(){
//获取运用上下文
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//获取SpringTest类
SpringTest springTest = (SpringTest) applicationContext.getBean("springTest");
//调用sHello方法
springTest.sHello();
} public void sHello(){
System.out.println("hello ");
}
}

1.3  3种ApplicationContext实现

ApplicationContext容器:ApplicationContext是Spring中较高级的容器,它可以加载配置文件中定义的Bean,并将所有的Bean 集中在一起,当有请求的时候分配Bean。

最经常被使用的ApplicationContext接口实现如下:

ClassPathXmlApplicationContext从类路径ClassPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作,具体代码如下:

//装载单个配置文件实例化ApplicationContext容器
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
//装载多个配置文件实例化ApplicationContext容器
String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);

FileSystemXmlApplicationContext从指定的文件系统路径中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。具体代码如下:

//装载单个配置文件实例化ApplicationContext容器
ApplicationContext cxt = new FileSystemXmlApplicationContext ("beans.xml");
//装载多个配置文件实例化ApplicationContext容器
String[] configs = {"c:/beans1.xml","c:/beans2.xml"};
ApplicationContext cxt = new FileSystemXmlApplicationContext(configs);

XmlBeanFactory引用资源:

Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
TestController TestController =
(TestController) factory.getBean("TestController");
TestController.hello();

XmlWebApplicationContext从Web应用中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。这是为Web工程量身定制的,使用WebApplicationContextUtils类的getRequiredWebApplicationContext方法可在JSP与Servlet中取得IoC容器的引用。

1.4  集成Spring MVC框架

首先,我们把集成Spring MVC所需要的Maven依赖包和相关的属性值添加到pom.xml文件中,具体代码如下:

<properties>
<spring.version>5.0.4.RELEASE</spring.version>
<javax.servlet.version>4.0.0</javax.servlet.version>
<jstl.version>1.2</jstl.version>
</properties>
<!--springmvc start -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--springmvc end -->

其次,在web.xml配置文件中添加DispatcherServlet配置,具体代码如下:

<!--配置DispatcherServlet -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置SpringMVC需要加载的配置文件 spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<!--默认匹配所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>

接着,我们在/src/main/resources目录下创建配置文件spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--扫描controller(后端控制器),并且扫描其中的注解-->
<context:component-scan base-package="com.xjx.controller"/>
<!--设置配置方案 -->
<mvc:annotation-driven/> <!--配置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/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

我们在/src/main/webapp/WEB-INF目录下创建views文件夹,在views文件下创建hello.jsp文件,具体代码如下:

<%@page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body> hello, </body>
</html>

1.5  集成MyBatis框架

首先,我们把集成MyBatis框架所需要的依赖包添加到pom.xml文件中,具体代码如下:

<properties>
<spring.version>5.0.4.RELEASE</spring.version>
<javax.servlet.version>4.0.0</javax.servlet.version>
<jstl.version>1.2</jstl.version>
<mybatis.version>3.4.6</mybatis.version>
<mysql.connector.java.version>8.0.9-rc</mysql.connector.java.version>
<druid.version>1.1.9</druid.version>
<mybatis.spring.version>1.3.2</mybatis.spring.version>
</properties>
<!--mybatis start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.java.version}</version>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!--mybatis end -->

这里用的是阿里巴巴druid数据源

其次,在/src/main/resources目录下创建jdbc.properties配置文件,具体代码如下:

//驱动
jdbc.driverClassName=com.mysql.jdbc.Driver
//mysql连接信息
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssmpro?serverTimezone=GMT
//用户名
jdbc.username=root
//密码
jdbc.password=123456

jdbc.properties配置:主要配置驱动和连接数据库的配置信息。

最后,我们在applicationContext.xml配置文件添加如下的配置,具体代码如下

<!--1、配置数据库相关参数-->
<context:property-placeholder
location="classpath:jdbc.properties" ignore-unresolvable="true"/> <!--2.数据源 druid -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--3、配置SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--扫描sql配置文件:mapper需要的xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean> <!--扫描basePackage下所有以@MyBatisDao注解的接口 -->
<bean id="mapperScannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.xjx.dao"/>
</bean>

我们在/src/main/java/com.xjx.model目录下创建数据库表对应的实体类对象User,具体的代码如下:

public class User implements Serializable{

    private Integer id;
private String name;
private String password; //省略set、get方法
}

实体类对象User创建完成之后,我们在/src/main/java/com.xjx.dao目录下创建对应的DAO对象UserDao,UserDao是一个接口,提供了findAll方法用来查询所有的用户。UserDao具体代码如下:

package com.xjx.dao;
import com.xjx.model.User;
import org.springframework.stereotype.Repository;
import java.util.List; @Repository
public interface UserDao { List<User>findAll();
}

接口类UserDao创建完成之后,我们在/src/main/java/com.xjx.service目录下创建对应的服务层接口UserService,服务层接口UserService代码也非常简单,只提供了一个查询所有用户的方法findAll(),具体的代码如下:

package com.xjx.service;
import com.xjx.model.User;
import java.util.List; public interface UserService { List<User>findAll();
}

服务层接口UserService开发完成之后,我们在/src/main/java/com.xjx.service.impl开发对应的服务层实现类UserServiceImpl,实现类主要是注入UserDao接口,并实现findAll()方法,在findAll()方法中调用UserDao的findAll()方法,具体代码如下所示:

package com.xjx.service.impl;
import com.xjx.dao.UserDao;
import com.xjx.model.User;
import com.xjx.service.UserService;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{ @Resource
private UserDao UserDao; public List<User>findAll() {
return UserDao.listAllUser();
}
}

服务层实现类UserServiceImpl开发完成之后,我们在/src/main/java/com.xjx.controller目录下创建控制层类UserController,并注入服务层接口。UserController类只有一个findAll()方法。在UserController类上添加映射路径/user,在findAll()方法上添加映射路径/findAll。

package com.xjx.controller;
import com.xjx.model.User;
import com.xjx.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List; @Controller
@RequestMapping(value = "/user")
public class UserController { @Resource
private UserService UserService; @GetMapping("/findAll")
public String findAll(Model model){
List<User> UserList = UserService.findAll();
for(User User : UserList){
System.out.println("id: " + User.getId());
System.out.println("name: " + User.getName());
}
return "hello";
}
}

最后,我们在/src/main/resources目录下创建UserMapper.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.xjx.dao.UserDao">
<sql id="userField">
a.id as "id",
a.name as "name",
a.password as "password"
</sql>
<!--获取所有用户 -->
<select id="findAll" resultType="com.xjx.model.User">
select
<include refid="userField"/>
from _user as a
</select> </mapper>

1.6  集成Log4J框架

首先需要在pom.xml文件中引入所需的依赖,具体代码如下:

!-- log4j2 -->
<properties>
//省略部分代码
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>

我们在项目的/src/main/java/resources/下创建配置文件log4j.properties,具体代码如下所示:

###set log levels
log4j.rootLogger = DEBUG,Console ###输出到控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.lout=org.apache.log4j.PatternLout
log4j.appender.Console.lout.ConversionPattern= %d{ABSOLUTE} %5p %c{1}:%L - %m%n

1.7  集成Junit测试框架

首先,我们在项目的pom.xml配置文件中添加相关的依赖,具体代码如下:

<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

然后,我们在项目的/src/main/test/com.xjx.test目录下创建测试基类BaseJunit4Test,具体代码如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BaseJunit4Test {
}

@RunWith参数化运行器,用于指定Junit运行环境,是Junit提供给其它框架测试环境接口扩展,为了便于使用Spring的依赖注入,Spring提供了SpringJUnit4ClassRunner作为Junit测试环境。

BaseJunit4Test类开发完成之后,我们在/src/main/test/com.xjx.test目录下创建UserDaoTest测试类简单测试集成Junit框架是否成功,具体代码如下:

public class UserDaoTest extends BaseJunit4Test{

    @Resource
private UserDao UserDao; @Test
public void testFindAll(){
List<User> userList = UserDao.findAll();
System.out.println(userList.size());
}
}

最后,在src/main/test/com.xjx.test目录下创建SpringTest测试类,具体代码如下:

05-11 13:03