SSM整合

Posted xzh0717

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合相关的知识,希望对你有一定的参考价值。

SSM整合

一、整合 spring与springMVC

Ⅰ)整合web层

1、创建web动态工程

2、导入spring包与配置文件

spring包
  - com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
  - com.springsource.org.aopalliance-1.0.0.jar
  - com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
  - com.springsource.org.apache.commons.logging-1.1.1.jar
  - com.springsource.org.apache.commons.pool-1.5.3.jar
  - com.springsource.org.apache.log4j-1.2.15.jar
  - com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  - druid-1.1.9.jar
  - hamcrest-core-1.3.jar
  - spring-aop-5.0.7.RELEASE.jar
  - spring-aspects-5.0.7.RELEASE.jar
  - spring-beans-5.0.7.RELEASE.jar
  - spring-context-5.0.7.RELEASE.jar
  - spring-core-5.0.7.RELEASE.jar
  - spring-expression-5.0.7.RELEASE.jar
  - spring-jdbc-5.0.7.RELEASE.jar
  - spring-orm-5.0.7.RELEASE.jar
  - spring-test-5.0.7.RELEASE.jar
  - spring-tx-5.0.7.RELEASE.jar
  - spring-web-5.0.7.RELEASE.jar
配置文件 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"
       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.ssm"/>

    <!--<bean id="" class=""/>-->
    
</beans>

3、在web.xml当中配置spring监听器

<?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的核心监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--
        加载Spring的配置文件的路径的
        默认加载的/WEB-INF/applicationContext.xml
     -->
    <context-param>
        <param-name> contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>

4、添加springMVC相关jar包与配置文件

相关jar包

  - spring-webmvc-5.0.7.RELEASE.jar

配置文件 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"
       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-4.0.xsd">

    <!--注解扫描-->
    <context:component-scan base-package="com.ssm">
        <!--只扫描控制器-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--静态资源访问-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

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

5、在web.xml当中配置springMVC前端控制器和编码

<!-- 解决post乱码问题 -->
<filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <!-- 设置编码参是UTF8 -->
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

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

<!-- 配置SpringMVC前端控制器 -->
<servlet>
    <servlet-name>mySpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 指定SpringMVC配置文件 -->
    <!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
    <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>mySpringMVC</servlet-name>
    <!-- 拦截所有,不包括jsp,包含.js .png.css     建议使用  -->
    <url-pattern>/</url-pattern>
</servlet-mapping>

6、测试springMVC

创建form表单

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>index</title>
  </head>
  <body>
  <form action="${pageContext.request.contextPath}/addcustomer">
    <p>客户名称:<input type="text"></p>
    <p>客户职业:<input type="text"/></p>
    <p>客户电话: <input type="text"/></p>
    <p>客户邮件: <input type="text"/></p>
    <input type="submit" value="添加">
  </form>
  </body>
</html>

CustomerController

@Controller
public class CustomerController {

    @RequestMapping("addcustomer")
    public String addCustomer(){
        return "/result.jsp";
    }

}

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>结果页</h1>
</body>
</html>

Ⅱ)整合 Service 层

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>index</title>
  </head>
  <body>
  <form action="${pageContext.request.contextPath}/addcustomer">
    <p>客户名称:<input type="text" name="cust_name"></p>
    <p>客户职业:<input type="text" name="cust_profession"/></p>
    <p>客户电话: <input type="text" name="cust_phone"/></p>
    <p>客户邮件: <input type="text" name="email"/></p>
    <input type="submit" value="添加">
  </form>
  </body>
</html>

Customer

@Getter@Setter@ToString
public class Customer {
    private Integer cust_id;
    private String cust_name;
    private String cust_profession;
    private String cust_phone;
    private String email;
}

CustomerService

public interface CustomerService {
    void saveCustomer(Customer customer);
}

CustomerServiceImpl

@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Override
    public void saveCustomer(Customer customer) {
        System.out.println("来到了业务层--" + customer);
    }
}

CustomerController

@Controller
public class CustomerController {

    /* 注入业务层 */
    @Autowired
    private CustomerService customerService;

    @RequestMapping("addcustomer")
    public String addCustomer(Customer customer){
        System.out.println("来到web层--" + customer);
        customerService.saveCustomer(customer);
        return "/result.jsp";
    }
}

Ⅲ )整合 dao 层

1、添加Mybatis相关jar包

  - ant-1.10.3.jar
  - ant-launcher-1.10.3.jar
  - asm-7.0.jar
  - cglib-3.2.10.jar
  - commons-logging-1.2.jar
  - javassist-3.24.1-GA.jar
  - log4j-1.2.17.jar
  - log4j-api-2.11.2.jar
  - log4j-core-2.11.2.jar
  - mybatis-3.5.3.jar
  - mybatis-spring-1.3.2.jar
  - ognl-3.2.10.jar
  - slf4j-api-1.7.26.jar
  - slf4j-log4j12-1.7.26.jar

2、添加Mybatis配置文件和数据库属性文件

sqlMapConfig.xml

<?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="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <!-- 批量别名定义,扫描整个包下的类,别名为类名(大小写不敏感) -->
        <package name="com.ssm.domain" />
    </typeAliases>
</configuration>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

3、创建CustomerMapper

CustomerMapper

public interface CustomerMapper {
    void insertCustomer(Customer customer);
}

CustomerMapper.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.ssm.mapper.CustomerMapper">

    <insert id="insertCustomer">
        insert into `customer`(cust_name,cust_profession,cust_phone,email)
        values (#{cust_name},#{cust_profession},#{cust_phone},#{email})
    </insert>

</mapper>

4、在applicationContext配置文件中添加Mybatis数据库相关配置信息

<?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.ssm"/>
    
    <!--spring-Mybatis整合-->
    <!--加载数据库属性文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <!--属性文件当中的名称不能和name名称一样-->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- Mybatis的工厂 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 核心配置文件的位置 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <!--配置mapper映射文件的路径-->
        <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"/>
    </bean>
    <!-- 配置Mapper接口扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置Mapper扫描包 -->
        <property name="basePackage" value="com.ssm.mapper" />
    </bean>
</beans>

5、调用dao层

@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void saveCustomer(Customer customer) {
        System.out.println("来到了业务层--" + customer);
        // 调用dao层
        customerMapper.insertCustomer(customer);
    }
}

以上是关于SSM整合的主要内容,如果未能解决你的问题,请参考以下文章

SSM框架整合

SSM 项目整合详细解读

SSM整合

520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“

shiro整合SSM(spring_springmvc_mybatis)

ssm整合redis