15.ssm框架的整合

1.先导入依赖pom.xml

1.思路

第一步:先独立写spring的实现模块

第二步:先独立写springmvc的实现模块

第三步:整合spring和springmvc(使用web.xml中的监听实现【将实现applicationContext.xml一开始就被扫描到】)

第四步:独立写mybatis的实现模块

第五步:整合spring和mybatis(将mybatis的SqlMapConfig.xml文件放到spring容器的中去管理:【将动态的dao放到spring中去管理】)

整体思想:表现层(controller)调用业务层(service【@Service("accountService")】)调用持久层(dao)

通过:

结构图:

1.先独立写spring的实现模块

a.使用Account实体类

package com.itheima.pojo;

public class Account {
    private Integer id;
    private Integer uid;
    private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }
}

b.编写service层(spring业务层)接口IAccountService

package com.itheima.service;

import com.itheima.pojo.Account;

import java.util.List;

public interface IAccountService {
    //查询所有
    public List<Account> findAll();
}

c.编写service层(spring业务层)接口实现类IAccountServiceimpl

package com.itheima.service.impl;

import com.itheima.controller.AccountController;
import com.itheima.dao.IAccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

public class AccountServiceImpl implements IAccountService {

    @Override
    public List<Account> findAll() {
        System.out.println("service被调用----spring查询成功......");
        return accountDao.findAll();
    }
}

d.编写service层测试类测试spring环境是否搭建成功

package com.itheima.test;

import com.itheima.service.IAccountService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

    @Test
    public void testQueryAll() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        IAccountService service = ac.getBean(IAccountService.class); // 因为给service起了别名,所以通过id的方式获取class
        service.findAll();

    }
}

e.编写spring主配置文件: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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <!--只处理Service和dao -->
    <context:component-scan base-package="com">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


</beans>

f.运行成功(我就不晒截图了)

第二步:独立写springmvc的实现模块

a.编写表现层(AccountController)

package com.itheima.controller;

import com.itheima.dao.IAccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    @RequestMapping("/findAll")
    public String findAll(){
        System.out.println("springmvc控制层的输出.....");
        
    }
}

b.编写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"
       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">

    <!--开启注解扫描,只扫描controller-->
    <context:component-scan base-package="com">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

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

    <!--过滤静态资源-->
    <!--    <mvc:resources mapping="/css/**" location="/css/"/>-->
    <!--    <mvc:resources mapping="/images/**" location="/images/"/>-->
    <!--    <mvc:resources mapping="/js/**" location="/js/"/>-->
    <!--开启springmvc注解支持-->
    <mvc:annotation-driven/>

</beans>

c.配置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>
  <display-name>Archetype Created Web Application</display-name>

  <!--前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc.xml配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动服务器时,创建该servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--配置中文乱码过滤器-->
  <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>
</web-app>

d.index.jsp首页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
    <h3>欢迎来到首页</h3>
    <a href="account/findAll">点击测试</a>
</body>
</html>

e.success.jsp成功界面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h2>跳转成功....</h2>
    <c:forEach items="${data}" var="account">
        <tr>
            <td>${account.id}</td>
            <td>${account.uid}</td>
            <td>${account.money}</td>
        </tr>
    </c:forEach>

</body>
</html>
 

f.运行成功(不晒截图)

第三步:整合spring和springmvc(使用web.xml中的监听实现【将实现applicationContext.xml一开始就被扫描到】)

a.在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>
  <display-name>Archetype Created Web Application</display-name>
<!--
    spring整合springmvc思路:
    在整合之前需要明白,我们需要在controller中调用service,
    最快捷的便是使用依赖注入,而至今使用Tomcat服务器只加载了springmvc.xml文件,
    并没有applicationContext.xml的加载(也就是spring并没有被加载),
    所以可以通过监听ServeltContext域对象,在创建时加载spring的配置文件(applicationContext.xml)
