简单的mybatis+spring+junit整合

简介: 使用的数据库是:oralce建立一张TEST表,再建一个ID字段用于简单的测试需要的包:spring的jar包,mybatis.

使用的数据库是:oralce

建立一张TEST表,再建一个ID字段用于简单的测试

需要的包:spring的jar包,mybatis.jar,mybatis-spring.jar,commons-logging.jar,aopalliance.jar,aspectjweaver-1.6.9.jar,ojdbc6.jar,junit.jar,junit-dep.jar


spring配置文件:applicationContext.xml

<span style="font-size:18px;"><?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:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
    <!-- org.springframework.jndi.JndiObjectFactoryBean -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
        <property name="username" value="test"/>
        <property name="password" value="test"/>
    </bean>
    <!-- 配置</span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">jdbcTransactionManager</span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;"> --></span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">
</span><span style="font-size:18px;">    <bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置spring组件扫描 -->    
    <context:component-scan base-package="*" />

    <!-- 启用autowire -->
    <context:annotation-config />
	
    <!-- 设置exposeProxy为true -->
    <aop:aspectj-autoproxy expose-proxy="true"/>

    <!-- </span><span style="font-family:microsoft yahei;color:#555555;"><span style="font-size: 15px; line-height: 35px;">支持注解</span></span><span style="font-size:18px;"> -->
    <tx:annotation-driven />
	
    <!-- mybatis的SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="mybatis.model" />
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
	<constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!-- 扫描映射 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="mybatis.mapper" />
    </bean> 
</beans>
</span>
在mybatis.mapper下建立mapper

TestMapper.java

package mybatis.mapper;

import java.util.List;

public interface TestMapper {
	public List<String> selectAllInfo();
}
TestMapper.xml

<?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="mybatis.mapper.TestMapper">

  <select id="selectAllInfo" resultType="java.lang.String">
    SELECT
          ID
    FROM TEST
  </select>

</mapper>
建立模板类:Test.java

模板类需要映射数据库对应表TEST的所有字段

package mybatis.model;
/**
 * 实体类
 *
 */
public class Test {
	private Integer id;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
}
这时我们就构建了简单的mybatis+spring

然后建立junit测试类:

首先建立一个junit测试类的父类,这个父类中写了需要加载的spring配置文件,事务的控制,便于新的junit类来继承这个父类

package mybatis.junit;

