SSM(二)——整合

框架混合使用,先了解一下各层的概念。刚学的时候真的是被吓得不轻,博客把几个层说得文绉绉,硬是没听懂,问了师兄几个例子,终于略懂。

  • 控制层:controller层,通过接收前端的参数来调用服务层,将前端的参数传给服务层,获取一个结果再返回给前端,初学的时候例如main函数,就算是控制层在调用方法。
  • 服务层:service层,存放业务逻辑相关处理,例如【买书】的逻辑就是【人扣钱+商店减书】,它通过调用持久层实现对数据库的操作。
  • 持久层:dao层,有的也称为mapper层,搞数据库的,名字的由来应该是事务的持久性。写的都是对数据库操作的方法,提供功能给服务层。

前端<==>控制层<==>服务层<==>持久层<==>数据库

例如在持久层有两个方法,【人扣钱】和【商店减书】,直接在main函数直接调用就好,可以不通过服务层的【买书】=【人扣钱】+【商店减书】方法,但是不利于项目后期的延展和维护。


SpringMVC是Spring的一个模块,所以不存在整合的说法。有的只是Spring和MyBatis的整合,记录一下基于Mapper接口方式的整合过程

1.导包

多加一个mybatis-spring包,spring、mybatis、springmvc的其他包看需求导。

2.创建数据表,实体类和Mapper接口即XML映射文件

数据表:tb_cutomer tb_order

实体类:Customer Order

Mapper接口:CustomerMapper OrderMapper 

XML映射文件:CustomerMapper OrderMapper

3.配置spring-config.xml

命名空间、引入资源文件、配置数据源、配置JDBC、配置事务管理器、开启注解驱动等常规操作

4.spring-config.xml里配置MyBatis工厂

    <!-- 5.配置MyBatis工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>

5.配置mybatis-config.xml

此时不再需要配置<environments>,这个在spring-config.xml写好了,只需要配置<mappers>。

6.使Mapper接口能生成Mapper对象

(1)通过MapperFactoryBean

    <!-- 6.基于MapperFactoryBean的Mapper代理开发,挨个手写,代码臃肿,与下面的等价 -->
    <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mapper.CustomerMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <bean id="orderMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mapper.OrderMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

这样就可以通过ac.getBean("customerMapper",CustomerMapper.class)获取Mapper对象。如上所言,通过MapperFactoryBean需要挨个配置会使得代码臃肿。

(2)通过MapperScannerConfigurer

    <!--  6.基于MapperScannerConfigurer的Mapper代理开发 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mapper"/>
    </bean>

7.在main函数中运行

    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring-config.xml");
        CustomerMapper customerMapper=ac.getBean("customerMapper",CustomerMapper.class);
        HashMap<String, Object> map=new HashMap<String, Object>();
        map.put("id",1);
        System.out.println(customerMapper.selectCustomerById(map));
        OrderMapper orderMapper=ac.getBean("orderMapper",OrderMapper.class);
        System.out.println(orderMapper.selectOrderById(map));
        
    }

8.目录结构

9.spring-config.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:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    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-4.3.xsd
     
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util.xsd
     
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.3.xsd
     
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
     
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
     ">


    <!-- 引入资源文件db.properties -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="Location" value="db.properties"></property>
    </bean>


    <!-- 扫描包 -->
    <context:component-scan
        base-package="com" />

    <!-- 开启通知注解功能 -->
    <aop:aspectj-autoproxy />


    <!-- 1.创建并通过外部资源文件配置数据源 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 2.通过数据源配置JdbcTemplate -->
    <bean id="jdbcTemplate"
        class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 3.配置事务管理器 -->
    <bean id="dataSourceTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 4.开启事务注解驱动 -->
    <tx:annotation-driven proxy-target-class="true"
        transaction-manager="dataSourceTransactionManager" />
    
    <!-- 5.配置MyBatis工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>
    
    <!-- 6.基于MapperFactoryBean的Mapper代理开发,挨个手写,代码臃肿,与下面的等价 -->
    <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mapper.CustomerMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <bean id="orderMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mapper.OrderMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
    
    <!--  6.基于MapperScannerConfigurer的Mapper代理开发 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mapper"/>
    </bean>-->
    


</beans>

10.mybatis-config.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><!-- 开启执行时SQL语句的打印过程 -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    
    <typeAliases><!-- 默认类型别名 -->
        <package name="com.bean"/>
    </typeAliases>

    <mappers>
        <package name="com.mapper"/> 
    </mappers>
</configuration>

 其实,在mybatis-config.xml这里不需要写<mappers>的配置了

原文地址:https://www.cnblogs.com/shoulinniao/p/13042630.html