mybatis:多对一、一对多查询(详解)

MyBatis 的关联关系配置 一对多,一对一,多对多 关系的映射处理 MyBatis 的关联关系配置 一对多,一对一,多对多 关系的映射处理及好处 阅读详情

目录

问题、需求

数据库设置方法

解决多对一问题

实体类的设置方法

sql语句

业务层

xml的写法

查询结果

解决一对多问题

实体类设置

sql语句

业务层

xml写法

查询结果


问题、需求

多对一问题:查出每个学生的信息,并 查出他班主任的信息

一对多问题:查出每个老师的信息,并 查出他的所有学生的信息

一对多:一个班主任有很多的学生

多对一:很多学生有一个班主任

数据库设置方法

在多的那一方添加一个字段,这个字段取自于一的那一方表的id

为什么是一的那一方的id而不是name?

        这里只能选一的那一方的id,如果你选择了一的那一方的name,那耦合度就很高了,如果班级改名字了,那你就得改两个表,而id就不会出现这种问题,这叫解耦

学生表,需要注意的是:t_id 表示学生对应的班主任

 班主任表

解决多对一问题

多对一问题:查出每个学生的信息,并 查出他班主任的信息

实体类的设置方法

学生类:额外加上一个老师类的对象,用来查询这个学生对应的班主任

注意自己加上get、set、toString

    private Integer id;
    private String Sname;
    private String sex;
    private Integer age;
    private Integer t_id;
    //Teacher属性用于查询学生的老师时使用
    private Teacher teacher;

班主任类:中规中矩

    private Integer id;
    private String Tname;

sql语句

下面两种都可以

select * from student left join teacher on student.t_id = teacher.id;
SELECT * FROM student s,teacher t where s.t_id = t.id

业务层

//    查询每个老师的信息以及其对应学生 1
public List<Teacher> getTeacher1();

//    查询每个老师的信息以及对应学生 2
public List<Teacher> getTeacher2();
 

xml的写法

第一种形式:按照查询嵌套处理

就是将两个查询语句嵌套起来

第二种形式:按照结果嵌套处理

就是对复杂字段进行单独处理

<!--多对一:第1种-->
<!--查询每个学生的对应的老师:方法1-->
<!--第一种:    按照查询嵌套处理-->
<!--
    1.查询所有的学生信息
    2.根据查询出来的t_id,寻找对应的老师
 -->
<!--    注意学生实体类当中要有老师类的对象才能实现关联查询-->
<!--  resultMap:返回关联的结果映射-->
<select id = "getStudent1"  resultMap="StudentTeacher1">
    select * from student;
</select>
<!--结果映射集-->
<resultMap id="StudentTeacher1"  type="com.qcby.entity.Student">
    <result property="id" column="id"/>
    <result property="Sname" column="Sname"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="t_id" column="t_id"/>
    <!-- 复杂的属性我们需要单独去处理 对象:association   集合:collection   -->
    <!-- property="teacher" student类当中的关联字段 -->
    <!-- column="t_id" 两个表的关联字段-->
    <!-- javaType="com.javen.model.Teacher" 为复杂属性设置类类型-->
    <!-- select="getTeacher"  :调用下一个查询语句       -->
    <association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/>
</resultMap>

<select id="getTeacher" resultType="com.qcby.entity.Teacher">
    select  * from  teacher where id = #{t_id};    <!-- #{id}; 可以写任何东西,因为会自动匹配 t_id -->
</select>


<!--多对一:第2种-->
<!--查询每个学生的对应的老师:方法2-->
<!--第二种:    按照结果嵌套处理-->
<!--    按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
    SELECT
        *
    FROM
        student
            LEFT JOIN teacher ON student.t_id = teacher.id
</select>
<resultMap id="StudentTeacher2" type="com.qcby.entity.Student">
    <result property="id" column="id"/>
    <result property="Sname" column="Sname"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="t_id" column="t_id"/>
    <association property="teacher" javaType="com.qcby.entity.Teacher">
        <result property="id" column="id"/>
        <result property="Tname" column="Tname"/>
    </association>
</resultMap>
 

查询结果

解决一对多问题

一对多问题:查出每个老师的信息,并 查出他的所有学生的信息

实体类设置

学生类:中规中矩

    private Integer id;
    private String Sname;
    private String sex;
    private Integer age;
    private Integer t_id;

班主任类:需要加上一个学生集合

    private Integer id;
    private String Tname;
    //Student集合 用于查询老师的学生时使用
    private List<Student> students;

sql语句

下面两种都可以

  SELECT  * FROM teacher
            LEFT JOIN student  on student.t_id = teacher.id
SELECT * FROM student s,teacher t where s.t_id = t.id

业务层

    //查询每个学生的信息以及其对应的老师:方法1
    public List<Student> getStudent1();
    //查询每个学生的的信息以及其对应的老师:方法2
    public List<Student> getStudent2();

xml写法

第一种形式:按照查询嵌套处理

就是在xml里面嵌套两个查询语句

第二种形式:按照结果嵌套处理

在xml里面,对复杂的属性(这里就是学生的信息是复杂的)单独处理


<!--一对多:第1种-->
<!--查询每个老师有多少学生:第一种-->
<!--第一种:按照查询嵌套处理-->
<select id="getTeacher1" resultMap="TeacherStudent1">
    select * from teacher
</select>
<resultMap id="TeacherStudent1" type="com.qcby.entity.Teacher">
    <collection property="students" column="id" javaType="ArrayList"
                ofType="com.qcby.entity.Student" select="getStudentByTeacherId" />
</resultMap>
<select id="getStudentByTeacherId" resultType="com.qcby.entity.Student">
    select * from student where t_id = #{t_id}
</select>


<!--一对多:第2中-->
<!--查询每个老师有多少学生:第二种-->
<!--按照结果进行查询-->
<select id="getTeacher2" resultMap="TeacherStudent2">
    SELECT
        *
    FROM
        teacher
            LEFT JOIN student ON student.t_id = teacher.id
</select>
<resultMap id="TeacherStudent2" type="com.qcby.entity.Teacher">
    <result property="id" column="id"/>
    <result property="Tname" column="Tname"/>

    <!-- 复杂的属性我么需要单独去处理 对象:association   集合:collection
      在集合中的泛型信息,我们使用ofType获取
      -->
    <collection property="students" ofType="com.qcby.entity.Student">
        <result property="Sname" column="Sname"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <result property="t_id" column="t_id"/>
    </collection>
</resultMap>
 

查询结果

【转】MyBatis 详解(一对一,一对多,多对多) 目录 1、什么是MyBatis? 2、MyBatis 入门实例基于xml配置 3、MyBatis 入门实例注解配置 4、MyBatis 入门实例 一对一 基于xml配置 4、MyBatis 入门实例 一对多,多对一 基于xml配置 5、MyBatis 入门实例 多对多 基于xml配置 回到顶部 1、什么是MyBatis?   MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了goo... 阅读详情

相关推荐

处理 Mybatis 中一对多、多对一、多对多映射的黑魔法

前言先看看Mybatis官方介绍MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。注意,这里是简单的XML! 可是,当我们的数据库表关系错综复杂,表与表之存在一对多、多对一、多对多

小浩子的博客 8397

详解Mybatis一对多、多对一、多对多

Mybatis一对一、一对多、多对多

菜鸟逆袭之路 2万+

Mybatis的多对一与一对多查询 Mapper.xml 讲解

文章目录Mybatis、一、数据库搭建二、多对一查询**1.创建Student实体类****2.创建Teacher实体类****3.StudentMapper构建****4. StudentMapper.xml 讲解(只展示核心代码部分)****4.1 按照查询嵌套处理****4.2 按照结果嵌套处理**三、一对多1. 创建Student实体类2. 创建Teacher实体类对象3. TeacherMapper构建4. TeacherMapper.xml讲解(只展示核心代码部分)4.1 按照结果嵌套处理4

JssenJ的博客 1584

Mybatis结果映射——一对多

我们知道 Mybatis 的结果映射,如果遇到实体类的属性是自定义,事情就不会那么简单。可能会有一对多,多对一,多对多的情况产生。本文讲解一下多对一的情况。 多对一,指的是 A 表的多行对应 B 表的某一行,就是 A 的多个实例对应 B 的一个实例。比如我们有一张博客表,表里记录了作者,题目,时间,阅读量等信息。还有一张读者表。表里记录了作者姓名、地址等信息。 Blog 表中的多条记录,对应了 Author 表中的一条记录,这就是多对一。...

pan2635376816的博客 1万+

李宏毅机器学习hw3

