【源码分析设计模式 6】Mybatis中的组合模式,sqlproformysql使用教程

本文详细分析了Mybatis中如何运用组合模式,通过大学、学院、专业类的示例展示了组合模式的实现,并提供了测试用例。同时,文章还探讨了HashMap的putAll方法的源码分析,以及在动态SQL中IF标签的应用,揭示了Mybatis的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package designMode.advance.composite;

public abstract class OrganizationComponent {

private String name; // 名字

private String des; // 说明

protected void add(OrganizationComponent organizationComponent) {

//默认实现

throw new UnsupportedOperationException();

}

protected void remove(OrganizationComponent organizationComponent) {

//默认实现

throw new UnsupportedOperationException();

}

//构造器

public OrganizationComponent(String name, String des) {

super();

this.name = name;

this.des = des;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDes() {

return des;

}

public void setDes(String des) {

this.des = des;

}

//方法print, 做成抽象的, 子类都需要实现

protected abstract void print();

}

2、容器构件 --> 学校类

package designMode.advance.composite;

import java.util.ArrayList;

import java.util.List;

public class University extends OrganizationComponent {

List organizationComponents = new ArrayList();

// 构造器

public University(String name, String des) {

super(name, des);

}

// 重写add

@Override

protected void add(OrganizationComponent organizationComponent) {

organizationComponents.add(organizationComponent);

}

// 重写remove

@Override

protected void remove(OrganizationComponent organizationComponent) {

organizationComponents.remove(organizationComponent);

}

@Override

public String getName() {

return super.getName();

}

@Override

public String getDes() {

return super.getDes();

}

// print方法,就是输出University 包含的学院

@Override

protected void print() {

System.out.println("--------------" + getName() + “--------------”);

//遍历 organizationComponents

for (OrganizationComponent organizationComponent : organizationComponents) {

organizationComponent.print();

}

}

}

3、容器构件 --> 学院类

package designMode.advance.composite;

import java.util.ArrayList;

import java.util.List;

public class College extends OrganizationComponent {

//List 中 存放的Department

List organizationComponents = new ArrayList();

// 构造器

public College(String name, String des) {

super(name, des);

}

// 重写add

@Override

protected void add(OrganizationComponent organizationComponent) {

// 将来实际业务中,Colleage 的 add 和 University add 不一定完全一样

organizationComponents.add(organizationComponent);

}

// 重写remove

@Override

protected void remove(OrganizationComponent organizationComponent) {

organizationComponents.remove(organizationComponent);

}

@Override

public String getName() {

return super.getName();

}

@Override

public String getDes() {

return super.getDes();

}

// print方法,就是输出University 包含的学院

@Override

protected void print() {

System.out.println("--------------" + getName() + “--------------”);

//遍历 organizationComponents

for (OrganizationComponent organizationComponent : organizationComponents) {

organizationComponent.print();

}

}

}

4、叶子节点 --> 专业类

package designMode.advance.composite;

public class Department extends OrganizationComponent {

//没有集合

public Department(String name, String des) {

super(name, des);

}

//add , remove 就不用写了,因为他是叶子节点

@Override

public String getName() {

return super.getName();

}

@Override

public String getDes() {

return super.getDes();

}

@Override

protected void print() {

System.out.println(getName());

}

}

5、测试类

package designMode.advance.composite;

public class Client {

public static void main(String[] args) {

//从大到小创建对象 学校

OrganizationComponent university = new University(“清华大学”, " 中国顶级大学 ");

//创建 学院

OrganizationComponent computerCollege = new College(“计算机学院”, " 计算机学院 ");

OrganizationComponent infoEngineercollege = new College(“信息工程学院”, " 信息工程学院 ");

//创建各个学院下面的系(专业)

computerCollege.add(new Department(“软件工程”, " 软件工程不错 "));

computerCollege.add(new Department(“网络工程”, " 网络工程不错 "));

computerCollege.add(new Department(“计算机科学与技术”, " 计算机科学与技术是老牌的专业 "));

//

infoEngineercollege.add(new Department(“通信工程”, " 通信工程不好学 "));

infoEngineercollege.add(new Department(“信息工程”, " 信息工程好学 "));

//将学院加入到 学校

university.add(computerCollege);

university.add(infoEngineercollege);

university.print();

//infoEngineercollege.print();

}

}


《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享


六、Java集合类HashMap 源码分析


HashMap 提供了putAll方法,可以将另一个Map对象放入自己的存储空间中,如有相同的key则会覆盖之前的key值所对应的value值。

1、代码实例

package designMode.advance.composite;

import java.util.HashMap;

import java.util.Map;

public class HashMapComposite {

public static void main(String[] args) {

Map<Integer,String> universityMap = new HashMap<>();

universityMap.put(1,“清华大学”);

universityMap.put(2,“北京大学”);

universityMap.put(3,“辽宁石油化工大学”);

System.out.println("universityMap: " + universityMap);

Map<Integer,String> collegeMap = new HashMap<>();

collegeMap.put(1,“计算机学院”);

collegeMap.put(4,“信息工程学院”);

System.out.println("collegeMap: " + collegeMap);

universityMap.putAll(collegeMap);

System.out.println(“universityMap.putAll(collegeMap),”+universityMap);

}

}

2、控制台输出

3、源码分析

putAll接收的参数为父类Map类型,所以hashmap是一个容器类,map的子类为叶子类,当然如果map的其它子类也实现了putAll方法,那么他们既是容器类,又都是叶子类;

同理,ArrayList 中的 addAll(Collection<? extends E> c) 方法也是一个组合模式的应用;

七、Mybatis SqlNode中的组合模式


MyBatis 的强大特性之一便是它的动态SQL,其通过 if, choose, when, otherwise, trim, where, set, foreach 标签,可组合成非常灵活的SQL语句,从而提高开发人员的效率。

动态SQL – IF

SELECT * FROM BLOG WHERE state = ‘ACTIVE’

AND title like #{title}

AND author_name like #{author.name}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值