MyBatis 及联查询
⼀对多
//创建两个实体 public class Student { private long id; private String name; private Classes classes; } public class Classes { private long id; private String name; private List<Student> students; } //创建mapper @Mapper public interface StudentMapper { Student findById(long id); } //创建Mapper.xml <?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"> <!--第 1 步绑定 mapper接口全类名--> <mapper namespace="com.example.springbootjdbc.mapper.StudentMapper"> <!--id:随便命名 type:实体的全路径 全路径:包名+类名--> <resultMap id="studentMap" type="com.example.springbootjdbc.entity.Student"> <!--column:sql语句里面字段名 property:实体里面的属性名--> <id column="id" property="id"/> <result column="name" property="name"/> <!-- association:返回一个实体用association property:实体里面的属性名 javaType:实体的全路径 --> <association property="classes" javaType="com.example.springbootjdbc.entity.Classes"> <id column="cid" property="id"/> <result column="cname" property="name"/> </association> </resultMap> <select id="findById" parameterType="long" resultMap="studentMap"> SELECT s.id,s.name,c.id as cid,c.name as cname FROM student s,classes c where s.id=#{id} and s.cid=c.id </select> </mapper> //测试 @SpringBootTest class SpringbootJdbcApplicationTests { @Autowired private StudentMapper studentMapper; @Test void getStudent() { Student student = studentMapper.findById(1); System.out.println(student); } }
反向一对多
返回是一个集合的处理
//创建Mapper @Mapper public interface ClassesMapper { Classes findById(long id); } //创建Mapper.xml <?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"> <!--第 1 步绑定 mapper接口全类名--> <mapper namespace="com.example.springbootjdbc.mapper.ClassesMapper"> <!--id:随便命名 type:实体的全路径 全路径:包名+类名--> <resultMap id="classesMap" type="com.example.springbootjdbc.entity.Classes"> <!--column:sql语句里面字段名 property:实体里面的属性名--> <id column="cid" property="id"/> <result column="cname" property="name"/> <!-- collection:返回一个集合用association property:实体里面的属性名 javaType:实体的全路径 --> <collection property="studentList" ofType="com.example.springbootjdbc.entity.Student"> <id column="id" property="id"/> <result column="name" property="name"/> </collection> </resultMap> <select id="findById" parameterType="long" resultMap="classesMap"> SELECT s.id,s.name,c.id as cid,c.name as cname FROM student s,classes c where c.id=#{id} and s.cid=c.id </select> </mapper>
多对多关系
多对多关系中用到了一个中间表customer_goods
//创建实体 @Data public class Goods { private long id; private String name; private List<Customer> customers; } @Data public class Customer { private long id; private String name; private List<Goods> goods; } //创建Mapper @Mapper public interface CustomerMapper { public Customer find(long id); } @Mapper public interface GoodsMapper { public Goods find(long id); } //创建CustomerMapper.xml <?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"> <!--第 1 步绑定 mapper接口全类名--> <mapper namespace="com.example.springbootjdbc.mapper.CustomerMapper"> <!--id:随便命名 type:实体的全路径 全路径:包名+类名--> <resultMap id="customerMap" type="com.example.springbootjdbc.entity.Customer"> <!--column:sql语句里面字段名 property:实体里面的属性名--> <id column="cid" property="id"/> <result column="cname" property="name"/> <!-- collection:返回一个集合用association ofType:泛型类型 property:实体里面的属性名 javaType:实体的全路径 --> <collection property="goods" ofType="com.example.springbootjdbc.entity.Goods"> <id column="gid" property="id"/> <result column="gname" property="name"/> </collection> </resultMap> <select id="find" parameterType="long" resultMap="customerMap"> SELECT c.id cid,c.name cname,g.id gid,g.name gname FROM customer c,goods g,customer_goods cg WHERE c.id=#{id} and c.id=cg.cid and g.id=cg.gid </select> </mapper> //创建GoodsMapper.xml <?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"> <!--第 1 步绑定 mapper接口全类名--> <mapper namespace="com.example.springbootjdbc.mapper.GoodsMapper"> <!--id:随便命名 type:实体的全路径 全路径:包名+类名--> <resultMap id="goodsMap" type="com.example.springbootjdbc.entity.Goods"> <!--column:sql语句里面字段名 property:实体里面的属性名--> <id column="gid" property="id"/> <result column="gname" property="name"/> <!-- collection:返回一个集合用association property:实体里面的属性名 javaType:实体的全路径 --> <collection property="customers" ofType="com.example.springbootjdbc.entity.Customer"> <id column="cid" property="id"/> <result column="cname" property="name"/> </collection> </resultMap> <select id="find" parameterType="long" resultMap="goodsMap"> SELECT c.id cid,c.name cname,g.id gid,g.name gname FROM customer c,goods g,customer_goods cg WHERE g.id=#{id} and c.id=cg.cid and g.id=cg.gid </select> </mapper>
Sql语句
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50642 Source Host : localhost:3306 Source Database : jdbc Target Server Type : MYSQL Target Server Version : 50642 File Encoding : 65001 Date: 2020-07-14 15:53:02 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for book -- ---------------------------- DROP TABLE IF EXISTS `book`; CREATE TABLE `book` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `author` varchar(20) DEFAULT NULL, `publish` varchar(20) DEFAULT NULL, `pages` int(10) DEFAULT NULL, `price` float(10,2) DEFAULT NULL, `bookcaseid` int(10) DEFAULT NULL, `abled` int(10) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK_ieh6qsxp6q7oydadktc9oc8t2` (`bookcaseid`), CONSTRAINT `FK_ieh6qsxp6q7oydadktc9oc8t2` FOREIGN KEY (`bookcaseid`) REFERENCES `bookcase` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of book -- ---------------------------- INSERT INTO `book` VALUES ('1', '解忧杂货店', '东野圭吾', '电子工业出版社', '102', '27.30', '9', '1'); INSERT INTO `book` VALUES ('2', '追风筝的人', '卡勒德·胡赛尼', '中信出版社', '330', '26.00', '1', '1'); INSERT INTO `book` VALUES ('3', '人间失格', '太宰治', '作家出版社', '150', '17.30', '1', '1'); INSERT INTO `book` VALUES ('4', '这就是二十四节气', '高春香', '电子工业出版社', '220', '59.00', '3', '1'); INSERT INTO `book` VALUES ('5', '白夜行', '东野圭吾', '南海出版公司', '300', '27.30', '4', '1'); INSERT INTO `book` VALUES ('6', '摆渡人', '克莱儿·麦克福尔', '百花洲文艺出版社', '225', '22.80', '1', '1'); INSERT INTO `book` VALUES ('7', '暖暖心绘本', '米拦弗特毕', '湖南少儿出版社', '168', '131.60', '5', '1'); INSERT INTO `book` VALUES ('8', '天才在左疯子在右', '高铭', '北京联合出版公司', '330', '27.50', '6', '1'); INSERT INTO `book` VALUES ('9', '我们仨', '杨绛', '生活.读书.新知三联书店', '89', '17.20', '7', '1'); INSERT INTO `book` VALUES ('10', '活着', '余华', '作家出版社', '100', '100.00', '6', '1'); INSERT INTO `book` VALUES ('11', '水浒传', '施耐庵', '三联出版社', '300', '50.00', '1', '1'); INSERT INTO `book` VALUES ('12', '三国演义', '罗贯中', '三联出版社', '300', '50.00', '2', '1'); INSERT INTO `book` VALUES ('13', '红楼梦', '曹雪芹', '三联出版社', '300', '50.00', '5', '1'); INSERT INTO `book` VALUES ('14', '西游记', '吴承恩', '三联出版社', '300', '60.00', '3', '1'); -- ---------------------------- -- Table structure for classes -- ---------------------------- DROP TABLE IF EXISTS `classes`; CREATE TABLE `classes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of classes -- ---------------------------- INSERT INTO `classes` VALUES ('1', '语文'); INSERT INTO `classes` VALUES ('2', '数学'); INSERT INTO `classes` VALUES ('3', '英语'); -- ---------------------------- -- Table structure for customer -- ---------------------------- DROP TABLE IF EXISTS `customer`; CREATE TABLE `customer` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of customer -- ---------------------------- INSERT INTO `customer` VALUES ('1', '张三'); INSERT INTO `customer` VALUES ('2', '李四\r\n'); INSERT INTO `customer` VALUES ('3', '王五'); -- ---------------------------- -- Table structure for customer_goods -- ---------------------------- DROP TABLE IF EXISTS `customer_goods`; CREATE TABLE `customer_goods` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cid` int(11) DEFAULT NULL, `gid` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of customer_goods -- ---------------------------- INSERT INTO `customer_goods` VALUES ('1', '1', '2'); INSERT INTO `customer_goods` VALUES ('2', '1', '3'); INSERT INTO `customer_goods` VALUES ('3', '2', '1'); INSERT INTO `customer_goods` VALUES ('4', '3', '1'); -- ---------------------------- -- Table structure for depaetment -- ---------------------------- DROP TABLE IF EXISTS `depaetment`; CREATE TABLE `depaetment` ( `dep_name` varchar(255) DEFAULT '', `d_id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`d_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of depaetment -- ---------------------------- INSERT INTO `depaetment` VALUES ('研发部', '1'); INSERT INTO `depaetment` VALUES ('销售', '2'); -- ---------------------------- -- Table structure for employee -- ---------------------------- DROP TABLE IF EXISTS `employee`; CREATE TABLE `employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `last_name` varchar(255) NOT NULL DEFAULT '', `email` varchar(255) DEFAULT '', `gender` int(2) NOT NULL, `d_id` int(100) NOT NULL, PRIMARY KEY (`id`,`d_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of employee -- ---------------------------- INSERT INTO `employee` VALUES ('1', 'zhnag', '232@qq.com', '1', '1'); INSERT INTO `employee` VALUES ('2', 'asddas', 'sdsdf@qq.cn', '1', '2'); INSERT INTO `employee` VALUES ('3', 'asddas', 'sdsdf@qq.cn', '1', '2'); INSERT INTO `employee` VALUES ('4', 'asddas', 'sdsdf@qq.cn', '1', '1'); -- ---------------------------- -- Table structure for goods -- ---------------------------- DROP TABLE IF EXISTS `goods`; CREATE TABLE `goods` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of goods -- ---------------------------- INSERT INTO `goods` VALUES ('1', '电视'); INSERT INTO `goods` VALUES ('2', '冰箱'); INSERT INTO `goods` VALUES ('3', '洗衣机'); -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(11) DEFAULT NULL, `cid` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', '张三', '2'); INSERT INTO `student` VALUES ('2', '李四', '2'); INSERT INTO `student` VALUES ('3', '王五', '3'); -- ---------------------------- -- Table structure for t_account -- ---------------------------- DROP TABLE IF EXISTS `t_account`; CREATE TABLE `t_account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(11) DEFAULT NULL, `password` varchar(11) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_account -- ---------------------------- INSERT INTO `t_account` VALUES ('1', '尺寸', 'asfdfsd', '12'); INSERT INTO `t_account` VALUES ('2', '尺寸', 'asfdfsd', '12'); -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `phone` varchar(11) DEFAULT NULL COMMENT '手机号', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('11', 'admin', '123456', '15233551994');