/*
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>