Homework 3 - Convolutional Neural Network 1作业概述 在food_11文件中有三个文件分别是training、validation、testing,打开training和validation可以看见总共有11种食物(0-10的类别编号),而testing是没有类别编号的。 然后用卷积神经网络对食物图片进行分类。 2导入用到的库 torch库 这个我弄了半天,直接pip总是timeout,用镜像源下下来又出现OSError: [WinError..

老问题的博客 4186

Kubernetes集群环境搭建与初始化

Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理。在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以便对应用请求进行负载均衡。在Kubernetes中,我们可以创建多个容器,每个容器里面运行一个应用实例,然后通过内置的负载均衡策略,实现对这一组应用实例的管理、发现、访问,而这些细节都不需要运维人员去进行复杂的手工配置和处理。

m0_75254642的博客 1522

MyBatis框架(二):多对一查询、一对多查询、ResultMap、动态SQL

在mybatis框架下写代码的步骤: 创建实体类创建对应的接口写每个接口对应的xml文件编写测试类 在一对多查询和多对一查询之前,先准备数据库 以老师和学生为例 老师的数据库 CREATE TABLE `teacher` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGI...

她长眠于月色 278

HDMI设计2----EDID and E-EDID

1,EDID是什么?有什么用? 图1 应用场景 如图1所示的应用场景,PC将视频信号发送 到我们的设备上,经过一系列的传输和切换,设备将视频信号发送到显示器或者投影机上,也就是说,我们的设备位于PC和显示器之间。首先,在输入端,设备需要让PC知道自己是能够接受输入视频信号的,让PC认为自己是一台显示器。如果不这样做的话,PC可能会拒绝输出任何视频信号的(VGA接口不需要EDID也能输出)。除此之外,设备还要告诉PC自己支持的显示时序。而在输出端,当设备需要自建输出时序而不是简单的复制...

Archar_Saber的博客 7986

mybatis 一对多查询_Java自学之mybatis:使用注解方式多对一查询

学习目的:学习使用注解方式实现多对一查询,查询出所有的Product,通过product_中的cid查询到所属的Category。Part 1mapperCategoryMapperpackage cn.vaefun.mapper; import cn.vaefun.pojo.Category; import org.apache.ibatis.annotations.*; import ja...

weixin_39643255的博客 73

人形机器人硬件开发:骨架系统设计全攻略

本文以人形机器人骨架系统为核心研究对象,系统解析其从理论建模到工程实现的全流程设计方法。通过融合刚体力学、材料科学与机器人控制理论,构建"概念基础-理论框架-架构设计-实现机制-实际应用-高级考量-综合拓展"的七层分析体系。重点覆盖运动学/动力学建模、轻量化结构设计、关节模块集成、多物理场耦合优化等关键技术,结合波士顿动力Atlas、本田ASIMO等经典案例,提供从原型设计到量产验证的工程实践指南。

AI天才研究院 1867

RK3588 ENV 环境配置之 fw_printenv

RK3588 ENV环境配置指南 本文详细介绍了在RK3588平台上配置ENV环境变量的完整流程。首先需要在parameter分区表中划分env分区,然后在U-Boot中配置MMC偏移地址。接着编译fw_printenv工具并部署到目标设备,配置fw_env.config文件指定存储位置。重点解决了内核中读写ENV时遇到的CRC校验问题,通过U-Boot的saveenv命令初始化环境变量存储区域。最后展示了如何使用fw_printenv/fw_setenv工具在内核中读写ENV变量,并建议将配置集成到根文件

501846585_QQAndroid_java 1029

MyBatis的多对一、一对多查询

MyBatis的多对一、一对多查询

m0_58475163的博客 485

YOLOv12改进策略【独家融合改进】| MobileNetV4+BiFPN,轻松实现降参涨点,适用专栏内所有的骨干替换

可扩展的高效物体检测BiFPN(加权双向特征金字塔网络)

Limiiiing的博客 977

MyBatis_多表查询的结果封装

文章目录8.1. 多对一查询8.1.1. 确定查询需求8.1.2. 多对一环境8.1.3. 使用resultMap接收1. mapper接口方法2. 编写SQL片段3. 编写resultMap4. 测试总结8.1.4. 使用嵌套select查询1. 编写resultMap+嵌套select2. 编写statement3. 编写Mapper接口方法4. 测试总结8.1.5. 使用VO类接收1. 新增VO类2. 修改statement的resultType3. mapper接口方法4. 测试8.2. 一对多查询

qq_44509920的博客 5622

SAP-MM-采购过账接口(与WMS集成)-代码

1、判断传入值非空检验; 2、判断过账日期是否允许在当前期间; 3、判断是否为集采订单业务,是否审批完成; 4、判断是否发布标准成本发布(此业务场景与价格控制3+S有关) 5、保存日志表; 6、判断业务公司订单审批状态; 7、创建STO交货单、集采公司订单过账、STO公司间创建交货单、交货单过账(两步法)、过账失败回滚(冲销交货、删除交货单、冲销集采入库)

qq_32198063的博客 419

在MATLAB中将C语言代码转化为可执行的mexw64文件

这样,您可以将高效的C语言算法集成到MATLAB环境中,以便更好地利用MATLAB的功能和特性。请记住,在使用mex转换C语言代码之前,您需要正确编写和测试C语言代码,并确保它与MATLAB的mex接口兼容。MATLAB是一种功能强大的数值计算软件,它提供了一个机制,可以将C语言代码转化为MATLAB可执行的mexw64文件。在上述命令中,"-output"选项指定生成的mex文件的名称,"myfunction"是您选择的名称,"myfunction.c"是要编译的C语言源文件。步骤1:编写C语言代码。

CodeRoarX的博客 1426

Mybatis—多对一,一对多

多对一 要求:查询学生和老师的相关信息 Suudent类的参数 private Integer id; private String name; //学生需要关联一个老师 private Teacher teacher; 按照结果嵌套查询 <select id="getStudent" resultMap="StudentTeacher2"> select s.id sid,s.name sname,t.name tname from Student s,Tea

拧发条鸟的博客 222

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Mybatis多对一一对多查询

一、环境搭建 1.导入lombok <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://mav

深漂小码哥 1009

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

day12_复杂查询

一、复杂查询环境搭建 ​ mybatis-config.xml文件配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <setting

qq_51611787的博客 205

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

sql语句的自定义映射规则(一对多,多对一)

表与表 一对多或者多对一 关联查询时 默认的映射规则不满足,就自定义映射规则。 规律:外键在哪儿,多方就在哪儿。 表t_department 表t_employee 两个表的关联 t_department.manager_id = t_employee.id 准备2个表的domain /** * 部门实体对象 */ @Data public class Department { private Long id; //部门编号 private String sn; //

csdn_shilin的博客 1818

Mybatis框架(8) —— MyBatis 的对象关系映射

Mybatis框架MyBatis 的对象关系映射准备多表,表之间有外键关系(员工表和部门表)多对一查询一对多查询N+1方式映射代码运行结果等值连接查询映射代码运行结果 MyBatis 的对象关系映射 在实际开发中,一个业务可能涉及到多个数据表的查询,那么多表查询就涉及连接查询(等值查询),等值查询 表与表之间有一个外键关联 但是程序中最终获取的表封装的对象,对象与对象之间是没有外键关系的,对象和对...

NotaChtholly的博客 558

Mybatis一对多查询的两种姿势,你值得拥有(收藏就完事了)

文章目录数据库表准备实例演示方法一:联合查询ResultMap映射方法二:子查询映射总结 前言 最近碰到了Mybatis一对多查询的场景,在这里总结对比下常见的两种实现方式。 本文以常见的订单表和订单详情表来举例说明; 数据库表准备 订单表 tbl_order 订单详情表 tlb_order_detail ps: 一个订单关联多个订单详情,通过order_no订单号关联;   实例演示 方法一:联合查询ResultMap映射 sql直接关联查询,然后结果集通过resultMap的c

臭小子的博客 3万+

MyBatis高级查询:一对多查询详解

一对多查询,也称为“主从查询”或“嵌套查询”,是指在一个数据库查询中,一个主表记录可以关联多个从表记录的情况。在 MyBatis 中,一对多查询通常通过 resultMap 和 association/collection 标签来实现。resultMap:resultMap 是 MyBatis 中用于定义如何从数据库结果集映射到 Java 对象的一个高级特性。它允许你完全控制如何将结果集中的列映射到对象的属性上。:用于处理一对一的关联关系。collection:用于处理一对多的关联关系。

Chujun123528的博客 2403

Mybatis 多对一和一对多查询

文章目录Mybatis 多对一 and 一对多查询详解数据库需求Mybatis代码注意 Mybatis 多对一 and 一对多查询详解 数据库 员工表 t_emp 部门表 t_dept CREATE TABLE `t_emp` ( `emp_id` int NOT NULL AUTO_INCREMENT, `emp_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, `age` int DEF

王轩的博客 714

Mybatis多对一查询的配置及两种方法的使用示例对比以及Mybatis一对多查询两种方法使用示例及对比

同多对一的查询一样,mapper.xml配置中使用子查询嵌套的方法在SQL执行的时候会进行多次查询,最终查询次数和取得的行数成正比增加。City(id=0, cityname=北京, areaList=[Area(id=1, cityid=1, areaname=海淀区), Area(id=35, cityid=1, areaname=东城区), Area(id=18, cityid=1, areaname=西城区), Area(id=37, cityid=1, areaname=朝阳区)])

本博客记录了从2014年以来在工作学习中遇到的技术问题、解决方法以及部分个人人生经历、经验等内容。 1176
上一篇: 【LeetCode-简单】1. 两数之和(详解)
下一篇: 【LeetCode-简单】9.回文数(详解)
丨康有为丨
博客等级 码龄4年 2973粉丝 114原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值