mybatis-day02

resultMap

使用resultType要求列名和属性名一致,不过不一致就无法创建映射,我们可以使用resultMap来解决这种问题


	
	
	


List list = mapper.findAllUser();

一对一

方式一:新建类A 继承已有的类B,再把C的属性放到A中

Orders>User 一对一

public class OrdersCustom extends Orders {

	private int uid;
	private String username;
	private Date birthday;
	private String sex;
	private String address;
}

SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);

List list = mapper.findAllOrders();

方式二:在类A中加入属性类B

public class Orders {
    private Integer id;

    private Integer userId;

    private String number;

    private Date createtime;

    private String note;
    
    private User user;
}

	
	
	
	
	
	
	
		
		
		
		
		
		
	


SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);

List list = mapper.findAllOrders2();

一对多

在一方加入属性多

public class User {
	private int id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;
	
	private List ordersList;
}

SQL


	
	
	
	
	
	
	
	
		
		
		
		
		
	


java

SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);

List list = mapper.findAllUsers();

#与$的区别?#

mybatis和hibernate的区别

源码

https://gitee.com/ssmcqc/cuimybatis01

你可能感兴趣的:(ssm)