mybatis一对一的实现

文章展示了如何使用Navicat创建MySQL数据库表`t_ticket`和`t_customer`,以及对应的JavaPOJO类`Customer`和`Ticket`,并演示了一对一映射在MyBatis中的配置和查询方法。
摘要由CSDN通过智能技术生成
/*
 Navicat Premium Data Transfer

 Source Server         : 114.116.11.98
 Source Server Type    : MySQL
 Source Server Version : 50729
 Source Host           : 114.116.11.98:3306
 Source Schema         : mybatis

 Target Server Type    : MySQL
 Target Server Version : 50729
 File Encoding         : 65001

 Date: 05/05/2023 14:24:48
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_ticket
-- ----------------------------
DROP TABLE IF EXISTS `t_ticket`;
CREATE TABLE `t_ticket`  (
  `ticketId` int(11) NOT NULL AUTO_INCREMENT,
  `ticketAddress` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `ticketPrice` int(11) NOT NULL,
  `ticketCId` int(11) NOT NULL,
  PRIMARY KEY (`ticketId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_ticket
-- ----------------------------
INSERT INTO `t_ticket` VALUES (1, '武汉到重庆', 100, 1);
INSERT INTO `t_ticket` VALUES (2, '北京到上海', 200, 1);
INSERT INTO `t_ticket` VALUES (3, '深圳到广州', 50, 1);

SET FOREIGN_KEY_CHECKS = 1;
/*
 Navicat Premium Data Transfer

 Source Server         : 114.116.11.98
 Source Server Type    : MySQL
 Source Server Version : 50729
 Source Host           : 114.116.11.98:3306
 Source Schema         : mybatis

 Target Server Type    : MySQL
 Target Server Version : 50729
 File Encoding         : 65001

 Date: 05/05/2023 14:25:56
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_customer
-- ----------------------------
DROP TABLE IF EXISTS `t_customer`;
CREATE TABLE `t_customer`  (
  `customerId` int(11) NOT NULL AUTO_INCREMENT,
  `customerName` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `customerTel` int(11) NOT NULL,
  PRIMARY KEY (`customerId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_customer
-- ----------------------------
INSERT INTO `t_customer` VALUES (1, '小王', 1888327654);
INSERT INTO `t_customer` VALUES (2, '天天', 1345654635);

SET FOREIGN_KEY_CHECKS = 1;

pojo

public class Customer {
	private Integer customerId;
	private String customerName;
	private Integer customerTel;
	private List<Ticket> tickets;//ʹ��һ��List����ʾ��Ʊ

	public List<Ticket> getTickets() {
		return tickets;
	}

	public void setTickets(List<Ticket> tickets) {
		this.tickets = tickets;
	}

	public Integer getCustomerId() {
		return customerId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public Integer getCustomerTel() {
		return customerTel;
	}

	public void setCustomerTel(Integer customerTel) {
		this.customerTel = customerTel;
	}

	public Customer(Integer customerId, String customerName, Integer customerTel, List<Ticket> tickets) {
		this.customerId = customerId;
		this.customerName = customerName;
		this.customerTel = customerTel;
		this.tickets = tickets;
	}

	public Customer() {
	}

	@Override
	public String toString() {
		return "Customer{" +
				"customerId=" + customerId +
				", customerName='" + customerName + '\'' +
				", customerTel=" + customerTel +
				", tickets=" + tickets +
				'}';
	}
}
public class Ticket {
	private Integer ticketId;
	private String ticketAddress;
	private Integer ticketPrice;
	private Integer ticketCId;
	private Customer customer;//ʹ��һ��customer����ʾ�˿�

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Integer getTicketId() {
		return ticketId;
	}

	public void setTicketId(Integer ticketId) {
		this.ticketId = ticketId;
	}

	public String getTicketAddress() {
		return ticketAddress;
	}

	public void setTicketAddress(String ticketAddress) {
		this.ticketAddress = ticketAddress;
	}

	public Integer getTicketPrice() {
		return ticketPrice;
	}

	public void setTicketPrice(Integer ticketPrice) {
		this.ticketPrice = ticketPrice;
	}

	public Integer getTicketCId() {
		return ticketCId;
	}

	public void setTicketCId(Integer ticketCId) {
		this.ticketCId = ticketCId;
	}

	public Ticket() {
	}

	public Ticket(Integer ticketId, String ticketAddress, Integer ticketPrice, Integer ticketCId, Customer customer) {
		this.ticketId = ticketId;
		this.ticketAddress = ticketAddress;
		this.ticketPrice = ticketPrice;
		this.ticketCId = ticketCId;
		this.customer = customer;
	}

	@Override
	public String toString() {
		return "Ticket{" +
				"ticketId=" + ticketId +
				", ticketAddress='" + ticketAddress + '\'' +
				", ticketPrice=" + ticketPrice +
				", ticketCId=" + ticketCId +
				", customer=" + customer +
				'}';
	}
}

controller

service

public interface DuobiaoService {
    //一对一
    List<Ticket> OnetOne(int id);
   
}

serviceimpl

import com.jdbc.entity.Course;
import com.jdbc.entity.Customer;
import com.jdbc.entity.Student;
import com.jdbc.entity.Ticket;
import com.jdbc.mapper.DuobiaoServiceMapper;
import com.jdbc.mapper.SelectAllMapper;
import com.jdbc.service.DuobiaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional//事物
public class DuobiaoServiceImpl implements DuobiaoService {

    @Autowired
    private DuobiaoServiceMapper duobiaoServiceMapper;
    //一对一
    @Override
    public List<Ticket> OnetOne(int id) {
        return duobiaoServiceMapper.OnetOne(id);
    }
    

}

dao

public interface DuobiaoServiceMapper {
    //一对一
    List<Ticket> OnetOne(int id);
 
}

mapper

<?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.jdbc.mapper.DuobiaoServiceMapper" >
<!--    一对一-->
    <!--    //一对一-->
    <!--    顾客查票一对一-->
    <!--定义数据库字段与实体对象的映射关系 -->
    <resultMap type="com.jdbc.entity.Ticket" id="ticketBean">
        <id column="ticketId" property="ticketId" />
        <result column="ticketAddress" property="ticketAddress" />
        <result column="ticketPrice" property="ticketPrice" />
        <result column="ticketCId" property="ticketCId" />
        <!-- 一对一的关系 -->
        <!-- property: 指的是属性的值, javaType:指的是元素的类型 -->
        <association property="customer" javaType="com.jdbc.entity.Customer">
            <id column="customerId" property="customerId" />
            <result column="customerName" property="customerName" />
            <result column="customerTel" property="customerTel" />
        </association>
    </resultMap>
<!--一对一-->
    <select id="OnetOne" resultMap="ticketBean">
        select c.*,t.* from t_customer c,t_ticket t where
            c.customerId=t.ticketCId and t.ticketId =#{ticketId}
    </select>







</mapper>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值