xerga 2 anni fa
parent
commit
b53411e290

+ 1 - 0
02_MySql/.idea/modules.xml

@@ -4,6 +4,7 @@
     <modules>
       <module fileurl="file://$PROJECT_DIR$/02_MySql.iml" filepath="$PROJECT_DIR$/02_MySql.iml" />
       <module fileurl="file://$PROJECT_DIR$/jdbc-day01/jdbc-day01.iml" filepath="$PROJECT_DIR$/jdbc-day01/jdbc-day01.iml" />
+      <module fileurl="file://$PROJECT_DIR$/jdbc-day02/jdbc-day02.iml" filepath="$PROJECT_DIR$/jdbc-day02/jdbc-day02.iml" />
     </modules>
   </component>
 </project>

+ 124 - 0
02_MySql/.idea/uiDesigner.xml

@@ -0,0 +1,124 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Palette2">
+    <group name="Swing">
+      <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
+      </item>
+      <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
+      </item>
+      <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
+        <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
+        <initial-values>
+          <property name="text" value="Button" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="RadioButton" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="CheckBox" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="Label" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
+          <preferred-size width="200" height="200" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
+          <preferred-size width="200" height="200" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
+      </item>
+      <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
+          <preferred-size width="-1" height="20" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
+      </item>
+      <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
+      </item>
+    </group>
+  </component>
+</project>

+ 22 - 0
02_MySql/jdbc-day02/jdbc-day02.iml

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module-library">
+      <library name="JUnit4">
+        <CLASSES>
+          <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
+          <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+    <orderEntry type="library" name="libs" level="project" />
+  </component>
+</module>

+ 11 - 0
02_MySql/jdbc-day02/src/Test01.java

@@ -0,0 +1,11 @@
+import org.junit.Test;
+
+public class Test01 {
+
+
+    @Test
+    public void test01(){
+
+    }
+
+}

+ 56 - 0
02_MySql/jdbc-day02/src/com/lovecoding/bean/Department.java

