spring boot + mybatis实现一对一,一对多的样码之一种

  • 时间:
  • 浏览:0

http://blog.csdn.net/KingBoyWorld/article/details/78966789

建表得话:

相关mapper的写法,我总真是,应该还须要繁复那个路径吧。

[Car [id=3, name=ÂêɯÀ­µÙ, color=blue, userId=4], Car [id=4, name=À¼²©»ùÄá, color=yellow, userId=4]]

(如此 理会乱码之事)

http://localhost:3030/user/id/4/include/car

注意@One和@Many的注解哟

有有一种是直接在接口里用注解实现,

这些知识点差太大 了。

经过几天的复习,

另有有一种还是稳重的写xml,用resultmap来搞懂。

省掉service,直接测试:

几只pojo,注意的是如此 用外键关联,而是 用了外键来对应,哪个表须要取外面的关联的,就要在POJO里建立对应的属性。

package opsstock.paic.com.cn.entity;

import java.util.List;

public class User {
	private Long id;
	private String name;
	private Address address;
	private Long addressId;
	private List<Car> cars;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Long getAddressId() {
		return addressId;
	}
	public void setAddressId(Long addressId) {
		this.addressId = addressId;
	}
	public List<Car> getCars() {
		return cars;
	}
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", addressId=" + addressId + "]";
	}
	
	

}

参考URL:

package opsstock.paic.com.cn.entity;

public class Car {
	private Long id;
	private String name;
	private String color;
	private Long userId;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	@Override
	public String toString() {
		return "Car [id=" + id + ", name=" + name + ", color=" + color + ", userId=" + userId + "]";
	}
	
	

}

package opsstock.paic.com.cn.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import opsstock.paic.com.cn.entity.Car;

public interface CarRepository {
	
	@Select("SELECT * FROM `car` WHERE user_id = #{userId}")
	List<Car> findCarByUserId(Long userId);

}

实现的土法律依据,有有有一种不同的风格,

则输入网址:

返回结果: