SSM框架整合

SSM框架整合
工程下载: https://download.csdn.net/download/qq_40547249/12897499

  1. 使用Maven创建一个web-app项目
  2. 导入依赖
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. 在web.xml中配置前端控制器以及字符过滤器
<filter>
    <filter-name>characterEncodingFilter</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>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- springmvc 配置文件位置-->
    <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>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
  1. 编写SpringMVC配置文件springmvc.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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描控制器类 -->
    <context:component-scan base-package="cn.decade.controller"/>

    <!-- 配置视图资源解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
  1. 编写控制器类UserController以及实体类User
package cn.decade.controller;

import cn.decade.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author dongjie
 * @since 2020/9/29 9:15
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        model.addAttribute("msg", "Good Good Study!");
        return "success";
    }

    @RequestMapping("updateUser")
    public String updateUser(User user, Model model) {
        System.out.println(user);
        model.addAttribute("msg", "Day Day Up!");
        return "success";
    }

}
package cn.decade.domain;

/**
 * @author dongjie
 * @since 2020/9/29 9:21
 */
public class User {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  1. 编写jsp界面
<%--
  Created by IntelliJ IDEA.
  User: dongjie
  Date: 2020/9/29
  Time: 9:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>
<body>

<a href="user/findAll"><p>查询所有用户</p></a>

<form action="user/updateUser">

    <label for="name">
        姓名:
        <input type="text" name="name" id="name">
    </label>

    <label for="age">
        年龄:
        <input type="text" name="age" id="age">
    </label>

    <input type="submit" value="提交">

</form>

</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: dongjie
  Date: 2020/9/29
  Time: 9:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
         isELIgnored="false" %>
<html>
<head>
    <title>成功界面</title>
</head>
<body>

<h3>SUCCESS!</h3>

<h4>${msg}</h4>

</body>
</html>

  1. 对SpringMVC进行测试, 一切正常
  2. 添加业务层代码
package cn.decade.service;

import cn.decade.domain.User;

import java.util.List;

/**
 * @author dongjie
 * @since 2020/9/29 9:29
 */
public interface IUserService {

    List<User> findAll();

    boolean updateUser(User user);

}
package cn.decade.service;

import cn.decade.domain.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dongjie
 * @since 2020/9/29 9:30
 */
@Service
public class UserServiceImpl implements IUserService {

    @Override
    public List<User> findAll() {
        User user = new User();
        user.setName("fanghua");
        user.setAge(16);
        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        return users;
    }

    @Override
    public boolean updateUser(User user) {
        System.out.println("业务层: " + user);
        return false;
    }
}
  1. 修改Controller代码
package cn.decade.controller;

import cn.decade.domain.User;
import cn.decade.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author dongjie
 * @since 2020/9/29 9:15
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        model.addAttribute("msg", "Good Good Study!");
        userService.findAll().forEach(System.out::println);
        return "success";
    }

    @RequestMapping("updateUser")
    public String updateUser(User user, Model model) {
        System.out.println("Controller层: " + user);
        model.addAttribute("msg", "Day Day Up!");
        userService.updateUser(user);
        return "success";
    }

}
  1. 在web.xml中加入ContextLoaderListener监听器,并指定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>
  1. 进行测试, 成功
  2. 引入MyBatis以及数据库,数据源相关依赖,还需引入spring-tx包以及spring-jdbc包
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.5</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.5</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.21</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.24</version>
</dependency>
  1. 编写dao层
package cn.decade.dao;

import cn.decade.domain.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author dongjie
 * @since 2020/9/29 9:45
 */
@Repository
public interface UserMapper {
    
    @Select("SELECT * FROM user")
    List<User> findAll();
    
    @Update("UPDATE user SET age = #{age} WHERE name = #{name}")
    int updateUser(User user);
    
}
  1. 配置Spring配置文件application.xml
<!-- 数据源配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://121.36.39.185:3306/user"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>

<!-- sqlSessionFactory工厂设置 -->
<bean id="factoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- mapper文件扫描 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.decade.dao"/>
</bean>
  1. 修改Service层代码
package cn.decade.service;

import cn.decade.dao.UserMapper;
import cn.decade.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author dongjie
 * @since 2020/9/29 9:30
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public List<User> findAll() {
        return mapper.findAll();
    }

    @Override
    public boolean updateUser(User user) {
        return mapper.updateUser(user) > 0;
    }
}
  1. 修改Controller层代码
package cn.decade.controller;

import cn.decade.domain.User;
import cn.decade.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author dongjie
 * @since 2020/9/29 9:15
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        model.addAttribute("msg", "Good Good Study!");
        userService.findAll().forEach(System.out::println);
        return "success";
    }

    @RequestMapping("updateUser")
    public String updateUser(User user, Model model) {
        System.out.println("Controller层: " + user);
        model.addAttribute("msg", "Day Day Up!");
        userService.updateUser(user);
        return "success";
    }

}
  1. 测试,成功.
错误汇总
  1. java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport

    解决: 引入spring-tx包

  2. java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy

    解决: 引入spring-jdbc包

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页