1. 程式人生 > >mybatis入門使用2:傳引數

mybatis入門使用2:傳引數

package com.lls.test;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 MyBatisParam {
private static final Logger LOGGER = LoggerFactory.getLogger(MyBatisParam.class);

@Autowired
private EmployeeMapper employeeMapper;
@Test
public void paramObjTest() {
/*
* 物件做引數,需要物件使用到的屬性 get/set方法
*/
Employee employee = new Employee();
employee.setDepartmentid(1);
employee.setEmployeename("xiaoA");
employee.setPosition("manager");
employee.setSalary(13000d);
int result = employeeMapper.insert(employee);
LOGGER.info("result: "+result);
}
@Test
public void paramAnnotationTest() {
/*
* 註解做引數,引數不太多時用
*/
List<Employee> employees = employeeMapper.selectByDepartmentID(1,12000d);
LOGGER.info("employees.size: "+employees.size());
}
@Test
public void paramMapTest() {
/*
* map放入引數,可以方便擴充套件
*/
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("departmentID", 1);
paramMap.put("salary", 12000d);
List<Employee> employees = employeeMapper.selectByDepartmentIDMap(paramMap);
LOGGER.info("employees.size: "+employees.size());
}

}