-->
  <!--加载Spring-->
  <!--配置监听器,默认只加载web-inf目录下的applicationContext.xml-->
  <!--但是我们的文件在resources文件夹下-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--设置配置文件路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>







  <!--前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc.xml配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动服务器时,创建该servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--配置中文乱码过滤器-->
  <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>
</web-app>

b.在控制层调用业务层

package com.itheima.controller;

import com.itheima.dao.IAccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    //原先之下只加了这一段代码;注入调用findAll        accountService.findAll();
    @Autowired
    private IAccountService accountService;


    @RequestMapping("/findAll")
    public String findAll(){
        System.out.println("springmvc控制层的输出.....");
        accountService.findAll();
        return "success";
    }
}

c.运行成功(不晒截图)

第四步:独立写mybatis的实现模块

a.编写dao接口:IAccountDao

package com.itheima.dao;

import com.itheima.pojo.Account;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
/*持久层接口*/
@Repository
public interface IAccountDao {
    //查询所有
    @Select("select * from account")
    public List<Account> findAll();
}

b.编写SqlMapConfig配置文件(后面加入到spring的applicationContext中去统一管理,也就是删掉)

<?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">

<!-- mybatis 的 主配置文件-->
<configuration>
    <!--环境配置-->
    <environments default="mysql">
        <!--配置mysql-->
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"/>
            <!--配置数据源/连接池-->
            <dataSource type="POOLED">
                <!--配置连接数据库的基本信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
    <mappers>
        <!--使用xml进行查询-->
        <!--<mapper resource="mapper/UserDao.xml"/>-->
        <!--使用注解-->
        <!--<mapper class="club.twzw.dao.UserDao"/>-->
        <!--扫描mapper下所有注解-->
        <package name="com.itheima.dao"/>
    </mappers>

</configuration>

c.编写mybatis的测试类

package com.itheima.test;

import com.itheima.dao.IAccountDao;
import com.itheima.pojo.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {

    @Test
    public void test() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(resourceAsStream);
        SqlSession session = factory.openSession(true);
        IAccountDao accountDao = session.getMapper(IAccountDao.class);
        List<Account> all = accountDao.findAll();
        for (Account account : all) {
            System.out.println("查询所有---" + account);
        }
    }
}

d.运行成功(不截图)

第五步:spring和mybatis的整合(将SqlMapConfig.xml放入applicationContext中管理【动态dao管理】)

a.将SqlMapConfig.xml放入applicationContext中管理(SqlMapConfig.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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <!--只处理Service和dao -->
    <context:component-scan base-package="com">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>







    <!--
    Spring整合mybatis思路就是将动态代理的dao对象交给springioc容器管理
    并且实现service层调用dao层,使用依赖注入
    -->


    <!--将Mybatis的SqlMapConfig东西放到spring的配置文件当中-->
    <!--Spring 配置mybatis-->
    <!--配置连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_mybatis"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置dao所在的包-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>

</beans>

b.spring业务层调用(mybatis)dao的持久层

package com.itheima.service.impl;

import com.itheima.controller.AccountController;
import com.itheima.dao.IAccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private  IAccountDao accountDao;
    @Override
    public List<Account> findAll() {
        System.out.println("service被调用----spring查询成功......");
        return accountDao.findAll();
    }
}

c.最后如果点击超链接有输出在界面上有数据则成功

1.导入依赖pom.xml

<dependencies>
      <!--junit4-->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
      </dependency>
      <!--日志-->
      <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
      </dependency>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
      <!-- mysql数据库驱动依赖 -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.41</version>
      </dependency>
      <!--c3p0  数据连接池 jar-->
      <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.1</version>
      </dependency>
      <!--C3P0额外依赖的一个jar包-->
      <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>mchange-commons-java</artifactId>
        <version>0.2.10</version>
      </dependency>
      <!-- Mybatis框架:-->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.2</version>
      </dependency>
      <!-- MyBatis整合Spring的适配包 -->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.1</version>
      </dependency>
      <!--EL表达式-->
      <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
        <type>jar</type>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <type>jar</type>
      </dependency>

总结:道路很漫长,加油吧少年!!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值