diff --git a/myCompanyDb/.gitignore b/myCompanyDb/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/myCompanyDb/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/myCompanyDb/.idea/.gitignore b/myCompanyDb/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/myCompanyDb/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/myCompanyDb/.idea/compiler.xml b/myCompanyDb/.idea/compiler.xml new file mode 100644 index 0000000..a1757ae --- /dev/null +++ b/myCompanyDb/.idea/compiler.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/myCompanyDb/.idea/dataSources.xml b/myCompanyDb/.idea/dataSources.xml new file mode 100644 index 0000000..7c5456b --- /dev/null +++ b/myCompanyDb/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/myCompanyDb/.idea/kotlinc.xml b/myCompanyDb/.idea/kotlinc.xml new file mode 100644 index 0000000..5c6661c --- /dev/null +++ b/myCompanyDb/.idea/kotlinc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/myCompanyDb/.idea/libraries/lib.xml b/myCompanyDb/.idea/libraries/lib.xml new file mode 100644 index 0000000..fa8838a --- /dev/null +++ b/myCompanyDb/.idea/libraries/lib.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/myCompanyDb/.idea/misc.xml b/myCompanyDb/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/myCompanyDb/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/myCompanyDb/.idea/modules.xml b/myCompanyDb/.idea/modules.xml new file mode 100644 index 0000000..f839a29 --- /dev/null +++ b/myCompanyDb/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/myCompanyDb/.idea/uiDesigner.xml b/myCompanyDb/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/myCompanyDb/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/myCompanyDb/.idea/vcs.xml b/myCompanyDb/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/myCompanyDb/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/myCompanyDb/lib/druid-1.1.21.jar b/myCompanyDb/lib/druid-1.1.21.jar new file mode 100644 index 0000000..605b7f9 Binary files /dev/null and b/myCompanyDb/lib/druid-1.1.21.jar differ diff --git a/myCompanyDb/lib/lombok-1.18.24.jar b/myCompanyDb/lib/lombok-1.18.24.jar new file mode 100644 index 0000000..3a985fe Binary files /dev/null and b/myCompanyDb/lib/lombok-1.18.24.jar differ diff --git a/myCompanyDb/lib/mysql-connector-j-8.0.31.jar b/myCompanyDb/lib/mysql-connector-j-8.0.31.jar new file mode 100644 index 0000000..8b74bb8 Binary files /dev/null and b/myCompanyDb/lib/mysql-connector-j-8.0.31.jar differ diff --git a/myCompanyDb/myCompanyDB.iml b/myCompanyDb/myCompanyDB.iml new file mode 100644 index 0000000..0dfd584 --- /dev/null +++ b/myCompanyDb/myCompanyDB.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/myCompanyDb/resources/jdbc.properties b/myCompanyDb/resources/jdbc.properties new file mode 100644 index 0000000..3347d08 --- /dev/null +++ b/myCompanyDb/resources/jdbc.properties @@ -0,0 +1,7 @@ +driverClassName=com.mysql.cj.jdbc.Driver +url=jdbc:mysql://localhost:3306/my_company_db +username=root +password=guo123456 +initialSize=5 +maxActive=10 +maxWait=1000 \ No newline at end of file diff --git a/myCompanyDb/src/com/guo/db/Dao/BaseDao.java b/myCompanyDb/src/com/guo/db/Dao/BaseDao.java new file mode 100644 index 0000000..69be1b1 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/Dao/BaseDao.java @@ -0,0 +1,148 @@ +package com.guo.db.Dao; + + +import com.guo.db.util.JDBCUtil; + +import java.lang.reflect.Field; +import java.sql.*; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@SuppressWarnings("all") +public class BaseDao { + // 公共的查询方法 返回的是单个对象 + public T baseQueryObject(Class clazz, String sql, Object ... args) { + T t = null; + Connection connection = JDBCUtil.getConnection(); + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + int rows = 0; + try { + // 准备语句对象 + preparedStatement = connection.prepareStatement(sql); + // 设置语句上的参数 + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i + 1, args[i]); + } + // 执行 查询 + resultSet = preparedStatement.executeQuery(); + if (resultSet.next()) { + t = (T) resultSet.getObject(1); + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + if (null != resultSet) { + try { + resultSet.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + if (null != preparedStatement) { + try { + preparedStatement.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + JDBCUtil.releaseConnection(); + } + return t; + } + // 公共的查询方法 返回的是对象的集合 + public List baseQuery(Class clazz, String sql, Object ... args){ + List list =new ArrayList<>(); + Connection connection = JDBCUtil.getConnection(); + PreparedStatement preparedStatement=null; + ResultSet resultSet =null; + int rows = 0; + try { + // 准备语句对象 + preparedStatement = connection.prepareStatement(sql); + // 设置语句上的参数 + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i+1,args[i]); + } + // 执行 查询 + resultSet = preparedStatement.executeQuery(); + + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + // 将结果集通过反射封装成实体类对象 + while (resultSet.next()) { + // 使用反射实例化对象 + Object obj = clazz.getDeclaredConstructor().newInstance(); + + for (int i = 1; i <= columnCount; i++) { + String columnName = metaData.getColumnLabel(i); + Object value = resultSet.getObject(columnName); + // 处理datetime类型字段和java.util.Data转换问题 + if(value.getClass().equals(LocalDateTime.class)){ + value= Timestamp.valueOf((LocalDateTime) value); + } + Field field = clazz.getDeclaredField(columnName); + field.setAccessible(true); + field.set(obj,value); + } + list.add((T)obj); + } + + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + if (null !=resultSet) { + try { + resultSet.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + if (null != preparedStatement) { + try { + preparedStatement.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + JDBCUtil.releaseConnection(); + } + return list; + } + // 通用的增删改方法 + public int baseUpdate(String sql,Object ... args) { + // 获取连接 + Connection connection = JDBCUtil.getConnection(); + PreparedStatement preparedStatement=null; + int rows = 0; + try { + // 准备语句对象 + preparedStatement = connection.prepareStatement(sql); + // 设置语句上的参数 + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i+1,args[i]); + } + // 执行 增删改 executeUpdate + rows = preparedStatement.executeUpdate(); + // 释放资源(可选) + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + if (null != preparedStatement) { + try { + preparedStatement.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + JDBCUtil.releaseConnection(); + } + // 返回的是影响数据库记录数 + return rows; + } +} + diff --git a/myCompanyDb/src/com/guo/db/Dao/TestSql.java b/myCompanyDb/src/com/guo/db/Dao/TestSql.java new file mode 100644 index 0000000..8c629d9 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/Dao/TestSql.java @@ -0,0 +1,39 @@ +package com.guo.db.Dao; + + +import com.guo.db.pojo.Customers; +import org.junit.Test; + +import java.util.List; + +public class TestSql extends BaseDao { + @Test + public void Insert() { + String sql = "insert into customers values (DEFAULT, ?, ?)"; + int row = baseUpdate(sql, "张三", "12345678901"); + System.out.println(row); + } + + @Test + public void Update() { + String sql = "update customers set customer_name = ? where customer_id = ?"; + int row = baseUpdate(sql, "李四", "13"); + System.out.println(row); + } + + @Test + public void Delete() { + String sql = "delete from customers where customer_id = ?"; + int row = baseUpdate(sql, "13"); + System.out.println(row); + } + + @Test + public void Select() { + String sql = "select customer_id customerId, customer_name customerName, phone from customers where customer_id = ?"; + List customers = baseQuery(Customers.class, sql, "1"); + for (Customers c : customers) { + System.out.println(c.getCustomerId() + " " + c.getCustomerName() + " " + c.getPhone()); + } + } +} diff --git a/myCompanyDb/src/com/guo/db/pojo/Customers.java b/myCompanyDb/src/com/guo/db/pojo/Customers.java new file mode 100644 index 0000000..9bd7bd6 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/Customers.java @@ -0,0 +1,14 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Customers { + private int customerId; + private String customerName; + private String phone; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/Kpi.java b/myCompanyDb/src/com/guo/db/pojo/Kpi.java new file mode 100644 index 0000000..bd4407d --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/Kpi.java @@ -0,0 +1,14 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Kpi { + private int kid; + private int sid; + private int grade; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/OrderDetails.java b/myCompanyDb/src/com/guo/db/pojo/OrderDetails.java new file mode 100644 index 0000000..235e3ba --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/OrderDetails.java @@ -0,0 +1,16 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class OrderDetails { + private int orderDetailId; + private int orderId; + private int productId; + private int quantity; + private double unitPrice; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/ProductSuppliers.java b/myCompanyDb/src/com/guo/db/pojo/ProductSuppliers.java new file mode 100644 index 0000000..d93c728 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/ProductSuppliers.java @@ -0,0 +1,14 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ProductSuppliers { + private int productSupplierId; + private int productId; + private int supplierId; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/Products.java b/myCompanyDb/src/com/guo/db/pojo/Products.java new file mode 100644 index 0000000..efabd8b --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/Products.java @@ -0,0 +1,15 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Products { + private int productId; + private String productName; + private int price; + private int stockQuantity; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/Returns.java b/myCompanyDb/src/com/guo/db/pojo/Returns.java new file mode 100644 index 0000000..79ec093 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/Returns.java @@ -0,0 +1,15 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Returns { + private int returnId; + private String returnDate; + private int orderId; + private String reason; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/SalesActivities.java b/myCompanyDb/src/com/guo/db/pojo/SalesActivities.java new file mode 100644 index 0000000..9cbae05 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/SalesActivities.java @@ -0,0 +1,16 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SalesActivities { + private int activityId; + private String activityName; + private String startDate; + private String endDate; + private double totalSalesAmount; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/SalesOrders.java b/myCompanyDb/src/com/guo/db/pojo/SalesOrders.java new file mode 100644 index 0000000..44be8c7 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/SalesOrders.java @@ -0,0 +1,16 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SalesOrders { + private int orderId; + private String orderDate; + private int customerId; + private int salespersonId; + private int totalAmount; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/ShippingMethods.java b/myCompanyDb/src/com/guo/db/pojo/ShippingMethods.java new file mode 100644 index 0000000..e1188ab --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/ShippingMethods.java @@ -0,0 +1,16 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ShippingMethods { + private int shippingMethodId; + private String methodName; + private double costPerUnit; + private int estimatedDeliveryTime; + private int orderId; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/Suppliers.java b/myCompanyDb/src/com/guo/db/pojo/Suppliers.java new file mode 100644 index 0000000..2476cb3 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/Suppliers.java @@ -0,0 +1,14 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Suppliers { + private int supplierId; + private String companyName; + private String phone; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/Warehouses.java b/myCompanyDb/src/com/guo/db/pojo/Warehouses.java new file mode 100644 index 0000000..8ac9fa8 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/Warehouses.java @@ -0,0 +1,14 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Warehouses { + private int warehouseId; + private String location; + private int capacity; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/productWarehouses.java b/myCompanyDb/src/com/guo/db/pojo/productWarehouses.java new file mode 100644 index 0000000..5413d71 --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/productWarehouses.java @@ -0,0 +1,15 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class productWarehouses { + private int productWarehouseId; + private int productId; + private int warehouseId; + private int quantityInWarehouse; +} diff --git a/myCompanyDb/src/com/guo/db/pojo/salesPeople.java b/myCompanyDb/src/com/guo/db/pojo/salesPeople.java new file mode 100644 index 0000000..0844b3f --- /dev/null +++ b/myCompanyDb/src/com/guo/db/pojo/salesPeople.java @@ -0,0 +1,14 @@ +package com.guo.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class salesPeople { + private int salespersonId; + private String salespersonName; + private String phone; +} diff --git a/myCompanyDb/src/com/guo/db/util/JDBCUtil.java b/myCompanyDb/src/com/guo/db/util/JDBCUtil.java new file mode 100644 index 0000000..9a2922b --- /dev/null +++ b/myCompanyDb/src/com/guo/db/util/JDBCUtil.java @@ -0,0 +1,67 @@ +package com.guo.db.util; + + +import com.alibaba.druid.pool.DruidDataSourceFactory; + +import javax.sql.DataSource; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; +@SuppressWarnings("all") +public class JDBCUtil { + private static ThreadLocal threadLocal =new ThreadLocal<>(); + private static DataSource dataSource; + // 初始化连接池 + static{ + // 读取.properties配置文件 + Properties properties = new Properties(); + InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); + try { + properties.load(resourceAsStream); + } catch (IOException e) { + throw new RuntimeException(e); + } + + try { + dataSource = DruidDataSourceFactory.createDataSource(properties); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /*1 向外提供连接池的方法*/ + public static DataSource getDataSource(){ + return dataSource; + } + /*2 向外提供连接的方法*/ + public static Connection getConnection(){ + Connection connection = threadLocal.get(); + if (null == connection) { + try { + connection = dataSource.getConnection(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + threadLocal.set(connection); + } + + return connection; + } + /*定义一个归还连接的方法 (解除和ThreadLocal之间的关联关系) */ + public static void releaseConnection(){ + Connection connection = threadLocal.get(); + if (null != connection) { + threadLocal.remove(); + // 把连接设置回自动提交的连接 + try { + connection.setAutoCommit(true); + // 自动归还到连接池 + connection.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} +