1. 程式人生 > >ssm框架整合入門系列——修改和測試mapper

ssm框架整合入門系列——修改和測試mapper

修改maper–新增自己的sql查詢方法

  1. 修改EmployeeMapper.java 新增:
  <resultMap type="com.liantao.crud.bean.Employee" id="WithDeptResultMap">
  	<id column="emp_id" jdbcType="INTEGER" property="empId" />
    <result column="emp_name" jdbcType="VARCHAR" property="empName" />
    <result column="gender"
jdbcType="CHAR" property="gender" />
<result column="email" jdbcType="VARCHAR" property="email" /> <result column="d_id" jdbcType="INTEGER" property="dId" /> <!-- 指定聯合查詢出的部門欄位的封裝 --> <association property="department" javaType="com.liantao.crud.bean.Department">
<id column="dept" property="deptId"></id> <result column="dept_name" property="deptName"></result> </association> </resultMap> <sql id="WithDept_Column_List"> e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name </sql
>
<!-- Employee selectByPrimaryKey(Integer empId); List<Employee> selectByExampleWithDepat(EmployeeExample example); --> <!-- 查詢員工同時帶部門資訊 --> <select id="selectByExampleWithDept" resultMap="WithDeptResultMap"> select <if test="distinct"> distinct </if> <include refid="WithDept_Column_List" /> FROM tbl_emp e LEFT JOIN tbl_dept d ON e.d_id=d.dept_id <if test="_parameter != null"> <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null"> order by ${orderByClause} </if> </select> <!-- 查詢員工不帶部門資訊的 --> <select id="selectByPrimaryKeyWithDept" resultMap="WithDeptResultMap"> select <include refid="WithDept_Column_List" /> FROM tbl_emp e LEFT JOIN tbl_dept d ON e.d_id=d.dept_id where emp_id = #{empId,jdbcType=INTEGER} </select>
  1. 修改Employee.java,新增:
  //希望查詢員工的同時部門資訊也是查詢好的
  	private Department department;

以上修改,是專案功能要求所新增。

測試

src/test/java 目錄新建MapperTest.java檔案,新增程式碼:

package com.liantao.crud.test;

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.liantao.crud.dao.DepartmentMapper;

/**
 * 測試dao層的工作
 * @author liantao.me
 * 推薦Spring的專案就可以使用Spring的單元測試,可以自動注入我們需要的元件
 * 1.匯入SpringTest模組,在pom.xml新增pendency
 * [email protected]指定Spring配置檔案的位置
 * 3.直接autowired要使用的元件即可
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class MapperTest {
	
	@Autowired
	DepartmentMapper departmentMapper;
	
	/**
	 * 測試DepartmentMapper
	 */
	@Test
	public void testCRUD(){
	/*
	 * 原生測試方法
		//1、建立Spring IOC容器
		ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
		//2、從容器中獲取mapper
		ioc.getBean(DepartmentMapper.class);
	*/
		System.out.println(departmentMapper);
		
	}
	
}

其中包含了SpringTest模組的使用,所以在maven倉庫 拿到相關依賴包,新增到pom.xml

<!-- Spring Test -->
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-test</artifactId>
	    <version>4.3.7.RELEASE</version>
	    <scope>test</scope>
	</dependency>

右鍵 run->JUnit Test 執行結果如下說明成功:

JUnit

接著,分貝測試部門和員工插入方法測試: 記得在DepartmentEmployee類中新增有參、無參建構函式~(myeclipse 右鍵source 下新增即可) 比如Employee的:

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Employee(Integer empId, String empName, String gender, String email, Integer dId) {
		super();
		this.empId = empId;
		this.empName = empName;
		this.gender = gender;
		this.email = email;
		this.dId = dId;
	}
	//1.插入幾個部門
		departmentMapper.insertSelective(new Department(null,"開發部"));
		departmentMapper.insertSelective(new Department(null,"測試部"));
		
		//2.生成員工資料,測試員工插入
		employeeMapper.insertSelective(new Employee(null,"tom","M","[email protected]",1));

新增程式碼:

//1.插入幾個部門
		departmentMapper.insertSelective(new Department(null,"開發部"));
		departmentMapper.insertSelective(new Department(null,"測試部"));
		
		//2.生成員工資料,測試員工插入
		employeeMapper.insertSelective(new Employee(null,"tom","M","[email protected]",1));

批量資料插入方法sqlSession:

在applicationContext.xml下,新增:

	<!-- 配置一個可以執行批量的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>

詳細介紹sqlSession的文章。

MapperTest.java測試類中,自動裝配sqlSession:

@Autowired 
SqlSession sqlSession;

使用方法

//3.批量插入多個員工
		EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
		for(int i=0;i<1000;i++){
			String uid = UUID.randomUUID().toString().substring(0,5)+i;
			mapper.insertSelective(new Employee(null,uid,"M",uid+"@gmail.com",1));
		}
		System.out.println("批量插入員工資料完成!");

UUID 是 通用唯一識別碼(Universally Unique Identifier)的縮寫,是一種軟體建構的標準,亦為開放軟體基金會組織在分散式計算環境領域的一部分。其目的,是讓分散式系統中的所有元素,都能有唯一的辨識資訊,而不需要通過中央控制端來做辨識資訊的指定。如此一來,每個人都可以建立不與其它人衝突的UUID。在這樣的情況下,就不需考慮資料庫建立時的名稱重複問題。目前最廣泛應用的UUID,是微軟公司的全域性唯一識別符號(GUID),而其他重要的應用,則有Linux ext2/ext3檔案系統、LUKS加密分割槽、GNOME、KDE、Mac OS X等等。 UUID簡史 SqlSession 介紹:地址

執行,輸出“批量插入員工資料完成!”即成功。可在資料庫檢視資料。

END