zoukankan      html  css  js  c++  java
  • 18SSM资源整合2

    Spring整合MyBatis

    即,Spring框架中加入MyBatis的功能,让Spring的配置文件applicationContext.xml完成一部分sqlMapConfig.xml的配置

    1. 将sqlMapConfig.xml命名为sqlMapConfig-spring.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>
          <!--    定义别名-->
          <typeAliases>
              <!--        <typeAlias type="com.domain.Account" alias="account"/>-->
              <package name="com.domain"/>
          </typeAliases>
      </configuration>
      
    2. 在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:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/aop/spring-aop.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd">
      
          <!--    组件扫描 service和mapper-->
          <context:component-scan base-package="com">
              <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          </context:component-scan>
      
      <!--    加载properteis文件-->
          <context:property-placeholder location="classpath:jdbc.properties"/>
      
      <!--    配置数据源-->
          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <property name="driverClass" value="${jdbc.driver}"/>
              <property name="jdbcUrl" value="${jdbc.url}"/>
              <property name="user" value="${jdbc.username}"/>
              <property name="password" value="${jdbc.password}"/>
          </bean>
      
      <!--    配置sessionFactory:sessionFactory是为了让spring自动创建下面的mapper对象-->
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
      <!--        加载mybatis核心文件-->
              <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"/>
          </bean>
      
      <!--    扫描mapper所在的包: 创建mapper对象, 通过注解注入,快速使用-->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="basePackage" value="com.mapper"/>
          </bean>
      
      <!--    声明式事务控制-->
      <!--    平台事务管理器:让每一个service成为一个事务-->
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource"/>
          </bean>
      <!--    配置事务增强-->
          <tx:advice id="txAdvice">
              <tx:attributes>
                  <tx:method name="*"/>
              </tx:attributes>
          </tx:advice>
      <!--    事务的织入-->
          <aop:config>
              <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.impl.*.*(..))"/>
          </aop:config>
      </beans>
      
    3. AccountServiceImpl.java中通过注解注入mapper,直接使用

      @Service("accountService")
      public class AccountServiceImpl implements AccountService {
      
          @Autowired
          private AccountMapper accountMapper;
      
          @Override
          public void save(Account account) {
              accountMapper.save(account);
          }
      
          @Override
          public List<Account> findAll() {
              return accountMapper.findAll();
          }
      }
      
    4. 测试

  • 相关阅读:
    【转】Windows Server 2012无法安装 .NET3.5-安装角色或功能失败,找不到源文件-十有三博客
    jfinal undertow项目集成JDK做成系统服务
    【转】解决undertow多个https证书的web项目部署问题
    说说 C# 9 新特性的实际运用
    php RSA加解密
    mscomm控件使用详解 转
    VB中让listview自动调整列宽
    QueryPerformanceFrequency使用方法--Windows高精度定时计数
    VB6鼠标滚轮插件
    Microsoft Visual C++ 6.0快捷键(绝对值得掌握)
  • 原文地址:https://www.cnblogs.com/mingriyingying/p/13701642.html
Copyright © 2011-2022 走看看