(Mybatis)单个对象的映射关系

1、一对一:自动映射

     Orders:

package com.jadan.pojo;

import java.util.Date;

public class Orders {
    private Integer id;
    private Integer userId;
    private String number;
    private Date createtime;
    private String note;

    public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number == null ? null : number.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note == null ? null : note.trim();
    }
}

 

     CustomerOrders:

package com.jadan.pojo;

import java.util.Date;

public class CustomerOrders extends Orders {
	private int uid;
	private String username;// 用户姓名
	private String sex;// 性别
	private Date birthday;// 生日
	private String address;// 地址
	
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

 

     UserMapper.xml 映射文件:






    
	

 

     UserMapper.java 接口文件:

package com.jadan.mapper;

import java.util.List;
import com.jadan.pojo.CustomerOrders;

public interface UserMapper {

	// 一对一: 自动映射
	public List findOrdersAndUser1();
	
}

 

     UserMapperTest.java 测试文件:

package mybatis_day01;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.Before;
import org.junit.Test;
import com.jadan.pojo.CustomerOrders;
import com.jadan.pojo.QueryVo;

public class UserMapperTest {
	
	private SqlSessionFactory factory;
	// 作用: 在测试方法前执行这个初始化方法
	@Before
	public void setUp() throws Exception {
		// 核心配置文件名
		String resource = "SqlMapConfig.xml";
		// 通过流将核心配置文件读取进来
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 通过核心配置文件输入流来创建会话工厂
		factory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	
	// 一对一: 自动映射
	@Test
	public void testFindOrdersAndUser1() {
		SqlSession openSession = factory.openSession();
		// 通过getMapper方法来实例化接口
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		
		List list = mapper.findOrdersAndUser1();
		System.out.println(list);
	}
}

 

2、一对一:手动映射

     Orders:(与上面的Orders多了一个user属性)

package com.jadan.pojo;

import java.util.Date;

public class Orders {
    private Integer id;
    private Integer userId;
    private String number;
    private Date createtime;
    private String note;
    private User user;

    public User getUser() {
	return user;
    }

    public void setUser(User user) {
	this.user = user;
    }

	public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number == null ? null : number.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note == null ? null : note.trim();
    }
}

 

     UserMapper.xml 映射文件:







	
	
		
		
		
		
		
		
		
		
		
		
		
			
			
			
			
			
			
		 
	
	

 

     UserMapper.java 接口文件:

package com.jadan.mapper;

import java.util.List;
import com.jadan.pojo.Orders;

public interface UserMapper {
	
	// 一对一: 手动映射
	public List findOrdersAndUser2();
}

 

     UserMapperTest 测试文件:

package mybatis_day01;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.Before;
import org.junit.Test;

import com.jadan.mapper.UserMapper;
import com.jadan.pojo.Orders;

public class UserMapperTest {
	private SqlSessionFactory factory;
	// 作用: 在测试方法前执行这个初始化方法
	@Before
	public void setUp() throws Exception {
		// 核心配置文件名
		String resource = "SqlMapConfig.xml";
		// 通过流将核心配置文件读取进来
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 通过核心配置文件输入流来创建会话工厂
		factory = new SqlSessionFactoryBuilder().build(inputStream);
	}

        // 一对一: 手动映射
	@Test
	public void testFindOrdersAndUser2() {
		SqlSession openSession = factory.openSession();
		// 通过getMapper方法实例化接口
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		
		List list = mapper.findOrdersAndUser2();
		System.out.println(list);
	}
}

 

下一篇:对集合对象的映射

你可能感兴趣的:(MyBatis)