SSM 整合

1. 导入jar包

  • SpringMVC 所需jar包;
  • MyBatis 所需jar包 mybatis-3.4.1 和数据库驱动jar包;
  • MyBatis 与 Spring 整合需要适配包: mybatis-spring-1.3.1

2. 配置文件

2.1 MyBatis 配置文件

  • MyBatis 全局配置文件: mybatis-config.xml;
  • 数据库配置文件: dbconfig.properties;
  • 对应接口的映射文件: xxxMapper.xml;

2.2 Spring 配置文件

  • 在 web.xml 中配置Spring核心监听器;
  • 在类路径下,创建 applicationContext.xml;

2.3 SpringMVC 配置文件

  • 在 web.xml 中配置 DispatcherServlet;
  • 在 WEB-INF 目录下创建 springDispatcherServlet-servlet.xml;
// applicationContext.xml
<!-- Spring 管理所有的业务逻辑 -->
<context:component-scan base-package="cn.itcast.mybatis">
    <context:exclude-filter type="annotation"
                                    expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 引入数据库的配置文件 -->
<context:property-placeholder location="classpath:dbconfig.properties"/>

<!-- Spring 用来控制业务逻辑,包括数据源,事务控制,AOP -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="user" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

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

<!-- 开启基于注解的事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

<!-- Spring 整合 MyBatis:
            目的:1. spring管理所有组件: mapper 的实现类;
                 2.spring 用来管理事务: spring声明式事务;
-->

<!-- 创建出 SqlSessionFactory 对象 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 设置数据源 -->
    <property name="dataSource" ref="dataSource"></property>

    <!-- 指定mybatis全局配置文件的位置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>

    <!-- 指定mapper文件的位置 -->
    <property name="mapperLocation" value="classpath:mybatis/mapper/*.xml"/>

    <!-- 指定别名包 -->
    <property name="typeAliasesPackage" value="包路径"/>
</bean>

<!-- 配置一个可以进行批量执行的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    <constructor-arg name="executorType" value="BATCH"/>
</bean>

<!-- 扫描所有的 mapper 接口实现,让这些mapper能够自动注入 @Autowired -->
<mybatis-spring:scan base-package="cn.itcast.mybatis.dao"/>

<!-- 配置MapperScannerConfigurer, Mybatis-Spring 会自动为我们注册Mapper 对应的 MapperFactoryBean 对象 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.itcast.mybatis.mapper"/>
</bean>
-->

// EmployeeService.java 
public interface EmployeeMapper{
    
    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Autowired
    private SqlSession sqlSession;
    
    public List<Employee> getEmps(){
        // 创建可以执行批量操作的代理对象
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
    }

参考资料

原文地址:https://www.cnblogs.com/linkworld/p/7797587.html