SSM整合项目

本文最后更新于:1 年前

1. 创建web项目

  1. 创建maven工程;

  2. 修改pom文件 :

    1
    <packaging>war</packaging>
  3. 完成maven工程web项目结构;

  4. 添加web项目依赖:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
    </dependency>
  5. 配置服务器运行环境。

2. 部署MyBatis

  1. 添加MyBatis依赖:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <!-- mysql驱动 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
    </dependency>

    <!-- mybatis依赖 -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
    </dependency>

    <!-- lombok -->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.16</version>
    <scope>provided</scope>
    </dependency>
  2. 创建MyBatis配置文件 mybatis-config.xml

    1
    2
    3
    4
    5
    6
    <?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>

    </configuration>

3. 部署Spring、SpringMVC

3.1 添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<properties>
<spring.version>5.2.13.RELEASE</spring.version>
</properties>

<!--context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--aspects-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--jackson-databind-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>

3.2 创建Spring配置

多配置文件分开配置:

  • spring-context.xml 只配置注解声明、以及类的管理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 声明使用注解配置 -->
    <context:annotation-config/>
    <!-- 声明Spring工厂注解的扫描范围 -->
    <context:component-scan base-package="com.qfedu"/>

    </beans>
  • spring-mvc.xml 进行mvc相关的配置,例如静态资源配置、拦截器配置等

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?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: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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--声明MVC使用注解配置-->
    <mvc:annotation-driven/>

    </beans>
  • spring-mybatis.xml 进行Spring与MyBatis整合相关的配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <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.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    </beans>

3.3 配置SpringMVC前端控制器

在web.xml进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
<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:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

4. 整合配置(IoC)

4.1 导入mybatis-spring依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>

4.2 配置druid连接池

  1. 添加druid依赖

    1
    2
    3
    4
    5
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
    </dependency>
  2. 创建druid.properties,并配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    druid.driver=com.mysql.jdbc.Driver
    druid.url=jdbc:mysql://localhost:3306/db_2010_mybatis?characterEncoding=utf-8
    druid.username=root
    druid.password=admin123

    ## 连接池参数
    druid.pool.init=1
    druid.pool.minIdle=3
    druid.pool.maxActive=20
    druid.pool.timeout=30000
  3. 在spring-mybatis.xml配置数据源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <context:property-placeholder location="classpath:druid.properties"/>

    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${druid.driver}"/>
    <property name="url" value="${druid.url}"/>
    <property name="username" value="${druid.username}"/>
    <property name="password" value="${druid.password}"/>

    <property name="initialSize" value="${druid.pool.init}"/>
    <property name="minIdle" value="${druid.pool.minIdle}"/>
    <property name="maxActive" value="${druid.pool.maxActive}"/>
    <property name="maxWait" value="${druid.pool.timeout}"/>
    </bean>

4.3 配置SqlSessionFactory

在 spring-mybatis.xml 配置:

1
2
3
4
5
6
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
<property name="typeAliasesPackage" value="com.qfedu.bean"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

4.4 配置MapperScannerConfigurer

在spring-mybatis.xml配置:

1
2
3
4
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.qfedu.dao"/>
</bean>

5. 整合配置(AOP)

使用Spring提供的事务管理完成DAO操作的事务管理。

这里采用基于注解的事务管理配置,将Spring提供的事务管理切面类配置到Spring容器:

1
2
3
4
5
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"/>
</bean>

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

6 整合测试

6.1 完成User的查询操作

  1. 创建实体类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public class User {

    private int userId;
    private String userName;
    private String userPwd;
    private String userRealname;
    private String userImg;

    }
  2. 在DAO包中创建接口

    1
    2
    3
    4
    5
    public interface UserDAO {

    public User queryUserByName(String name);

    }
  3. 在mappers目录下创建映射文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <?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.qfedu.dao.UserDAO">

    <resultMap id="userMap" type="User">
    <id column="user_id" property="userId"/>
    <result column="user_name" property="userName"/>
    <result column="user_pwd" property="userPwd"/>
    <result column="user_realname" property="userRealname"/>
    <result column="user_img" property="userImg"/>
    </resultMap>


    <select id="queryUserByName" resultMap="userMap">
    select user_id,user_name,user_pwd,user_realname,user_img
    from users
    where user_name=#{userName}
    </select>

    </mapper>

6.2 对DAO单元测试

  1. 添加junit、spring-test依赖

    1
    2
    3
    4
    5
    6
     <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>
  2. 创建测试类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"classpath:spring-context.xml","classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
    public class UserDAOTest {

    @Resource
    private UserDAO userDAO;

    @Test
    public void queryUserByName() {
    User user = userDAO.queryUserByName("wangwu");
    System.out.println(user);
    }
    }
  3. 运行测试

6.3 对Service单元测试

  1. 在service包中创建接口

    1
    2
    3
    4
    5
    public interface UserService {

    public User checkLogin(String username,String userPwd);

    }
  2. 创建service接口的实现类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    @Service
    public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public User checkLogin(String userName, String userPwd) {
    User user = userDao.queryUserByName(userName);
    if (user.getUserPwd().equals(userPwd)) {
    return user;
    } else {
    return null;
    }
    }

    }
  3. 创建测试类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"classpath:spring-context.xml","classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
    public class UserServiceImplTest {

    @Resource
    private UserService userService;

    @Test
    public void testCheckLogin(){
    User user = userService.checkLogin("wangwu", "111111");
    assertNotNull(user);
    }

    }
  4. 运行测试

6.4 Cotroller层操作

  1. controller层

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    @Controller
    @RequestMapping("/user")
    public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String login(String userName, String userPwd, HttpServletRequest request){
    User user = userService.checkLogin(userName, userPwd);
    if (user == null){
    request.setAttribute("tips","用户名或密码错误!");
    return "/login.jsp";
    } else {
    request.getSession().setAttribute("user",user);
    return "redirect:/index.jsp";
    }
    }
    }
  2. 前端 login.jsp 页面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <head>
    <base href="${pageContext.request.contextPath}/">
    <title>Title</title>
    </head>
    <body>
    <h3>登录页面</h3>
    ${tips}
    <form action="user/login"method="post">
    <p>帐号:<input type="text"name="userName"/></p>
    <p>密码:<inputtype="password"name="userPwd"/></p>
    <p><input type="submit"value="登录"/></p>
    </form>
    </body>
  3. 页面测试


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!