<acronym id='bjumq'><em id='bjumq'></em><td id='bjumq'><div id='bjumq'></div></td></acronym><address id='bjumq'><big id='bjumq'><big id='bjumq'></big><legend id='bjumq'></legend></big></address>

    <fieldset id='bjumq'></fieldset>

      <dl id='bjumq'></dl>
      <span id='bjumq'></span><i id='bjumq'><div id='bjumq'><ins id='bjumq'></ins></div></i>
    1. <tr id='bjumq'><strong id='bjumq'></strong><small id='bjumq'></small><button id='bjumq'></button><li id='bjumq'><noscript id='bjumq'><big id='bjumq'></big><dt id='bjumq'></dt></noscript></li></tr><ol id='bjumq'><table id='bjumq'><blockquote id='bjumq'><tbody id='bjumq'></tbody></blockquote></table></ol><u id='bjumq'></u><kbd id='bjumq'><kbd id='bjumq'></kbd></kbd>
    2. <i id='bjumq'></i>
      <ins id='bjumq'></ins>

          <code id='bjumq'><strong id='bjumq'></strong></code>

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

          • 时间:
          • 浏览:3
          • 来源:今日新闻-热点新闻-头条新闻

          实现的依据 ,有一种生活不同的风格 ,

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

          另一种生活还是稳重的写xml  ,用resultmap来学会英语。

          经过几天的复习 ,

          这个知识点差那末 来太大了。

          参考URL:

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

          建表语句:

          CREATE TABLE IF NOT EXISTS `user` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `nick_name` varchar(60
          
          ) DEFAULT NULL,
            `address_id` int(11) DEFAULT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
          
          CREATE TABLE IF NOT EXISTS `address` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `province` varchar(60
          
          ) DEFAULT NULL,
            `city` varchar(60
          
          ) DEFAULT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
          
          CREATE TABLE IF NOT EXISTS `car` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `color` varchar(60
          
          ) DEFAULT NULL,
            `name` varchar(60
          
          ) DEFAULT NULL,
            `user_id` int(11) DEFAULT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
          
          INSERT INTO
              `user`
          VALUES
              ('1', 'baby', '1'),
              ('2', 'kingboy', '2'),
              ('3', 'boy', '3'),
              ('4', 'kingbaby', '4');
          
          INSERT INTO
              `address`
          VALUES
              ('1', '北京', '北京'),
              ('2', '天津', '天津'),
              ('3', '安徽', '宿州'),
              ('4', '广东', '广州');
          
          INSERT INTO
              `car`
          VALUES
              ('1', 'green', '路虎', '1'),
              ('2', 'white', '奔驰', '2'),
              ('3', 'blue', '玛莎拉蒂', '4'),
              ('4', 'yellow', '兰博基尼', '4');

          哪几个pojo  ,注意的是那末 用外键关联 ,本来我用了外键来对应 ,哪个表必须取外面的关联的  ,就要在POJO里建立对应的属性。

          package opsstock.paic.com.cn.entity;
          
          public class Address {
          	private Long id;
          	private String province;
          	private String city;
          	public Long getId() {
          		return id;
          	}
          	public void setId(Long id) {
          		this.id = id;
          	}
          	public String getProvince() {
          		return province;
          	}
          	public void setProvince(String province) {
          		this.province = province;
          	}
          	public String getCity() {
          		return city;
          	}
          	public void setCity(String city) {
          		this.city = city;
          	}
          	@Override
          	public String toString() {
          		return "Address [id=" + id + ", province=" + province + ", city=" + city + "]";
          	}
          	
          	
          
          }
          

          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 + "]";
          	}
          	
          	
          
          }
          

          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 + "]";
          	}
          	
          	
          
          }
          

          相关mapper的写法  ,我总真是  ,应该里能 比较复杂那个路径吧。

          package opsstock.paic.com.cn.mapper;
          
          import org.apache.ibatis.annotations.Select;
          
          import opsstock.paic.com.cn.entity.Address;
          
          public interface AddressRepository {
          	
          	@Select("SELECT * FROM `address` WHERE id = #{id}")
          	Address findAddressById(Long id);
          
          }
          

          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);
          
          }
          

          注意@One和@Many的注解哟

          package opsstock.paic.com.cn.mapper;
          
          import org.apache.ibatis.annotations.Result;
          import org.apache.ibatis.annotations.Results;
          import org.apache.ibatis.annotations.Select;
          import org.apache.ibatis.annotations.One;
          import org.apache.ibatis.annotations.Many;
          
          import opsstock.paic.com.cn.entity.User;
          
          public interface UserRepository {
          	
          	@Select("SELECT * FROM `user` WHERE id=#{id}")
          	@Results({
          		@Result(property="address", column="address_id", 
          				one = @One(select = "opsstock.paic.com.cn.mapper.AddressRepository.findAddressById"))
          	})
          	User findUserWithAddress(Long id);
          	
          	
          	@Select("SELECT * FROM `user` WHERE id = #{id}")
          	@Results({
          		@Result(property="cars", column="id",
          				many = @Many(select="opsstock.paic.com.cn.mapper.CarRepository.findCarByUserId"))
          	})
          	User getUserWithCar(Long id);
          }
          

          省掉service  ,直接测试:

          package opsstock.paic.com.cn.controller;
          
          import javax.annotation.Resource;
          
          import org.springframework.web.bind.annotation.GetMapping;
          import org.springframework.web.bind.annotation.PathVariable;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RestController;
          
          import opsstock.paic.com.cn.entity.User;
          import opsstock.paic.com.cn.mapper.UserRepository;
          
          @RestController
          @RequestMapping(value="/user")
          public class UserController {
          	
          	@Resource
          	UserRepository userRepository;
          	
          	@GetMapping(value="/id/{id}/include/address")
          	public String findUserWithAddress(@PathVariable Long id) {
          		User userWithAddress = userRepository.findUserWithAddress(id);
          		return userWithAddress.getAddress().toString();
          	}
          	
          	
          	@GetMapping(value = "/id/{id}/include/car")
              public String findUserWithCar(@PathVariable Long id) {
                  User user = userRepository.getUserWithCar(id);
                  return user.getCars().toString();
              }
          
          	
          
          }
          

          则输入网址:

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

          返回结果:

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

          (那末 理会乱码之事)

          

          猜你喜欢

          欢乐谷娱乐_iPhone 8/XR/11全系列限购 每人最多两部

          3月欢乐谷娱乐19日晚间欢乐谷娱乐,苹果苹果苹果苹果7官网一直对苹果苹果苹果苹果7手机进行限售,目前在售的全系列都包括在内,每人限欢乐谷娱乐购最多两部。查询显示,苹果苹果苹果苹

          2020-03-31

          健鼎国际_缩小版宏光Plus?五菱全新纯电动微型车官图发布

          还记得已经工信部曝光的五菱全新微型纯电动车吗健鼎国际,呆萌的造型,袖珍的身材,妥妥的入门级“老乐头”,前前男友见面们都直呼:小学生也能买的起了。今日,上汽通用五菱官方正式发布了

          2020-03-31

          时时彩平买技巧_疫情导致欧洲超70家车企停工 奔驰:损失无法量化

          随着疫情在海外多个国家的蔓延,除了极大的增加了当地医疗机构的压力之外,还对正常的生产活动造成了巨大的影响。因此欧洲地区确诊病例的快速增长,出于安全考虑及切断病毒的传播途径,欧洲

          2020-03-31

          线上真人赌博网_国产全新奥迪A3长轴距版正式下线:有望年内上市

          近日亲戚亲戚朋友从一汽大众官方微信号获悉,代号为AU381/0CN_KL项目首台整车下线仪式在青岛分公司总装车间举行。据了解线上真人赌博网,此前代号为AU33000/1CN-K

          2020-03-31

          澳门银河在线官方网址_在欧洲抗疫:大白鲨和英国的群体免疫机制的全民实验

          “一只大鱼要吃掉大伙儿儿所有的选民澳门银河在线官方网址,可他(市长)却决定(不关闭海滨),让海滨上的人继续工作。”英国首相鲍里斯在上周四的一次内阁紧急会议上提到了《大白鲨》的情

          2020-03-31