guyanqing 7 月之前
父節點
當前提交
1a37e3d147

+ 1 - 1
src/main/java/com/sf/day01/day02/TE.java → src/main/java/com/sf/day02/TE.java

@@ -1,4 +1,4 @@
-package com.sf.day01.day02;
+package com.sf.day02;
 
 import org.junit.Test;
 

+ 14 - 0
src/main/java/com/sf/quanrizhi/Column.java

@@ -0,0 +1,14 @@
+package com.sf.quanrizhi;
+
+import jdk.internal.org.objectweb.asm.tree.analysis.Value;
+
+import java.lang.annotation.*;
+import java.util.Vector;
+
+@Inherited
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Column {
+    String columnName();
+    String columnType();
+}

+ 15 - 0
src/main/java/com/sf/quanrizhi/Data.java

@@ -0,0 +1,15 @@
+package com.sf.quanrizhi;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Data {
+    public static  List<Product> products = new ArrayList<>();
+    static {
+        products.add(new Product(1L, "苹果手机", 8888.88,"手机"));
+        products.add(new Product(2L, "华为手机", 6666.66,"手机"));
+        products.add(new Product(3L, "联想笔记本",7777.77,"电脑"));
+        products.add(new Product(4L, "机械键盘", 999.99,"键盘"));
+        products.add(new Product(5L, "雷蛇鼠标", 222.22,"鼠标"));
+    }
+}

+ 5 - 0
src/main/java/com/sf/quanrizhi/MyPredicate.java

@@ -0,0 +1,5 @@
+package com.sf.quanrizhi;
+@FunctionalInterface
+public interface MyPredicate {
+    boolean test(Product product);
+}

+ 60 - 0
src/main/java/com/sf/quanrizhi/Product.java