import org.junit.runner.RunWith;  
import org.springframework.test.context.ContextConfiguration;  
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
import org.springframework.test.context.transaction.TransactionConfiguration;
/** 
 * Junit 基础类,加载环境  
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})  
@TransactionConfiguration(transactionManager="jdbcTransactionManager",defaultRollback=true)
public  class TestBase extends AbstractTransactionalJUnit4SpringContextTests {  

}  
建立测试类:@Autowired是spring注解的一种方式,@Test是junit测试找的执行方法

package mybatis.junit.test;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import mybatis.junit.TestBase;
import mybatis.mapper.TestMapper;

public class TestMybatis extends TestBase {
	@Autowired
	private TestMapper testMapper;
	
	@Test
	public void getCallCenterAccountByCrmAccount(){
		try {
			List<String> list = testMapper.selectAllInfo();
			System.out.println(list.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
这样我们就将mybatis,spring,junit整合完毕了,这套整合主要用于平时的测试。

如果需要在web项目中启动还需要配置一个aciton层

web.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     
     <!-- 初始化spring配置文件-->
     <context-param>
	<param-name>contextConfigLocation</param-name>
<span style="white-space:pre">	</span><param-value>
<span style="white-space:pre">		</span>/WEB-INF/applicationContext*.xml
	</param-value>
     </context-param>
     <context-param>
	<param-name>webAppRootKey</param-name>
	<param-value>webApp.root</param-value>
     </context-param>
	
     <!-- 设置servlet编码开始 -->  
    <filter>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>utf-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    <!-- 设置servlet编码结束 -->
    
    <!-- 设置spring监听 -->
    <listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
    </listener>
    <!-- 设置spring监听结束 -->
	 
    <welcome-file-list>
    	<welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

再新增一个spring配置文件:applicationContextTaskConfig.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:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
     default-autowire="byName" default-lazy-init="true">
   
    <!-- ActionBean-->
    <bean id="testAction" class="mybatis.action.TestAction" scope="prototype"/>
</beans>
		
action类: TestAction.java

package mybatis.action;

import java.util.List;

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

import mybatis.mapper.TestMapper;

public class TestAction {
	@Autowired
	public TestMapper testMapper;
	
	public String selectAllInfo(){
		List<String> test = testMapper.selectAllInfo();
		System.out.println(test.size());
		return "success";
	}
}










目录
相关文章
|
1月前
|
XML Java 测试技术
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
这篇文章介绍了Spring5框架的三个新特性:支持@Nullable注解以明确方法返回、参数和属性值可以为空;引入函数式风格的GenericApplicationContext进行对象注册和管理;以及如何整合JUnit5进行单元测试,同时讨论了JUnit4与JUnit5的整合方法,并提出了关于配置文件加载的疑问。
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
|
3天前
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
|
4天前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
27天前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
8天前
|
前端开发 JavaScript Java
技术分享:使用Spring Boot3.3与MyBatis-Plus联合实现多层次树结构的异步加载策略
在现代Web开发中,处理多层次树形结构数据是一项常见且重要的任务。这些结构广泛应用于分类管理、组织结构、权限管理等场景。为了提升用户体验和系统性能,采用异步加载策略来动态加载树形结构的各个层级变得尤为重要。本文将详细介绍如何使用Spring Boot3.3与MyBatis-Plus联合实现这一功能。
33 2
|
18天前
|
Java 数据库连接 测试技术
SpringBoot 3.3.2 + ShardingSphere 5.5 + Mybatis-plus:轻松搞定数据加解密,支持字段级!
【8月更文挑战第30天】在数据驱动的时代,数据的安全性显得尤为重要。特别是在涉及用户隐私或敏感信息的应用中,如何确保数据在存储和传输过程中的安全性成为了开发者必须面对的问题。今天,我们将围绕SpringBoot 3.3.2、ShardingSphere 5.5以及Mybatis-plus的组合,探讨如何轻松实现数据的字段级加解密,为数据安全保驾护航。
64 1
|
27天前
|
Web App开发 前端开发 关系型数据库
基于SpringBoot+Vue+Redis+Mybatis的商城购物系统 【系统实现+系统源码+答辩PPT】
这篇文章介绍了一个基于SpringBoot+Vue+Redis+Mybatis技术栈开发的商城购物系统,包括系统功能、页面展示、前后端项目结构和核心代码,以及如何获取系统源码和答辩PPT的方法。
|
27天前
|
SQL Java 关系型数据库
SpringBoot 系列之 MyBatis输出SQL日志
这篇文章介绍了如何在SpringBoot项目中通过MyBatis配置输出SQL日志,具体方法是在`application.yml`或`application.properties`中设置MyBatis的日志实现为`org.apache.ibatis.logging.stdout.StdOutImpl`来直接在控制台打印SQL日志。
SpringBoot 系列之 MyBatis输出SQL日志
|
1月前
|
前端开发 Java 数据库连接
一天十道Java面试题----第五天(spring的事务传播机制------>mybatis的优缺点)
这篇文章总结了Java面试中的十个问题,包括Spring事务传播机制、Spring事务失效条件、Bean自动装配方式、Spring、Spring MVC和Spring Boot的区别、Spring MVC的工作流程和主要组件、Spring Boot的自动配置原理和Starter概念、嵌入式服务器的使用原因,以及MyBatis的优缺点。
|
1月前
|
Java 数据库连接 mybatis
基于SpringBoot+MyBatis的餐饮点餐系统
本文介绍了一个基于SpringBoot和MyBatis开发的餐饮点餐系统,包括系统的主控制器`IndexController`的代码实现,该控制器负责处理首页、点餐、登录、注册、订单管理等功能,适用于毕业设计项目。
42 0
基于SpringBoot+MyBatis的餐饮点餐系统