SSM项目之搭建Spring单元测试环境实现增删改查

在上一篇博文中,我们使用mybatis generators实现了逆向工程生成了相应的实体类,mapper接口和mapper的xml文件,那么,在本篇博文中我们要实现的就是测试Spring环境并实现数据增删改查。
在这里插入图片描述
首先,我们将applicationContext.xml文件配置好,即我们的spring

<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--除了Controller包不扫描以外,其他的都扫描-->
	<context:component-scan base-package="com.atguigu">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
	<!--=================== 数据源,事务控制,xxx ================-->
	<context:property-placeholder location="classpath:dbconfig.properties" />
	<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!--================== 配置和MyBatis的整合=============== -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定mybatis全局配置文件的位置 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<property name="dataSource" ref="pooledDataSource"></property>
		<!-- 指定mybatis,mapper文件的位置 -->
		<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	</bean>

	<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!--扫描所有dao接口的实现,加入到ioc容器中 -->
		<property name="basePackage" value="com.atguigu.crud.dao"></property>
	</bean>
	
	<!-- 配置一个可以执行批量的sqlSession -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
		<constructor-arg name="executorType" value="BATCH"></constructor-arg>
	</bean>
	<!--=============================================  -->

	<!-- ===============事务控制的配置 ================-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!--控制住数据源  -->
		<property name="dataSource" ref="pooledDataSource"></property>
	</bean>
	<!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)  -->
	<aop:config>
		<!-- 切入点表达式 -->
		<aop:pointcut expression="execution(* com.atguigu.crud.service..*(..))" id="txPoint"/>
		<!-- 配置事务增强 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
	</aop:config>
	
	<!--配置事务增强,事务如何切入  -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 所有方法都是事务方法 -->
			<tx:method name="*"/>
			<!--以get开始的所有方法  -->
			<tx:method name="get*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- Spring配置文件的核心点(数据源、与mybatis的整合,事务控制) -->
	
</beans>


然后我们需要进行mybatis.xml文件的配置
这个地方曾经困扰了博主好久,因为博主将mapper的包又重新扫描了一遍,造成了错误

<?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>
	<settings>//命名规则
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
	<typeAliases> <!--取别名 -->
		<!--推荐使用此方式取别名,指向bean包,然后就可以使用类型首字母小写取别名 -->
		<package name="com.ssm.beans" />
	</typeAliases>
</configuration>

在这里插入图片描述
然后我们需要进行测试环境搭建
在这里插入图片描述
具体代码:

package com.ssm.test;

import java.util.UUID;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ssm.beans.Department;
import com.ssm.beans.USer;
import com.ssm.dao.DepartmentMapper;
import com.ssm.dao.USerMapper;
//指定使用spring的测试模块
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
    
    "classpath:applicationContext.xml"})
public class MapperTest {
    
    
	/*
	 * 测试dao层
	 * 推荐spring就使用spring的单元测试可以自动注入我们需要的组件
	 * 1.导入Spring测试的功能模块
	 * 2.@ContextConfiguration配置文件的位置
	 * 3.直接autowrited要使用的组件
	 */
	@Autowired
	DepartmentMapper deptmapper;
	@Autowired
	USerMapper usermapper;
	@Autowired
	SqlSession sqlsession;
	@Test
	public void testCURD(){
    
    
		//测试部门查询
		//1.创建spring  ioc容器
		ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
		//2.从容器中获取mapper
		ioc.getBean(DepartmentMapper.class);
		System.out.println(ioc);
		System.out.println(deptmapper);
		//插入几个部门
		new Department(null,"开发部");
		//deptmapper.insertSelective(new Department(null,"开发部"));
		
		//生成员工数据,测试员工插入
		//usermapper.insertSelective(new USer(null,"岳好",1));
		//批量插入 使用可以执行批量操作的sqlsesion
		USerMapper usermapper=sqlsession.getMapper(USerMapper.class);
		for(int i=0;i<20;i++){
    
    
			String name=UUID.randomUUID().toString().substring(0, 5)+""+i;
			usermapper.insertSelective(new USer(null,name,i%5));
		}
		System.out.println("批量添加数据完成");
		
	}
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		
	}

}

这样,我们在执行完后就可衣实现相应的数据库操作了。
码字不易,给个赞呗。

猜你喜欢

转载自blog.csdn.net/pengxiang1998/article/details/114286200