1. Zemian Deng
  2. sandbox

Commits

Zemian Deng  committed e98e1c6

Added update and delete mapper.

  • Participants
  • Parent commits cf40605
  • Branches default

Comments (0)

Files changed (2)

File mybatis-demo/src/test/java/deng/employees/test1/EmployeeTest.java

View file
 package deng.employees.test1;
 
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThan;
 import static org.junit.Assert.assertThat;
 
 import java.io.InputStream;
 		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 		SqlSession session = sqlSessionFactory.openSession();
 		try {
+			Employee employee = null;
 			List<Employee> employees = session.selectList("deng.employees.test1.EmployeeMapper.selectByFirstName", "Zemian");
 			if (employees.size() == 0) {
 				assertThat(employees.size(), equalTo(0));
 				
-				Employee employee = new Employee();
-				//employee.setEmpNo(500001L);
+				employee = new Employee();
 				employee.setFirstName("Zemian");
 				employee.setLastName("Deng");
 				employee.setGender(Gender.M);
 				
 				employees = session.selectList("deng.employees.test1.EmployeeMapper.selectByFirstName", "Zemian");
 				assertThat(employees.size(), equalTo(1));
+				assertThat(employees.get(0).getEmpNo(), greaterThan(0L));
 				System.out.println("Zemian employee inserted. EmpNo=" + employees.get(0).getEmpNo());
+				
+				employee = employees.get(0);
+				employee.setFirstName("Zemian Junior");
+				Long empNo = employee.getEmpNo();
+				employee = session.selectOne("deng.employees.test1.EmployeeMapper.select", empNo);
+				assertThat(employee.getFirstName(), equalTo("Zemian Junior"));
 			} else {
 				System.out.println("Zemian employee has already existed. EmpNo=" + employees.get(0).getEmpNo());
 			}
+			
+			//Delete test employee
+			employee = employees.get(0);
+			Object result = session.delete("deng.employees.test1.EmployeeMapper.delete", employee.getEmpNo());
+			assertThat((Integer)result, equalTo(1));
+			
+			employees = session.selectList("deng.employees.test1.EmployeeMapper.selectByFirstName", "Zemian");
+			assertThat(employees.size(), equalTo(0));
+
+			employees = session.selectList("deng.employees.test1.EmployeeMapper.selectByFirstName", "Zemian Junior");
+			assertThat(employees.size(), equalTo(0));
 		} finally {
 			session.close();
 		}

File mybatis-demo/src/test/resources/deng/employees/test1/EmployeeMapper.xml

View file
 	<select id="selectLimit" resultType="deng.employees.Employee">
 		select * from employees limit ${limit} offset ${offset}
 	</select>
+	
 	<insert id="insert" parameterType="deng.employees.Employee" useGeneratedKeys="true">
 		insert into employees(birth_date, last_name, first_name, gender, hire_date)
 		values (#{birthDate}, #{lastName}, #{firstName}, #{gender}, #{hireDate})
 	</insert>
+	
+	<update id="update" parameterType="deng.employees.Employee">
+		update employees set
+		birth_date=#{birthDate},
+		last_name=#{lastName}, 
+		first_name=#{firstName}, 
+		gender=#{gender}, 
+		hire_date=#{hireDate}
+	</update>
+	
+	<delete id="delete" parameterType="int">
+		delete from employees where emp_no = #{empNo}
+	</delete>
 </mapper>