Spring+MyBatis+JUnit整合测试的搭建过程总结

  • 1创建项目(Maven项目支持聚合,聚合的项目模块之间具有继承的特性即子模块会继承父模块的配置)
    • 包含父子模块的Maven项目创建方法
      • 首先创建父项目
      • 选中父项的pom.xml文件右键-->Maven--》new Maven module project随后和父项目差不多操作
    • 在pom.xml可以添加许多有关开发者所在组织的信息
    • 当需要统一版本号时,为了升级和维护,建议将版本号version定义成变量的形式
<version>${ version }</version>
<properties>
        <version>0.1</version>
        <spring.version>4.3.9.RELEASE</spring.version>
</properties>

    • 将项目的配置信息写入pom.xml文件中包括
      • - JUnit(test)
      • - spring-test(test)
      • - 数据库驱动
      • - 连接池
      • - MyBatis
      • - MyBatis和Spring的整合
      • - Spring的事务
      • - Spring ORM的支持
      • - Spring 面向切面的支持
      • - Spring上下文支持
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zhiyou100.video</groupId>
    <artifactId>video-parent</artifactId>

    <!-- 版本号会在多个地方使用 为了统一版本,使用变量易于修改和维护 -->
    <version>${ version }</version>

    <!-- 表示该父类模块包含的是所有子模块的共有配置信息-->
    <packaging>pom</packaging>

    <name>Video Parent</name>
    <url>http://www.zhiyou100.com</url>
    <description>智游在线,智游在线教育平台,提供视频课程,互动学习等服务</description>
    <inceptionYear>2017</inceptionYear>
    <organization>
       <name>智游教育</name>
       <url>http://www.zhiyou100.com</url>
    </organization>

  <!-- 用于配置当前xml文件中要使用的变量 -->
    <properties>
       <version>0.1</version>
        <spring.version>4.3.9.RELEASE</spring.version>
    </properties>

    <!--这两个jar包所有子项目共有的因此放置于父项目中   -->
    <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <!-- 指明这个依赖项的作用范围 -->
           <scope>test</scope>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
           <!-- 保证版本的统一性,使用变量,也便于修改 -->
           <version>${spring.version}</version>
           <scope>test</scope>
       </dependency>
    </dependencies>

    <!-- modules用于将当前项目和另外一个或多个项目形成一个聚合项目 -->
    <modules>
       <module>video-service</module>
    </modules>

</project>

  • 在项目中定义数据模型(src/main/java/下)
  • 在项目中定义数据 库访问接口xxxDao(src/main/java/下)并使用@Mapper注解标记该接口
    //  在Maven项目中当dao层方法需要多个参数时使用Java容器解决这一问题
  • 在数据 操作对象Dao定义Mapper(映射器)的配置文件(如 XXDao.xml )(该配置文件中编写相应的sql语句,建立模型和数据库之间的映射关系)
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--mapper是映射器 -->
<mapper namespace="com.zhiyou100.video.dao.UserDao">

    <!-- 数据库和model的映射关系 -->
    <resultMap type="User" id="userResultMap">
       <id property="userId" column="user_id" />
       <result property="isSystem" column="is_system" />
       <result property="isAdmin" column="is_admin" />
    </resultMap>

    <!-- parameterType指明了参数的类型 这里类型是HashMap #{account}是利用key value 的方式从hashmap中拿到参数值-->
    <select id="getByLogin"  resultMap="userResultMap"  parameterType="HashMap">
        SELECT
        *
        FROM
        user
        WHERE
        status = 2
        AND username = #{account}
        AND password = #{password}
    </select>

</mapper>
  • 在项目中定义 服务层的XXservice类并使用@Service注解标记该类 该类中的XXdao使用@Autowired注解标记(spring框架去实现这个类,spring中不允许自己创建Dao和service,因为Dao和service由spring统一管理)
  • 创建测试类
  • 在测试类中使用注解创建上下文
    • 指明spring测试运行测试用例
    • 在测试类的资源文件中创建应用的上下文配置文件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:context="http://www.springframework.org/schema/context"

    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-4.3.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!--扫描bean 发现标记@Service@Autoeried等标记的类或属性-->
    <!-- 上下文注册类获取bean -->
    <context:component-scan base-package="com.zhiyou100.video" />

    <!--数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/zyvideo?characterEncoding=utf8&amp;useSSL=false" />
        <property name="username" value="root" />
        <property name="password" value="123123" />
    </bean>

    <!--MyBatis会话工厂,主要是映射-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis.xml" />
        <property name="typeAliasesPackage" value="com.zhiyou100.video.model" />
        <property name="mapperLocations" value="classpath*:com/zhiyou100/video/dao/*Dao.xml" />
    </bean>

    <!--MyBatis Mapper扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.zhiyou100.video.dao" />
    </bean>

    <!--数据库事务-->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--事务-->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

</beans>

    • 在测试类的资源文件中 创建mybatis的配置文件













  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值