SSM統合記事] III。第II章+統合+トランザクションユニット・テスト・ケースSSM(4章)

春+ SpringMvc + MyBatisのは、第二章総務++ユニットテストケースを統合します

githubの源码(day56-SSM-トランザクション)https://github.com/1196557363/ideaMavenProject

いくつかの知識は、参照を理解することはできません] SSMは、記事を統合する。春+ SpringMvc + MyBatisの単純なケースを

章に接続された場合[SSM]統合三条。SSM統合業務+ +ユニットテストケース最初の章(4章)

5.設定情勢

5.1定義I ...サービスおよびその実装クラスServiceImpl(クラスを実装プラス私は接頭辞を持っていません)

5.1.1 IDeptServiceとDeptServiceImpl

インタフェース定義IDeptService

package com.wpj.service;

import org.springframework.stereotype.*;

/**
 * ClassName: IDeptService
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 19:46
 * @since JDK 1.8
 */
public interface IDeptService {
}

DeptServiceImpl実装クラスの定義注射ダオ(実装クラスプラス@Serviceを覚えています)

package com.wpj.service.impl;

import com.wpj.dao.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

/**
 * ClassName: DeptServiceImpl
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 20:00
 * @since JDK 1.8
 */
@Service
public class DeptServiceImpl implements IDeptService {
    @Autowired
    private IDeptDao iDeptDao;
}
5.1.1 IEmpServiceとEmpServiceImpl

インタフェース定義IEmpService

package com.wpj.service;

/**
 * ClassName: IEmpService
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 19:47
 * @since JDK 1.8
 */
public interface IEmpService {

}

DeptServiceImpl実装クラスの定義注射ダオ(実装クラスプラス@Serviceを覚えています)

package com.wpj.service.impl;

import com.wpj.dao.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

/**
 * ClassName: EmpServiceImpl
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 20:03
 * @since JDK 1.8
 */
@Service
public class EmpServiceImpl implements IEmpService {
    @Autowired
    private IEmpDao iEmpDao;
}

5.2注釈と構成スキャン総務

上記のローディングjdbc.propertiesにスキャンコメント

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/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">

    <!-- 4.扫描注解 use-default-filters="false" 关闭默认机制 默认true -->
    <context:component-scan base-package="com.wpj" use-default-filters="false">
        <!-- 4.1 只扫描Serivce -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    
    <!-- 1. 加载jdbc.properties -->
    <!-- 2. 创建数据源 -->
    <!-- 3. Spring跟MyBatis整合 -->

    <!-- 5. 事务的配置 -->
    <!-- 5.1 事务管理器 负责事务的提交(需要连接数据库)-->
    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 5.1.1 设置数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 5.2 事务的策略 id:唯一标识 transaction-manager:事务管理器 -->
    <tx:advice id="txAdvice" transaction-manager="tx">
        <tx:attributes>
            <!--
                name:方法名称,支持通配符
                isolation:事务隔离级别
                propagation:传播级别
                read-only:是否只读
            -->
            <!-- 写操作 -->
            <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
            <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
            <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
            <!-- 读操作 -->
            <tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            <tx:method name="query*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            <tx:method name="select*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 5.3 事务AOP的配置 -->
    <aop:config>
        <!--  advice-ref: 指向事务的策略 -->
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wpj.service.*.*(..))" />
    </aop:config>
</beans>

5.3テスト(のみ成功サービスの注入かどうかを試験しました)

import com.wpj.service.*;
import org.apache.ibatis.session.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;

import javax.sql.*;

/**
 * ClassName: SSMTest
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\8 0008 17:51
 * @since JDK 1.8
 */

@RunWith(value= SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-context.xml")
public class SSMTest {

    @Autowired
    private IEmpService iEmpService;

    @Test
    public void testIEmpService(){
        System.out.println(iEmpService);
    }
}

5.4結果

エラーまたはnullがどこへ行くの前にチェックする必要がある場合は、間違った下記下ります。
可能性のある間違った場所

  1. IMPL @Serviceを追加しません
  2. DAOの注入は@Autowiredを追加しませんでした
  3. 間違ったスキャンプロファイルをパック
  4. 十分でない場合は、トランザクションは、一時的に除去することができます
    ここに画像を挿入説明

6.試験SpringとMyBatisの統合環境(EMPに例えば)

プロキシDAO層を作成するために、6.1

<!-- 6. 创建dao层的代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 6.1 Mapper接口的包名 -->
	<property name="basePackage" value="com.wpj.dao" />
	<!-- 6.2 sqlSessionFactoryBeanName-->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

6.2 DAOは、メソッドを追加します。

public interface IEmpDao {
    /**
     * 获取所有的Emp
     * @return
     */
    List<Emp> getAllEmp();
}

SQL 6.3マッパーマッピング定義

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.wpj.dao.IEmpDao">
		<!-- 配属性和字段的映射,将来写级联查询会用到-->
        <resultMap id="empMap" type="emp">
                <id property="empId" column="emp_id"/>
                <result property="empName" column="emp_name" />
                <result property="job" column="job" />
                <result property="superId" column="superId" />
                <result property="deptNo" column="deptNo" />
        </resultMap>

        <select id="getAllEmp" resultMap="empMap">
                select * from emp
        </select>
</mapper>

6.4 serivce対応する方法は、DAOで定義されています

public interface IEmpService {
    /**
     * 获取所有的Emp
     * @return
     */
    List<Emp> getAllEmp();
}

DAO注入メソッドを呼び出すことにより、6.5 serviceImpl

@Service
public class EmpServiceImpl implements IEmpService {
    @Autowired
    private IEmpDao iEmpDao;

    public List<Emp> getAllEmp() {
        return iEmpDao.getAllEmp();
    }
}

6.6テスト

import com.wpj.bean.*;
import com.wpj.service.*;
import org.apache.ibatis.session.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;

import javax.sql.*;
import java.util.*;

@RunWith(value= SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-context.xml")
public class SSMTest {
    @Autowired
    private IEmpService iEmpService;
    @Test
    public void testGetAllEmp() {
        List<Emp> empList = iEmpService.getAllEmp();
        for (Emp emp : empList) {
            System.out.println(emp);
        }
    }
}

6.7結果

ここでも、エラーまたはnullが間違って行く以下下るする場所の前に確認する必要があります。
可能性のある間違った場所

  1. DAO層マッパーが構成さsqlSessionFactoryBeanNameを配置します
  2. DAOのメソッド名とプロファイルのid SQLマッピングは対応していません
  3. このresultMapプロパティとフィールドのマッピングが間違っ
    ここに画像を挿入説明
継続するには。
  1. トランザクションが正常に構成されているかどうかを試験[SSM]統合三条。SSM統合+ +ユニットテストケース総務第三章(4章)
公開された42元の記事 ウォンの賞賛8 ビュー2439

おすすめ

転載: blog.csdn.net/TheNew_One/article/details/103896990