mybatis-210718-03---映射文件_参数处理_POJO_Map_TO

mybatis-210718-03—映射文件_参数处理_POJO_Map_TO


POJO:
	如果多个参数正好是我们业务逻辑的数据模型,我们就可以直接传入pojo
		#{属性名}:取出传入的pojo的属性值
		
Map:
	如果多个参数不是业务模型中的数据,没有对应的pojo,为了方便,我们也可以传入map
		#{key}:取出map中对应的值

TO:
	如果多个参数不是业务模型中的数据,但是经常使用,推荐来编写了TO(Transfer Object)数据传输对象
	page{
		int index;
		int size;
	}

Map示例

EmployeeMapper.java

package com.bgy.mybatis.dao;

import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.bgy.mybatis.bean.Employee;

public interface EmployeeMapper {
	public Employee getEmpByMap(Map<String,Object> map);
}

EmployeeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bgy.mybatis.dao.EmployeeMapper">
	<!--  -->
	<select id="getEmpByMap" resultType="com.bgy.mybatis.bean.Employee">
    select 
    	id,last_name,gender,email
    from
    	tbl_employee 
    where 
    	id = #{id} and last_name=#{lastName}
  </select>
</mapper>

MybatisTest.java

package com.bgy.mybatis.test;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import com.bgy.mybatis.bean.Employee;
import com.bgy.mybatis.dao.EmployeeMapper;
import com.bgy.mybatis.dao.EmployeeMapperAnnotation;

class MybatisTest {
	
	public SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		return new SqlSessionFactoryBuilder().build(inputStream);
	}

	/**
	 * 测试注解式映射
	 * @throws IOException
	 */
	@Test
	public void test02() throws IOException{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		
		SqlSession openSession = sqlSessionFactory.openSession();
		
		try {
			EmployeeMapperAnnotation mapper = openSession.getMapper(EmployeeMapperAnnotation.class);
			Employee employee = mapper.getEmpById(1);
			
			System.out.println("test02");
			System.out.println(employee);
		} finally {
			openSession.close();
		}
	}
	
	/**
	 * 测试map
	 * @throws IOException
	 */
	@Test
	public void test03() throws IOException{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		try {
			EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
			
            // 把参数信息放进map集合中
			Map<String,Object> map = new HashMap();
			map.put("id", 1);
			map.put("lastName","zhangsan");
			
			Employee employee = mapper.getEmpByMap(map);
			System.out.println(employee);
		} finally {
			sqlSession.close();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值