案例练习-添加和查询-优化(spring整合mybatis和声明式事务控制)

1.spring整合mybatis

结合上一篇博客:案例练习-添加和查询

 

 

问题:

 原因是找不到AccountMapper这个接口中的save方法

 在spring核心配置文件中,明明写了啊

 解决办法:

上面的问题就是idea创建包的时候的问题

这里我创建的包名是com.company.mapper,它在这里不是一级一级的创建文件夹而是创建了一个com.company.mapper名字的一个包,所以需要com/company/mapper 这样创建

 

spring整合mybatis主要是在spring的核心配置文件(applicationContext.xml)中进行配置

为什么能够整合,因为我在pom.xml中加入了

 

 applicationContext.xml

 而sqlMapConfig-spring.xml中

2.声明式事务控制

出现这样的问题:org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'tx:advice'.

spring配置xml文件中如果xml书写格式有误,或者xml头部的信息缺失了,就会报这样的错。org.xml.sax.SAXParseException: cvc-complex-type.2.4.c

这下面错误的原因是因为applicationContext.xml中的头部少了东西;配置事务管理要在头部 xsi:schemaLocation=  中加上这段代码:

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

 

这样才能使用事务的传播特性 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 

声明式事务控制:在applicationContext.xml中添加下面的

 

基于上一篇博客,主要修改下面的几个:

完整版的aplicationContext.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
">
    <!--组件扫描 扫描service和mapper-->
    <context:component-scan base-package="com.company">
    <!--排除controller的扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 加载properties文件-->
    <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-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入datasource-->
        <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.company.mapper"/>
    </bean>

    <!-- 声明式事务控制-->
    <!-- 平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--通知,事务的增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置AOP事务织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.company.service.impl.*.*(..))"/>
    </aop:config>

</beans>

之前的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.company.domain.Account" alias="account"/>
    </typeAliases>

</configuration>

AccountServiceImpl.java

package com.company.service.impl;

import com.company.domain.Account;
import com.company.mapper.AccountMapper;
import com.company.service.AccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@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();
    }
}

  

posted @ 2021-01-21 14:20  GumpYan  阅读(102)  评论(0编辑  收藏  举报