Commits

Zemian Deng committed 76c382a

Added update commit to myBatis test. Added Jackson deserialization sample for db insert.

Comments (0)

Files changed (4)

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

 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.hamcrest.Matchers;
 import org.junit.Test;
 
 import deng.employees.Employee;
 	}
 	
 	@Test
-	public void testInsert() throws Exception {
+	public void testInsertUpdateDelete() throws Exception {
 		String resource = "deng/employees/test1/mybatis-config.xml";
 		InputStream inputStream = Resources.getResourceAsStream(resource);
 		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 				
 				employee = employees.get(0);
 				employee.setFirstName("Zemian Junior");
+				result = session.update("deng.employees.test1.EmployeeMapper.update", employee);
+				session.commit();
+				assertThat((Integer)result, equalTo(1));
+
 				Long empNo = employee.getEmpNo();
 				employee = session.selectOne("deng.employees.test1.EmployeeMapper.select", empNo);
 				assertThat(employee.getFirstName(), equalTo("Zemian Junior"));
+				
+				//Delete test employee
+				result = session.delete("deng.employees.test1.EmployeeMapper.delete", empNo);
+				session.commit();
+				assertThat((Integer)result, equalTo(1));
 			} 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());
+				session.commit();
+				assertThat((Integer)result, equalTo(1));
 			}
 			
-			//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));
 
 			session.close();
 		}
 	}
+	
+	@Test
+	public void testJsonInsert() throws Exception {
+		String resource = "/deng/employees/employee-01.json";
+		InputStream inStream = getClass().getResourceAsStream(resource);
+		assertThat(inStream, Matchers.notNullValue())
+		;
+		ObjectMapper mapper = new ObjectMapper();
+		mapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
+		
+		Employee emp = mapper.readValue(inStream, Employee.class);
+		inStream.close();
+		System.out.println(mapper.writeValueAsString(emp));
+		
+		resource = "deng/employees/test1/mybatis-config.xml";
+		InputStream inputStream = Resources.getResourceAsStream(resource);
+		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
+		SqlSession session = sqlSessionFactory.openSession();
+		try {
+			Object result = session.insert("deng.employees.test1.EmployeeMapper.insert", emp);
+			session.commit();
+			assertThat((Integer)result, equalTo(1));
+
+			emp = session.selectOne("deng.employees.test1.EmployeeMapper.selectByFirstName", "Zemian");
+			System.out.println(mapper.writeValueAsString(emp));
+
+			result = session.delete("deng.employees.test1.EmployeeMapper.delete", emp.getEmpNo());
+			session.commit();
+			assertThat((Integer)result, equalTo(1));
+		} finally {
+			session.close();
+		}
+	}
 }

mybatis-demo/src/test/resources/deng/employees/employee-01.json

+{
+	"birthDate": "01/01/1970",
+	"firstName":"Zemian",
+	"lastName":"Deng",
+	"gender":"M",
+	"hireDate": "10/14/2012"
+}

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

Empty file added.

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

 	
 	<update id="update" parameterType="deng.employees.Employee">
 		update employees set
-		birth_date=#{birthDate},
-		last_name=#{lastName}, 
-		first_name=#{firstName}, 
-		gender=#{gender}, 
-		hire_date=#{hireDate}
+			birth_date=#{birthDate},
+			last_name=#{lastName}, 
+			first_name=#{firstName}, 
+			gender=#{gender}, 
+			hire_date=#{hireDate}
+		where emp_no = #{empNo}
 	</update>
 	
 	<delete id="delete" parameterType="int">