@@ -0,0 +1,60 @@
+package com.sf.quanrizhi;
+
+public class Product {
+    private Long    id;         // 序号
+    private String  name;       // 商品名称
+    private Double 	price;      // 价格
+    private String  type;       // 类型
+
+    public Product() {
+    }
+
+    public Product(Long id, String name, Double price, String type) {
+        this.id = id;
+        this.name = name;
+        this.price = price;
+        this.type = type;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Double getPrice() {
+        return price;
+    }
+
+    public void setPrice(Double price) {
+        this.price = price;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    @Override
+    public String toString() {
+        return "Product{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                ", price=" + price +
+                ", type='" + type + '\'' +
+                '}';
+    }
+}

+ 44 - 0
src/main/java/com/sf/quanrizhi/Student1.java

@@ -0,0 +1,44 @@
+package com.sf.quanrizhi;
+
+/**
+ * SELECT   id , name  FROM  Student
+ */
+@Table(value = "student")
+public class Student1 {
+    @Column(columnName = "sid",columnType = "int")
+    private int id;
+    @Column(columnName = "sname",columnType ="varchar(25)" )
+    private String name;
+
+    public Student1() {
+    }
+
+    public Student1(int id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+
+    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;
+    }
+
+    @Override
+    public String toString() {
+        return "Student1{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                '}';
+    }
+}

+ 151 - 0
src/main/java/com/sf/quanrizhi/T.java

@@ -1,6 +1,7 @@
 package com.sf.quanrizhi;
 
 import org.junit.Test;
+import sun.java2d.pipe.AAShapePipe;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -9,7 +10,9 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 import java.util.Properties;
 
 public class T {
@@ -232,4 +235,152 @@ public class T {
         Object invoke = method.invoke(o);
         System.out.println(invoke);
     }
+
+    @Test
+    public void t10(){
+        Class<Student1> student1Class = Student1.class;
+        Table annotation = student1Class.getAnnotation(Table.class);
+        String tableName = "";
+        if(annotation !=null){
+             tableName = annotation.value();
+        }
+        Field[] declaredFields = student1Class.getDeclaredFields();
+        String[] columns= new String[declaredFields.length];
+        int index = 0;
+        for (Field declaredField : declaredFields) {
+            Column annotation1 = declaredField.getAnnotation(Column.class);
+            if(annotation1 != null){
+                columns[index++]= annotation1.columnName();
+            }
+        }
+        String sql  = "SELECT ";
+        for (int i = 0;i<index;i++){
+            sql += columns[i];
+            if(i<index-1){
+                sql+=",";
+            }
+        }
+        sql += "   FROM "+tableName;
+        System.out.println(sql);
+    }
+
+    /**
+     * 通过反射
+     * 像List<String> 集合中添加整型int
+     *
+     */
+    @Test
+    public void t11() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
+        //创建list集合
+        List<String>  list = new ArrayList<>();
+        //应该获取Arraylist 的class对象
+        Class<? extends List> listClass = list.getClass();
+        Method add = listClass.getMethod("add", Object.class);
+        add.invoke(list,1);
+        add.invoke(list,"hello");
+        add.invoke(list,true);
+        System.out.println(list);
+    }
+
+
+    @Test
+    public void t12(){
+        //启动一个线程 并且输出一句话
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                System.out.println("开启一个新的线程!!");
+            }
+        }).start();
+
+        new Thread(() -> System.out.println("开启一个新的线程!!")).start();
+    }
+
+    /**
+     * 筛选出所有名称包含手机的商品
+     */
+    @Test
+    public void t13(){
+        List<Product> productsWithName = findProductsWithName(Data.products);
+        System.out.println(productsWithName);
+
+        //  筛选出价格大于1000的商品
+        List<Product> productsWithPrice = findProductsWithPrice(Data.products);
+        System.out.println(productsWithPrice);
+    }
+
+    public static List<Product> findProductsWithName(List<Product> products){
+        List<Product> list = new ArrayList<>();
+        for (Product p : products) {
+            if(p.getName().contains("手机")){
+                list.add(p);
+            }
+        }
+        return list;
+    }
+
+    public static List<Product> findProductsWithPrice(List<Product> products){
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(product.getPrice() > 1000){
+                list.add(product);
+            }
+        }
+        return list;
+    }
+
+
+    @Test
+    public void t14(){
+    // 筛选出名称种包含手机的商品
+        List<Product> productsWithCondition = findProductsWithCondition(Data.products, new MyPredicate() {
+            @Override
+            public boolean test(Product product) {
+                return  product.getName().contains("手机");
+            }
+        });
+        System.out.println(productsWithCondition);
+
+        List<Product> productsWithCondition1 = findProductsWithCondition(Data.products, new MyPredicate() {
+            @Override
+            public boolean test(Product product) {
+                return product.getPrice() > 1000;
+            }
+        });
+        System.out.println(productsWithCondition1);
+
+
+    }
+
+    public static List<Product> findProductsWithCondition(List<Product> products ,MyPredicate condition){
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(condition.test(product)){
+                list.add(product);
+            }
+        }
+        return list;
+    }
+
+
+    @Test
+    public void t16(){
+        findPhoneByCondition(Data.products,(product ->
+             product.getName().contains("手机")
+        ));
+
+        // 价格
+        findPhoneByCondition(Data.products,(product -> product.getPrice()>1000));
+    }
+
+    public static List<Product> findPhoneByCondition(List<Product> products,MyPredicate myPredicate){
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(myPredicate.test(product)){
+                list.add(product);
+            }
+        }
+        return list;
+    }
+
 }

+ 14 - 0
src/main/java/com/sf/quanrizhi/Table.java

@@ -0,0 +1,14 @@
+package com.sf.quanrizhi;
+
+import java.lang.annotation.*;
+
+@Inherited     //子类可以被继承
+@Target(ElementType.TYPE)   //定义在类上
+@Retention(RetentionPolicy.RUNTIME)  //生命周期   运行时
+public @interface Table {
+    String value();
+    /**
+     * 为什么要这么定义
+     * 背景:我们通过注解写一个sql语句  select 字段...  from  user
+     */
+}

二進制
target/classes/com/sf/day01/day02/TE.class


二進制
target/classes/com/sf/day02/TE.class


二進制
target/classes/com/sf/quanrizhi/Column.class


二進制
target/classes/com/sf/quanrizhi/Data.class


二進制
target/classes/com/sf/quanrizhi/MyPredicate.class


二進制
target/classes/com/sf/quanrizhi/Product.class


二進制
target/classes/com/sf/quanrizhi/Student1.class


二進制
target/classes/com/sf/quanrizhi/T$1.class


二進制
target/classes/com/sf/quanrizhi/T$2.class


二進制
target/classes/com/sf/quanrizhi/T$3.class


二進制
target/classes/com/sf/quanrizhi/T.class


二進制
target/classes/com/sf/quanrizhi/Table.class