笔试-2020/6/8
一、菜单树的实现
数据库表:
ID | Name | Parent_id |
M1 | 编程语言 | |
M2 | 办公工具 | |
M1_1 | 前端语言 | M1 |
M1_2 | 后端语言 | M1 |
M2_1 | Excel | M2 |
M2_2 | Word | M2 |
M1_1_1 | JS | M1_1 |
M1_2_1 | Java | M1_2 |
M1_2_2 | C++ | M1_2 |
实体类:
public class Menu { private String id; private String name; private String parentId; private List<Menu> childMenus; public Menu(String id, String name, String parentId) { this.id = id; this.name = name; this.parentId = parentId; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Menu> getChildMenus() { return childMenus; } public void setChildMenus(List<Menu> childMenus) { this.childMenus = childMenus; } @Override public String toString() { return "Menu{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", childMenus=" + childMenus + '}'; } }
获取整理好的菜单列表:
public static void main(String[] args) { List<Menu> list = new ArrayList<Menu>(); list.add(new Menu("M1", "编程语言", null)); list.add(new Menu("M2", "办公工具", null)); list.add(new Menu("M1_1", "前端语言", "M1")); list.add(new Menu("M1_2", "后端语言", "M1")); list.add(new Menu("M2_1", "Excel", "M2")); list.add(new Menu("M2_1", "Word", "M2")); list.add(new Menu("M1_1_1", "JavaScript", "M1_1")); list.add(new Menu("M1_2_1", "Java", "M1_2")); list.add(new Menu("M1_2_1", "C++", "M1_2")); ArrayList<Menu> newMenus = new ArrayList<Menu>(); //获取一级菜单 for (Menu menu:list) { if (menu.getParentId() == null) { newMenus.add(menu); menu.setChildMenus(getChild(menu.getId(), list)); } } System.out.println(newMenus); } public static List<Menu> getChild(String id, List<Menu> list) { List<Menu> childMenus = new ArrayList<Menu>(); for (Menu menu:list) { if (StringUtils.isNotBlank(menu.getParentId())) { if (menu.getParentId().equals(id)) { childMenus.add(menu); } } } for (Menu menu:childMenus) { menu.setChildMenus(getChild(menu.getId(), list)); } if (childMenus.size() == 0) { return null; } return childMenus; }
二、SQL语句类型
DDL(数据定义语言):create、drop、alter
DML(数据操作语言):select、insert、update、delete
DCL(数据控制语言):commit、rollback、revoke、grant
三、父子类加载顺序
public class Animal { static { System.out.println("父类静态代码块"); } public Animal() { System.out.println("父类无参构造器"); } public void test() { System.out.println("父类方法"); } } class Cat extends Animal { static { System.out.println("子类静态代码块"); } public Cat() { System.out.println("子类无参构造器"); } public void test() { System.out.println("子类方法"); } public static void main(String[] args) { Cat cat = new Cat(); cat.test(); } }
结果如下:
父类静态代码块
子类静态代码块
父类无参构造器
子类无参构造器
子类方法
四、单例模式实现
1、饿汉式
public class MySingleton { private static MySingleton instance = new MySingleton(); private MySingleton() { } public static MySingleton getInstance() { return instance; } }
2、懒汉式
public class MySingleton { private static MySingleton instance; private MySingleton() { } public static MySingleton getInstance() { if (instance == null) { instance = new MySingleton(); } return instance; } }
五、GROUP BY ... HAVING使用
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value;