@@ -0,0 +1,56 @@
+package com.lovecoding.bean;
+
+/**
+ * ClassName: Department
+ * Package: com.lovecoding.bean
+ * Description:
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/4/12 18:37
+ * @Version 1.0
+ *
+ *
+ * Department = 表名称 t_department 去掉表前缀
+ */
+public class Department {
+    //字段 = 属性
+    private Integer did; //int 类型 不能 = null
+    private String dname;
+    private String description;
+
+    public Department() {
+    }
+
+    public Integer getDid() {
+        return did;
+    }
+
+    public void setDid(Integer did) {
+        this.did = did;
+    }
+
+    public String getDname() {
+        return dname;
+    }
+
+    public void setDname(String dname) {
+        this.dname = dname;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    @Override
+    public String toString() {
+        return "Department{" +
+                "did=" + did +
+                ", dname='" + dname + '\'' +
+                ", description='" + description + '\'' +
+                '}';
+    }
+}

+ 36 - 0
02_MySql/jdbc-day02/src/com/lovecoding/dao/DepartmentDao.java

@@ -0,0 +1,36 @@
+package com.lovecoding.dao;
+
+import com.lovecoding.bean.Department;
+
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ * ClassName: DepartmentDao
+ * Package: com.lovecoding.dao
+ * Description:
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/4/12 18:42
+ * @Version 1.0
+ */
+public interface DepartmentDao {
+
+    //接口中定义方法 5个方法
+    //添加部门信息
+    int addDepartment(Department department) throws SQLException;
+
+    //修改方法 id
+    int updateDepartment(Department department) throws SQLException;
+
+    //删除方法
+    int deleteDepartmentById(Integer did) throws SQLException;
+
+    //查询方法 查询单个
+    Department getDepartmentById(Integer did) throws SQLException;
+
+    //查询方法 查询多个
+    List<Department> getDepartmentList() throws SQLException;
+
+
+}

+ 135 - 0
02_MySql/jdbc-day02/src/com/lovecoding/dao/impl/BaseDAOImpl.java

@@ -0,0 +1,135 @@
+package com.lovecoding.dao.impl;
+
+import com.lovecoding.utils.JDBCTools;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ClassName: BaseDAOImpl
+ * Package: com.lovecoding.dao.impl
+ * Description: 操作(增、删、改、查)代码重复度很高,所以可以抽取公共代码,给这些DAO的实现类可以抽取一个公共的父类,我们称为BaseDAOImpl.
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/4/12 19:28
+ * @Version 1.0
+ */
+public abstract class BaseDAOImpl {
+
+    //五个方法
+    //三个 update
+
+    /**
+     * 增删改通用方法
+     * @param sql sql语句
+     * @param params 参数
+     * @return
+     */
+    public int update( String sql, Object ... params ) throws SQLException {
+        // sql  ??
+        //获取链接
+        Connection connection = JDBCTools.getConnection();
+        //sql执行对象
+        PreparedStatement pst = connection.prepareStatement(sql);
+        //设置参数
+        for (int i = 1; i <= params.length; i++) {
+            //索引从1开始
+            //params数组索引从0开始
+            pst.setObject(i,params[i-1]);
+        }
+        //执行sql
+        return pst.executeUpdate();
+    }
+
+
+    //两个 query
+
+    /**
+     * 查询单个的方法
+     * @param clazz 封装的对象类型
+     * @param sql
+     * @param params
+     * @param <T>
+     * @return
+     */
+    public <T> T getBean(Class<T> clazz, String sql, Object ... params) throws SQLException {
+        List<T> list = getList(clazz, sql, params);
+
+        if (list != null && list.size() > 0 ){
+            return list.get(0);
+        }
+        return null;
+    }
+
+    /**
+     * 查询多个的方法
+     * @param clazz
+     * @param sql
+     * @param params
+     * @param <T>
+     * @return
+     */
+    public  <T> List<T> getList(Class<T> clazz, String sql, Object[] params) throws SQLException {
+        //返回对象
+        ArrayList<T> list = new ArrayList<>();
+        //获取链接
+        Connection connection = JDBCTools.getConnection();
+        //sql执行对象
+        PreparedStatement pst = connection.prepareStatement(sql);
+        //设置参数
+        if (params != null && params.length > 0){
+            for (int i = 1; i <= params.length; i++) {
+                //索引从1开始
+                //params数组索引从0开始
+                pst.setObject(i,params[i-1]);
+            }
+        }
+
+        //执行查询方法
+        ResultSet rs = pst.executeQuery();
+        //根据反射
+
+        //metaData 表头的信息
+        ResultSetMetaData metaData = rs.getMetaData();
+
+        //遍历
+        while (rs.next()){
+            //反射创建对象
+            try {
+                T t = clazz.newInstance();
+                //列的数量
+                int columnCount = metaData.getColumnCount();
+
+                for (int i = 1; i <= columnCount; i++) {
+                    //设置属性值
+                    //属性名称 值
+                    Object val = rs.getObject(i);
+                    //getColumnLabel 别名的中
+                    //getColumnName  列名 = 属性名
+                    String columnLabel = metaData.getColumnLabel(i);
+                    //注入值
+                    Field declaredField = clazz.getDeclaredField(columnLabel);
+                    //破解
+                    declaredField.setAccessible(true);
+                    //值
+                    declaredField.set(t,val);
+
+                }
+                //添加到list集合
+                list.add(t);
+            } catch (InstantiationException e) {
+                e.printStackTrace();
+            } catch (IllegalAccessException e) {
+                e.printStackTrace();
+            } catch (NoSuchFieldException e) {
+                e.printStackTrace();
+            }
+        }
+
+        return list;
+    }
+
+}

+ 52 - 0
02_MySql/jdbc-day02/src/com/lovecoding/dao/impl/DepartmentDaoImpl.java

@@ -0,0 +1,52 @@
+package com.lovecoding.dao.impl;
+
+import com.lovecoding.bean.Department;
+import com.lovecoding.dao.DepartmentDao;
+
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ * ClassName: DepartmentDaoImpl
+ * Package: com.lovecoding.dao.impl
+ * Description:
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/4/12 20:00
+ * @Version 1.0
+ */
+public class DepartmentDaoImpl  extends BaseDAOImpl  implements DepartmentDao {
+
+
+    @Override
+    public int addDepartment(Department department) throws SQLException {
+        String sql = " insert into t_department values(null,?,?); ";
+        return update(sql,department.getDname(),department.getDescription());
+    }
+
+    @Override
+    public int updateDepartment(Department department) throws SQLException {
+        String sql = " update t_department set dname=?,description=? where did=?; ";
+        return update(sql,department.getDname(),department.getDescription(),department.getDid());
+    }
+
+    @Override
+    public int deleteDepartmentById(Integer did) throws SQLException {
+        String sql = " delete from t_department where did = ?; ";
+        return update(sql,did);
+    }
+
+    @Override
+    public Department getDepartmentById(Integer did) throws SQLException {
+
+        String sql = " select * from t_department where did = ?; ";
+
+        return getBean(Department.class,sql,did);
+    }
+
+    @Override
+    public List<Department> getDepartmentList() throws SQLException {
+        String sql = " select * from t_department  ";
+        return getList(Department.class,sql,null);
+    }
+}

+ 73 - 0
02_MySql/jdbc-day02/src/com/lovecoding/test/TestDepartmentDao.java

@@ -0,0 +1,73 @@
+package com.lovecoding.test;
+
+import com.lovecoding.bean.Department;
+import com.lovecoding.dao.impl.DepartmentDaoImpl;
+import org.junit.Test;
+
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ * ClassName: TestDepartmentDao
+ * Package: com.lovecoding.test
+ * Description:
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/4/12 20:08
+ * @Version 1.0
+ */
+public class TestDepartmentDao {
+
+    @Test
+    public void test01() throws SQLException {
+        DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();
+
+        Department department = new Department();
+        department.setDname("dev");
+        department.setDescription("dev desc");
+        int i = departmentDao.addDepartment(department);
+        System.out.println(i);
+    }
+
+    @Test
+    public void test02() throws SQLException {
+        DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();
+
+        Department department = new Department();
+        department.setDid(2007);
+        department.setDname("test");
+        department.setDescription("test desc");
+        //修改
+        int i = departmentDao.updateDepartment(department);
+        System.out.println(i);
+    }
+
+    @Test
+    public void test03() throws SQLException {
+        DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();
+
+        //删除
+        int i = departmentDao.deleteDepartmentById(2007);
+        System.out.println(i);
+    }
+
+
+    @Test
+    public void test04() throws SQLException {
+        DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();
+
+        //查询
+        Department department = departmentDao.getDepartmentById(2008);
+        System.out.println(department);
+    }
+
+    @Test
+    public void test05() throws SQLException {
+        DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();
+
+        //查询所有
+        List<Department> departmentList = departmentDao.getDepartmentList();
+        departmentList.forEach(System.out::println);
+    }
+
+}

+ 67 - 0
02_MySql/jdbc-day02/src/com/lovecoding/utils/JDBCTools.java

@@ -0,0 +1,67 @@
+package com.lovecoding.utils;
+
+import com.alibaba.druid.pool.DruidDataSourceFactory;
+import org.junit.Test;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Properties;
+
+/**
+ * ClassName: JDBCTools
+ * Package: com.lovecoding.utils
+ * Description: //通过工具获取链接 释放
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2023/4/12 18:50
+ * @Version 1.0
+ */
+public class JDBCTools {
+
+    //数据源
+    private static DataSource dataSource = null;
+
+    //静态代码
+    static {
+        try {
+            //druid 配置文件 Properties
+            Properties properties = new Properties();
+            //类加载器
+            InputStream file = JDBCTools.class.getClassLoader().getResourceAsStream("druid.properties");
+            //读取
+            properties.load(file);
+            //德鲁伊
+            dataSource = DruidDataSourceFactory.createDataSource(properties);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 获取方法
+     * @return
+     * @throws SQLException
+     */
+    public static Connection getConnection() throws SQLException {
+        return  dataSource.getConnection();
+    }
+
+
+    /**
+     * 释放方法
+     * @return
+     * @throws SQLException
+     */
+    public static void free(Connection connection  ) throws SQLException {
+        if (connection != null){
+            //改为自动提交
+            connection.setAutoCommit(true);
+            connection.close();
+        }
+    }
+
+}

+ 8 - 0
02_MySql/jdbc-day02/src/druid.properties

@@ -0,0 +1,8 @@
+#key=value
+driverClassName=com.mysql.jdbc.Driver
+url=jdbc:mysql://localhost:3306/lovecoding?serverTimezone=UTC&rewriteBatchedStatements=true
+username=root
+password=root
+initialSize=5
+maxActive=10
+maxWait=1000

BIN
02_MySql/out/production/jdbc-day02/Test01.class


BIN
02_MySql/out/production/jdbc-day02/com/lovecoding/bean/Department.class


BIN
02_MySql/out/production/jdbc-day02/com/lovecoding/dao/DepartmentDao.class


BIN
02_MySql/out/production/jdbc-day02/com/lovecoding/dao/impl/BaseDAOImpl.class


BIN
02_MySql/out/production/jdbc-day02/com/lovecoding/dao/impl/DepartmentDaoImpl.class


BIN
02_MySql/out/production/jdbc-day02/com/lovecoding/test/TestDepartmentDao.class


BIN
02_MySql/out/production/jdbc-day02/com/lovecoding/utils/JDBCTools.class


+ 8 - 0
02_MySql/out/production/jdbc-day02/druid.properties

@@ -0,0 +1,8 @@
+#key=value
+driverClassName=com.mysql.jdbc.Driver
+url=jdbc:mysql://localhost:3306/lovecoding?serverTimezone=UTC&rewriteBatchedStatements=true
+username=root
+password=root
+initialSize=5
+maxActive=10
+maxWait=1000