ssm_crud 测试mapper生成结果

本文介绍了一个使用MyBatis进行员工信息批量插入的例子。在Java环境下,通过Spring JUnit4ClassRunner和ContextConfiguration注解配置了测试环境,并利用UUID生成唯一标识符实现了10条记录的快速插入。

整体如图。

a. 在com.taotao.ssm.test包,新建,MapperTest.java

这里顺便记下口诀:类为大驼峰命名规则,其它者为小驼峰规则。

package com.taotao.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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.taotao.ssm.bean.Employee;
import com.taotao.ssm.dao.DepartmentDao;
import com.taotao.ssm.dao.EmployeeDao;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class MapperTest {

	@Autowired
	DepartmentDao departmentDao;

	@Autowired
	EmployeeDao employeeDao;

	@Autowired
	SqlSession sqlSession;

	@Test
	public void testCRUD() {
		// ApplicationContext ioc=new
		// ClassPathXmlApplicationContext("applicationContext.xml");
		// DepartmentDao departmentDao=ioc.getBean(DepartmentDao.class);
		System.out.println(departmentDao);
		System.out.println(employeeDao);
		System.out.println(sqlSession);

		// departmentDao.insert(new Department(null,"研发部"));
		// employeeDao.insert(new Employee(null,"tomcat","F","tomcat@126.com",2));

		EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
		for (int i = 0; i < 10; i++) {
			String uid = UUID.randomUUID().toString().substring(0, 4);
			employeeDao.insertSelective(new Employee(null, uid, "M", uid + "@baidu.com", 2));
		}
		System.out.println("批量完成!");
	}

}

b.运行 juint

c.如有错,请留言。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值