Java

JavaWeb | 基于maven整合SSM框架(Spring+

2019-12-18  本文已影响0人  一颗白菜_

一、目录总览

对于一般的项目来说,整合后的大体的目录如下图:


目录总览

下面就开始整合三大框架。

二、创建maven项目

maven是一个项目管理工具,其pom.xml可以用来管理和维护当前项目所使用的jar包。




三、完善项目目录

1、新建目录如下:

2、添加web目录

从上图中你可以看得出我们的项目没有web目录,因此我们要来添加web目录:





然后我们就能在项目目录中看到web目录了,我们接下来把web目录移动到main文件夹下:



在移动文件夹后,需要在idea中修改其web目录的路径,不然idea找不到该目录:

到此,我们已经把项目目录完善好了:


3、配置tomcat

四、maven添加依赖

1、添加数据库驱动依赖

添加数据库连接池驱动和数据库驱动的依赖:

        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.2</version>
        </dependency>
        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

2、添加SpringMVC依赖

        <!-- springmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

3、添加spring的一系列依赖

        <!-- spring jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- spring面向切面编程 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- Spring单元测试 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

4、添加MyBatis及其与Spring整合的依赖

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.4</version>
        </dependency>
        <!-- mybatis整合spring适配包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>

5、添加其它依赖

        <!-- servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>provided</scope>
        </dependency>
        <!-- 返回json字符串的支持 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>

五、配置

1、新建数据源资源文件dbconfig.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud?useUnicode=true&characterEncoding=UTF8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

设置useUnicode=true&characterEncoding=UTF8可以防止中文插入数据库的时候发生乱码。

2、配置spring配置文件

<?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/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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--自动扫描的包-->
    <context:component-scan base-package="com.cerr">
        <!-- 除了控制器不扫描 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 数据源 -->
    <context:property-placeholder location="classpath:dbconfig.properties" />
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="pooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 与mybatis整合-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
        <!--指定全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 指定数据源 -->
        <property name="dataSource" ref="pooledDataSource" />
        <!-- 指定mapper文件的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <!-- 配置扫描器:将mybatis接口的实现加入到ioc容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
        <!-- 扫描所有dao接口的实现,加入到ioc容器中-->
        <property name="basePackage" value="com.cerr.dao"/>
    </bean>

    <!-- 配置批量操作的SqlSession -->
    <bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
        <constructor-arg name="executorType" value="BATCH"/>
    </bean>
    <!-- 事务控制 -->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <!-- 控制住数据源 -->
        <property name="dataSource" ref="pooledDataSource" />
    </bean>

    <!-- 开启基于注解的事务,使用xml配置形式的事务 -->
    <aop:config>
        <!-- 切入点表达式 -->
        <aop:pointcut id="txPoint" expression="execution(* com.cerr.service..*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>

    <!-- 配置事务增强:事务如何切入-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 所有方法都是事务方法 -->
            <tx:method name="*"/>
            <!-- get* 表示查询方法 -->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
</beans>

Spring配置扫描包的时候,为了不和SpringMVC扫描包冲突,一定要配置不扫描 Controller的包。同理中,在SpringMVC中只扫描Controller的包。

3、配置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/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">

    <!-- 扫描所有的包 -->
    <context:component-scan base-package="com.cerr" 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 name="suffix" value=".jsp"/>
    </bean>
    <!-- 将SpringMVC不能处理的请求交给tomcat-->
    <mvc:default-servlet-handler/>
    <!-- 能支持SpringMVC更高级的一些功能 -->
    <mvc:annotation-driven/>
</beans>

4、配置MyBatis全局配置文件

<?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>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.cerr.dao"/>
    </typeAliases>
</configuration>

因为关于数据库和一些MyBatis的配置都在Spring的配置中配置了,因此该配置文件比较简单。

5、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 启动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>

    <!-- 字符过滤器 -->
    <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>
        <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>characterEncodingFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

    <!-- 使用Rest风格的URI:将页面普通的post请求转为指定的delete或者put请求-->
    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 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:springDispatcherServlet-servlet.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>

六、编码阶段

在此我们先简单介绍下ServiceControllerDao层:

1、数据表

有一个tbl_emp表如下:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tbl_emp
-- ----------------------------
DROP TABLE IF EXISTS `tbl_emp`;
CREATE TABLE `tbl_emp`  (
  `emp_id` int(11) NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gender` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `email` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `d_id` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`emp_id`) USING BTREE,
  
) 

-- ----------------------------
-- Records of tbl_emp
-- ----------------------------
INSERT INTO `tbl_emp` VALUES (2, 'Jerry', 'M', 'Jerry@cerr.com', 1);
INSERT INTO `tbl_emp` VALUES (3, '17d860', 'M', '17d860@cerr.com', 1);
INSERT INTO `tbl_emp` VALUES (4, '4da0e1', 'M', '4da0e1@cerr.com', 1);
INSERT INTO `tbl_emp` VALUES (5, 'e75602', 'M', 'e75602@cerr.com', 1);
INSERT INTO `tbl_emp` VALUES (6, 'e23ca3', 'M', 'e23ca3@cerr.com', 1);
INSERT INTO `tbl_emp` VALUES (7, 'f2f314', 'M', 'f2f314@cerr.com', 1);
INSERT INTO `tbl_emp` VALUES (8, '8a23a5', 'M', '8a23a5@cerr.com', 1);

SET FOREIGN_KEY_CHECKS = 1;

2、JavaBean类

新建一个Employee类,代码如下:

package com.cerr.bean;

public class Employee {
    private Integer empId;
    private String empName;
    private String gender;
    private String email;
    private Integer dId;

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getdId() {
        return dId;
    }

    public void setdId(Integer dId) {
        this.dId = dId;
    }

    public Employee(Integer empId, String empName, String gender, String email, Integer dId) {
        this.empId = empId;
        this.empName = empName;
        this.gender = gender;
        this.email = email;
        this.dId = dId;
    }

    public Employee() {
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                ", dId=" + dId +
                '}';
    }
}

3、dao层

编写EmployeeMapper接口:

package com.cerr.dao;

import com.cerr.bean.Employee;

import java.util.List;

public interface EmployeeMapper {
    //获取所有的getEmps
    List<Employee> getEmps();
}

编写SQL映射文件(EmployeeMapper.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.cerr.dao.EmployeeMapper">
    <select id="getEmps" resultType="com.cerr.bean.Employee">
        select * from tbl_emp ;
    </select>
</mapper>

4、Service层

package com.cerr.service;

import com.cerr.bean.Employee;
import com.cerr.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper mapper;

    public List<Employee> getAll(){
        List<Employee> employees = mapper.getEmps();
        return employees;
    }
}

在Spring的配置文件中我们将所有的mapper添加到IOC容器中了,因此在此可以使用@Autowired注解来注入EmployeeMapper的代理对象,如果没有添加到IOC容器则会报错。

5、Controller层:返回JSON数据

package com.cerr.controller;
import com.cerr.bean.Employee;
import com.cerr.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @ResponseBody
    @RequestMapping(value = "/emps")
    public List<Employee> getEmps(){
        List<Employee> employees = new ArrayList<Employee>();
        employees = employeeService.getAll();
        return employees;
    }
}

对于Controller层,是处理请求用的,@RequestMapping用于映射请求地址用的,@ResponseBody注解是用于将返回的对象通过一定的类型转换处理转换为指定的类型。因为我们在引入了json的依赖,因此会转换为JSON数据。

我们通过http://localhost:8080/ssm_test1/emps发送请求之后,数据在被查出来之后就转换为JSON数据然后在页面显示,当然我们可以使用AJAX发请求然后接收数据进行处理:

6、Controller层:返回页面视图

上面那种返回JSON的可以适用于服务端和前端分离的情况,如果我们想直接返回视图的话可以使用下面这种:

    <!-- 配置视图解析器:方便页面返回-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

因此我们需要在WEB_INF目录下新建一个views目录,并新建一个success.jsp

然后我们在EmployeeController里面编码:

package com.cerr.controller;

import com.cerr.bean.Employee;
import com.cerr.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping(value = "/")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/empView")
    public String getEmpsWithView(Map<String,Object> map){
        List<Employee> employees = employeeService.getAll();
        if(employees.size() > 0){
            map.put("employees",employees);
        }
        return "success";
    }
}

在方法入参中传入一个map,在方法返回时,SpringMVC会自动将该map加入到SpringMVC的数据模型中,因此可以到页面直接在request域中获取,详情可看文章:SpringMVC中处理模型数据的几种方法(ModelAndView|@SessionAttributes|@ModelAttribute)以及运行原理

jsp页面就一句代码:${requestScope.employees}
在浏览器中发送请求后结果如下图,将request域中的employee集合打印出来。

上一篇下一篇

猜你喜欢

热点阅读