mybatis入门使用4:返回对象方式

mybatis将查询出的ResultSet封装成了对象,可以有以下2种写法:

1、resultMap="BaseResultMap"

2、resultType="com.lls.model.Employee"













ID, EmployeeName, Position, Salary, Tel, DepartmentID








测试:

package com.lls.test;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lls.mapper.EmployeeMapper;
import com.lls.model.Employee;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/spring-mybatis.xml"})
public class MyBatisSelectReturn {

private static final Logger LOGGER = LoggerFactory.getLogger(MyBatisSelectReturn.class);

@Autowired
private EmployeeMapper employeeMapper;

@Test
public void testResultMap() {
/*
* 在本model对应的 xml中写 更快更方便维护
*/
List employees = employeeMapper.selectHighestSalary();
LOGGER.info("testResultMap employees.size: "+ employees.size());
}

@Test
public void testResultType() {
/*
* 在其他model对应的 xml中写 可以减少没必要的 xml和 mapper
*/
List employees = employeeMapper.selectByEmpName("A");
LOGGER.info("testResultType employees.size: "+ employees.size());
}

}

代码文档:http://download.csdn.net/download/lanlianhua_luffy/9869769


你可能感兴趣的:(mybatis)