博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
框架:Mybatis单向多对一
阅读量:5147 次
发布时间:2019-06-13

本文共 7080 字,大约阅读时间需要 23 分钟。

一、假定一个父母有多个孩子,那父母和孩子对应关系为1对多。

二、 1、添加依赖包,参考上篇文章 

        2、初始化数据库和初始数据,以mysql为例

DROP DATABASE IF EXISTS moy_mybatis;CREATE DATABASE moy_mybatis CHARACTER SET UTF8;USE moy_mybatis;DROP TABLE  IF EXISTS t_children;CREATE TABLE t_children(  children_id INT(11),  children_name VARCHAR(50) DEFAULT NULL,  children_birthday DATE DEFAULT NULL,  parent_id INT(11) NOT NULL,  PRIMARY KEY (`children_id`));DROP TABLE  IF EXISTS t_parent;CREATE TABLE t_parent(  id INT(11)  ,  name VARCHAR(50) DEFAULT NULL,  birthday DATE DEFAULT NULL,  PRIMARY KEY (id));INSERT INTO t_children(children_id,children_name,children_birthday,parent_id)VALUES (1,'一号熊孩子',now(),1);INSERT INTO t_children(children_id,children_name,children_birthday,parent_id)VALUES (2,'二号熊孩子',now(),1);INSERT INTO t_children(children_id,children_name,children_birthday,parent_id)VALUES (3,'三号熊孩子',now(),2);INSERT INTO t_parent(id,name,birthday)VALUES (1,'一号父母',now());INSERT INTO t_parent(id,name,birthday)VALUES (2,'二号父母',now());
View Code

       3、新建实体类Children和Parent

package com.moy.mybatis3.entity;import java.util.Date;/** * [Project]:moy-gradle-project  
* [Email]:moy25@foxmail.com
* [Date]:2018/2/19
* [Description]:
* * @author YeXiangYang */public class Children { private int childrenId; private String childrenName; private Date childrenBirthday; public int getChildrenId() { return childrenId; } public void setChildrenId(int childrenId) { this.childrenId = childrenId; } public String getChildrenName() { return childrenName; } public void setChildrenName(String childrenName) { this.childrenName = childrenName; } public Date getChildrenBirthday() { return childrenBirthday; } public void setChildrenBirthday(Date childrenBirthday) { this.childrenBirthday = childrenBirthday; } @Override public String toString() { return "Children{" + "childrenId=" + childrenId + ", childrenName='" + childrenName + '\'' + ", childrenBirthday=" + childrenBirthday + '}'; }}
View Code
package com.moy.mybatis3.entity;import java.util.Date;import java.util.List;/** * [Project]:moy-gradle-project  
* [Email]:moy25@foxmail.com
* [Date]:2018/2/19
* [Description]:
* * @author YeXiangYang */public class Parent { private int id; private String name; private Date birthday; private List
children; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public List
getChildren() { return children; } public void setChildren(List
children) { this.children = children; } @Override public String toString() { return "Parent{" + "id=" + id + ", name='" + name + '\'' + ", birthday=" + birthday + ", children=" + children + '}'; }}
View Code

      4、新建实体对应配置文件Children.xml和Parent.xml

View Code
View Code

      5、编写接口ParentMapper

package com.moy.mybatis3.mapper;import com.moy.mybatis3.entity.Parent;/** * [Project]:moy-gradle-project  
* [Email]:moy25@foxmail.com
* [Date]:2018/2/19
* [Description]:
* * @author YeXiangYang */public interface ParentMapper extends BaseMapper
{ Parent queryParentAndChildren(int id);}
View Code

      6、为了方便测试,编写一个获取SqlSession的工具类Mybatis3Utils

package com.moy.mybatis3.utils;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.Reader;import java.util.Objects;/** * [Project]:moy-gradle-project  
* [Email]:moy25@foxmail.com
* [Date]:2018/2/19
* [Description]:
* * @author YeXiangYang */public abstract class Mybatis3Utils { public static final SqlSessionFactory sqlSessionFactory; public static final ThreadLocal
sessionThread = new ThreadLocal<>(); static { try { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { throw new RuntimeException(e); } } public static SqlSession getCurrentSqlSession() { SqlSession sqlSession = sessionThread.get(); if (Objects.isNull(sqlSession)) { sqlSession = sqlSessionFactory.openSession(); sessionThread.set(sqlSession); } return sqlSession; } public static void closeCurrentSession() { SqlSession sqlSession = sessionThread.get(); if (Objects.nonNull(sqlSession)) { sqlSession.close(); } sessionThread.set(null); }}
View Code

     7、新建mybatis配置信息文件mybatis-config.xml

View Code

     8、编写测试类ParentMapperTest

package com.moy.mybatis3.mapper;import com.moy.mybatis3.entity.Children;import com.moy.mybatis3.entity.Parent;import com.moy.mybatis3.utils.Mybatis3Utils;import org.apache.ibatis.session.SqlSession;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.util.List;import static org.junit.Assert.*;/** * [Project]:moy-gradle-project  
* [Email]:moy25@foxmail.com
* [Date]:2018/2/20
* [Description]:
* * @author YeXiangYang */public class ParentMapperTest { SqlSession sqlSession; ParentMapper parentMapper; @Before public void before() { sqlSession = Mybatis3Utils.getCurrentSqlSession(); parentMapper = sqlSession.getMapper(ParentMapper.class); } @After public void after() { Mybatis3Utils.closeCurrentSession(); } @Test public void queryParentAndChildren() { Parent parent = parentMapper.queryParentAndChildren(1); System.out.println(parent); }}
View Code

 

yexiangyang

moyyexy@gmail.com


 

转载于:https://www.cnblogs.com/moy25/p/8455879.html

你可能感兴趣的文章
Android Studio 学习(六)内容提供器
查看>>
作业1:求500到1000之间有多少个素数,并打印出来
查看>>
for循环:用turtle画一颗五角星
查看>>
浅谈JavaScript中的eval()
查看>>
操作系统学习(七) 、保护机制概述
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>
MySQL建表语句+添加注释
查看>>
[Leetcode][JAVA] LRU Cache
查看>>
本周内容
查看>>
js兼容公用方法
查看>>
如何将应用完美迁移至Android P版本
查看>>
【转】清空mysql一个库中的所有表的数据
查看>>
基于wxPython的python代码统计工具
查看>>
淘宝JAVA中间件Diamond详解(一)---简介&快速使用
查看>>
一种简单的数据库性能测试方法
查看>>
如何给JavaScript文件传递参数
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
Kettle学习系列之Kettle能做什么?(三)
查看>>
电脑没有安装iis,但是安装了.NET环境,如何调试网站发布的程序
查看>>
【Mac + GitHub】之在另一台Mac电脑上下载GitHub的SSH链接报错
查看>>