erga před 9 měsíci
rodič
revize
a42c22410f
37 změnil soubory, kde provedl 1585 přidání a 0 odebrání
  1. 38 0
      jdbc/jdbc01/.gitignore
  2. 8 0
      jdbc/jdbc01/.idea/.gitignore
  3. 7 0
      jdbc/jdbc01/.idea/encodings.xml
  4. 14 0
      jdbc/jdbc01/.idea/misc.xml
  5. 124 0
      jdbc/jdbc01/.idea/uiDesigner.xml
  6. 38 0
      jdbc/jdbc01/pom.xml
  7. 78 0
      jdbc/jdbc01/src/main/java/com/lc/jdbc/bean/Dept.java
  8. 41 0
      jdbc/jdbc01/src/main/java/com/lc/jdbc/dao/DeptDao.java
  9. 121 0
      jdbc/jdbc01/src/main/java/com/lc/jdbc/dao/impl/DeptDaoImpl.java
  10. 18 0
      jdbc/jdbc01/src/main/java/com/lc/jdbc/dao/impl/DeptDaoImpl1.java
  11. 219 0
      jdbc/jdbc01/src/main/java/com/lc/utils/BaseDAO.java
  12. 23 0
      jdbc/jdbc01/src/main/java/com/lc/utils/Column.java
  13. 44 0
      jdbc/jdbc01/src/main/java/com/lc/utils/DButils.java
  14. 46 0
      jdbc/jdbc01/src/main/java/com/lc/utils/DruidUtils.java
  15. 14 0
      jdbc/jdbc01/src/main/java/com/lc/utils/Table.java
  16. 10 0
      jdbc/jdbc01/src/main/resources/druid.properties
  17. 6 0
      jdbc/jdbc01/src/main/resources/jdbc.properties
  18. 56 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestConnection.java
  19. 21 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDBUtils.java
  20. 91 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDept.java
  21. 21 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDept1.java
  22. 33 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDruid.java
  23. 56 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestRS.java
  24. 104 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestStatement.java
  25. 67 0
      jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestTx.java
  26. 38 0
      mybatis/mybatis01/.gitignore
  27. 8 0
      mybatis/mybatis01/.idea/.gitignore
  28. 7 0
      mybatis/mybatis01/.idea/encodings.xml
  29. 14 0
      mybatis/mybatis01/.idea/misc.xml
  30. 42 0
      mybatis/mybatis01/pom.xml
  31. 9 0
      mybatis/mybatis01/src/main/java/com/lc/mybatis/mapper/DeptMapper.java
  32. 74 0
      mybatis/mybatis01/src/main/java/com/lc/mybatis/pojo/Dept.java
  33. 8 0
      mybatis/mybatis01/src/main/resources/log4j.properties
  34. 19 0
      mybatis/mybatis01/src/main/resources/mapper/DeptMapper.xml
  35. 31 0
      mybatis/mybatis01/src/main/resources/mybatis-config.xml
  36. 4 0
      mybatis/mybatis01/src/main/resources/mybatis.properties
  37. 33 0
      mybatis/mybatis01/src/test/java/com/lc/mybatis/TestDept.java

+ 38 - 0
jdbc/jdbc01/.gitignore

@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 8 - 0
jdbc/jdbc01/.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

+ 7 - 0
jdbc/jdbc01/.idea/encodings.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
+  </component>
+</project>

+ 14 - 0
jdbc/jdbc01/.idea/misc.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
+  <component name="MavenProjectsManager">
+    <option name="originalFiles">
+      <list>
+        <option value="$PROJECT_DIR$/pom.xml" />
+      </list>
+    </option>
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 124 - 0
jdbc/jdbc01/.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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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.svg" 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>

+ 38 - 0
jdbc/jdbc01/pom.xml

@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.lc</groupId>
+    <artifactId>jdbc01</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <!-- mysql依赖 -->
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>5.1.49</version>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <version>RELEASE</version>
+            <scope>test</scope>
+        </dependency>
+        <!-- 连接池 -->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.2.21</version>
+        </dependency>
+    </dependencies>
+
+</project>

+ 78 - 0
jdbc/jdbc01/src/main/java/com/lc/jdbc/bean/Dept.java

@@ -0,0 +1,78 @@
+package com.lc.jdbc.bean;
+
+import com.lc.utils.Column;
+import com.lc.utils.Table;
+
+/**
+ * 部门
+ * alt + 鼠标左键:整列编辑
+ * 在实体类中,基本数据类型建议使用其对应的包装类型
+ */
+
+@Table("tb_dept")
+public class Dept {
+
+    @Column(property = "id",filed = "id")
+    private int id; //主键
+    @Column(property = "deptName",filed = "dept_name")
+    private String deptName; //部门名称
+    @Column(property = "ordered",filed = "ordered")
+    private int ordered; //排序
+    @Column(property = "description",filed = "description")
+    private String description; //描述
+    @Column(property = "status",filed = "status")
+    private int status; //状态
+
+    //省略 set、get方法
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getDeptName() {
+        return deptName;
+    }
+
+    public void setDeptName(String deptName) {
+        this.deptName = deptName;
+    }
+
+    public int getOrdered() {
+        return ordered;
+    }
+
+    public void setOrdered(int ordered) {
+        this.ordered = ordered;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+
+    public void setStatus(int status) {
+        this.status = status;
+    }
+
+    @Override
+    public String toString() {
+        return "Dept{" +
+                "id=" + id +
+                ", deptName='" + deptName + '\'' +
+                ", ordered=" + ordered +
+                ", description='" + description + '\'' +
+                ", status=" + status +
+                '}';
+    }
+}

+ 41 - 0
jdbc/jdbc01/src/main/java/com/lc/jdbc/dao/DeptDao.java

@@ -0,0 +1,41 @@
+package com.lc.jdbc.dao;
+
+import com.lc.jdbc.bean.Dept;
+
+import java.sql.SQLException;
+import java.util.List;
+
+public interface DeptDao {
+
+    //根据id查询
+    /**
+     * 表中 一条数据  =  一个对象
+     */
+    public Dept getById( Integer id) throws SQLException;
+
+
+    /**
+     * 查询所有
+     * @return 返回所有数据
+     * @throws SQLException
+     */
+    public List<Dept> getAll() throws SQLException;
+
+
+    /**
+     * 修改部门
+     */
+    public int update(Dept dept) throws SQLException;
+
+
+    /**
+     * 添加部门
+     */
+    public int add(Dept dept) throws SQLException;
+
+    /**
+     * 删除部门
+     */
+    public int delete( Integer id ) throws SQLException ;
+
+}

+ 121 - 0
jdbc/jdbc01/src/main/java/com/lc/jdbc/dao/impl/DeptDaoImpl.java

@@ -0,0 +1,121 @@
+package com.lc.jdbc.dao.impl;
+
+import com.lc.jdbc.bean.Dept;
+import com.lc.jdbc.dao.DeptDao;
+import com.lc.utils.DButils;
+import com.lc.utils.DruidUtils;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DeptDaoImpl implements DeptDao {
+    @Override
+    public Dept getById(Integer id) throws SQLException {
+
+        //获取链接
+        Connection connection = DButils.getConnection();
+        //Sql
+        String sql = " select * from tb_dept where id = ? ";
+        //获取执行对象
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        //设置参数
+        preparedStatement.setInt(1,id);
+        //结果
+        ResultSet resultSet = preparedStatement.executeQuery();
+        Dept dept = null;
+        //循环
+        while (resultSet.next()){
+            dept = new Dept();
+            dept.setId(resultSet.getInt("id"));
+            dept.setDeptName(resultSet.getString("dept_name"));
+            dept.setOrdered(resultSet.getInt("ordered"));
+            dept.setDescription(resultSet.getString("description"));
+            dept.setStatus(resultSet.getInt("status"));
+        }
+        return dept;
+    }
+
+    @Override
+    public List<Dept> getAll() throws SQLException {
+        //获取链接
+        Connection connection = DButils.getConnection();
+        //Sql
+        String sql = " select * from tb_dept ";
+        //获取执行对象
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        //结果
+        ResultSet resultSet = preparedStatement.executeQuery();
+        List<Dept> list = new ArrayList<>();
+        //循环
+        while (resultSet.next()){
+            Dept dept = new Dept();
+            dept.setId(resultSet.getInt("id"));
+            dept.setDeptName(resultSet.getString("dept_name"));
+            dept.setOrdered(resultSet.getInt("ordered"));
+            dept.setDescription(resultSet.getString("description"));
+            dept.setStatus(resultSet.getInt("status"));
+            //添加到集合
+            list.add(dept);
+        }
+        return list;
+    }
+
+    @Override
+    public int update(Dept dept) throws SQLException {
+        //获取链接
+        Connection connection = DButils.getConnection();
+        //sql
+        String sql =" update tb_dept " +
+                " set dept_name = ?,ordered = ?,description = ?,status = ?" +
+                " where id = ? ";
+        //获取执行对象
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        //设置
+        preparedStatement.setString(1,dept.getDeptName());
+        preparedStatement.setInt(2,dept.getOrdered());
+        preparedStatement.setString(3,dept.getDescription());
+        preparedStatement.setInt(4,dept.getStatus());
+        preparedStatement.setInt(5,dept.getId());
+
+        //执行
+        return preparedStatement.executeUpdate();
+    }
+
+    @Override
+    public int add(Dept dept) throws SQLException {
+
+        //获取链接
+        Connection connection = DButils.getConnection();
+        //sql
+        String sql =" INSERT INTO `vipq2`.`tb_dept` ( `dept_name`, `ordered`, `description`, `status`) " +
+                " VALUES (?, ?, ?, ?); ";
+        //获取执行对象
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        //设置
+        preparedStatement.setString(1,dept.getDeptName());
+        preparedStatement.setInt(2,dept.getOrdered());
+        preparedStatement.setString(3,dept.getDescription());
+        preparedStatement.setInt(4,dept.getStatus());
+
+        //执行
+        return preparedStatement.executeUpdate();
+    }
+
+    @Override
+    public int delete(Integer id) throws SQLException {
+        //获取链接
+        Connection connection = DruidUtils.getConnection();
+        //sql
+        String sql = " delete from tb_dept where id = ?  ";
+        //获取执行对象
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        //设置参数
+        preparedStatement.setInt(1,id);
+        //执行
+        return preparedStatement.executeUpdate();
+    }
+}

+ 18 - 0
jdbc/jdbc01/src/main/java/com/lc/jdbc/dao/impl/DeptDaoImpl1.java

@@ -0,0 +1,18 @@
+package com.lc.jdbc.dao.impl;
+
+import com.lc.jdbc.bean.Dept;
+import com.lc.jdbc.dao.DeptDao;
+import com.lc.utils.BaseDAO;
+import com.lc.utils.DButils;
+import com.lc.utils.DruidUtils;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DeptDaoImpl1 extends BaseDAO<Dept> {
+
+}

+ 219 - 0
jdbc/jdbc01/src/main/java/com/lc/utils/BaseDAO.java

@@ -0,0 +1,219 @@
+package com.lc.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ClassName: BaseDao
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2024/1/28 14:30
+ * @Version 1.0
+ */
+public class BaseDAO<T> {
+
+    static Connection connection = null;
+    //获取连接
+    static {
+        //获取连接
+        try {
+            connection  = DButils.getConnection();
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public int insert(T t) throws Exception {
+        Class<T> clazz = (Class<T>) t.getClass();
+
+        String sql = "insert into " + getTableName(clazz) + "( "+getFiledSql(clazz)+" ) values ( ";
+
+        Field[] declaredFields = clazz.getDeclaredFields();
+        int length = declaredFields.length;
+        for (int i = 0; i < length; i++) {
+            if (i== length - 1){
+                sql+= " ?  );";
+            }else{
+                sql+= " ?,";
+            }
+        }
+
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+
+        for (int i = 1; i <= declaredFields.length; i++) {
+            declaredFields[i-1].setAccessible(true);
+
+            preparedStatement.setObject(i,declaredFields[i-1].get(t));
+        }
+
+        return preparedStatement.executeUpdate();
+    }
+
+    public int update(T t) throws Exception {
+        Class<T> clazz = (Class<T>) t.getClass();
+
+        String sql = " update " + getTableName(clazz) + " set ";
+
+        Field[] declaredFields = clazz.getDeclaredFields();
+
+        for (Field declaredField : declaredFields) {
+            String filedName = filedName(declaredField);
+            if (!declaredField.getName().equals("id")){
+                sql+= " " + filedName + " = ? , ";
+            }
+        }
+        sql = sql.substring(0, sql.length() - 2); // 去除最后一个逗号和空格
+
+        sql+= " where id = ?  ";
+
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+
+        int index = 0;
+        for (Field declaredField : declaredFields) {
+            if (!declaredField.getName().equals("id")){
+                declaredField.setAccessible(true);
+                preparedStatement.setObject(index,declaredField.get(t));
+            }
+            index++;
+        }
+        Field id = clazz.getDeclaredField("id");
+        id.setAccessible(true);
+        preparedStatement.setObject(index++,id.get(t));
+
+        return preparedStatement.executeUpdate();
+    }
+
+    public int del(int id) throws Exception {
+        Class<T> clazzClass = getClazzClass();
+        String sql = " delete from " + getTableName(clazzClass) + " where id = ? ";
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        preparedStatement.setObject(1,id);
+        return preparedStatement.executeUpdate();
+    }
+
+    public List<T> getAll()  {
+        //this 子类
+        try {
+            Class<T> clazz = getClazzClass();
+            //sql
+            String sql = "select " + getFiledSql(clazz) + " from " + getTableName(clazz);
+
+            //连接 DButils
+            Connection connection = DButils.getConnection();
+            PreparedStatement preparedStatement = connection.prepareStatement(sql);
+            //结果
+            ResultSet rs = preparedStatement.executeQuery();
+
+            List<T> list = new ArrayList<>();
+
+            while (rs.next()){
+                Constructor<T> constructor = clazz.getConstructor();
+                T t = constructor.newInstance();
+                //反射完成赋值
+                Field[] declaredFields = clazz.getDeclaredFields();
+                for (Field declaredField : declaredFields) {
+                    declaredField.setAccessible(true);
+                    declaredField.set(t,rs.getObject(filedName(declaredField)));
+                }
+                list.add(t);
+            }
+
+            return  list;
+
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public T getId(int id)  {
+        //this 子类
+        try {
+            Class<T> clazz = getClazzClass();
+
+            //sql
+            String sql = "select " + getFiledSql(clazz) + " from " + getTableName(clazz) + " where id = ? ";
+
+            //连接 DButils
+            Connection connection = DButils.getConnection();
+            PreparedStatement preparedStatement = connection.prepareStatement(sql);
+            //设置参数
+            preparedStatement.setInt(1,id);
+            //结果
+            ResultSet rs = preparedStatement.executeQuery();
+            //反射
+            return  getT(rs, clazz);
+
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+
+    public T getT(ResultSet rs , Class<T> clazz) throws Exception{
+
+        Constructor<T> constructor = clazz.getConstructor();
+        T t = constructor.newInstance();
+
+        while (rs.next()){
+            //反射完成赋值
+            Field[] declaredFields = clazz.getDeclaredFields();
+            for (Field declaredField : declaredFields) {
+                declaredField.setAccessible(true);
+                declaredField.set(t,rs.getObject(filedName(declaredField)));
+            }
+        }
+        return t;
+
+    }
+
+    private String getTableName(Class<T> clazz) {
+        //获取表名
+        if (clazz.isAnnotationPresent(Table.class)){
+            return clazz.getAnnotation(Table.class).value();
+        }else {
+            return clazz.getSimpleName();
+        }
+    }
+
+    private String getFiledSql(Class<T> clazz){
+        String filedSql = "";
+        //获取字段
+        Field[] fields = clazz.getDeclaredFields();
+        for (Field field : fields) {
+            //获取字段名
+            filedSql += filedName(field) + " , ";
+        }
+        filedSql = filedSql.substring(0,filedSql.lastIndexOf(","));
+
+        return filedSql;
+    }
+
+    public String filedName(Field field){
+        if (field.isAnnotationPresent(Column.class)){
+            //有注解
+            return field.getAnnotation(Column.class).filed();
+        }else{
+            //没有注解
+            return field.getName();
+        }
+    }
+
+    public Class<T> getClazzClass() throws ClassNotFoundException {
+        Class clazz = this.getClass();
+        //获取父类泛型类型
+        ParameterizedType genericSuperclass = (ParameterizedType) clazz.getGenericSuperclass();
+        //泛型
+        Type cla = genericSuperclass.getActualTypeArguments()[0];
+        //javabean的class
+        Class<T> typeClazz = (Class<T>) Class.forName(cla.getTypeName());
+        return typeClazz;
+    }
+}

+ 23 - 0
jdbc/jdbc01/src/main/java/com/lc/utils/Column.java

@@ -0,0 +1,23 @@
+package com.lc.utils;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * ClassName: Column
+ *
+ * @Author 爱扣钉-陈晨
+ * @Create 2024/1/28 11:27
+ * @Version 1.0
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Column { //标识 数据库中表中字段和javabean属性 对应关系。
+
+    String property() default "";
+
+    String filed() default "";
+
+}

+ 44 - 0
jdbc/jdbc01/src/main/java/com/lc/utils/DButils.java

@@ -0,0 +1,44 @@
+package com.lc.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+public class DButils {
+
+    static Connection connection = null;
+
+    static {
+        try {
+            // 读取配置文件
+            InputStream is = DButils.class.getClassLoader().getResourceAsStream("jdbc.properties");
+
+            //加载 is 输入流
+            Properties properties = new Properties();
+            properties.load(is);
+
+            String url = properties.getProperty("jdbc.url");
+            String username = properties.getProperty("jdbc.username");
+            String password = properties.getProperty("jdbc.password");
+
+            connection = DriverManager.getConnection(url, username, password);
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 获取链接
+     * @return
+     * @throws SQLException
+     */
+    public static Connection getConnection() throws SQLException {
+        return connection;
+    }
+
+}

+ 46 - 0
jdbc/jdbc01/src/main/java/com/lc/utils/DruidUtils.java

@@ -0,0 +1,46 @@
+package com.lc.utils;
+
+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.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+public class DruidUtils {
+
+    static DataSource dataSource;
+
+    static {
+        try {
+            // 读取配置文件
+            InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
+
+            //加载 is 输入流
+            Properties properties = new Properties();
+            properties.load(is);
+            //数据源
+            dataSource = DruidDataSourceFactory.createDataSource(properties);
+
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 获取链接
+     * @return
+     * @throws SQLException
+     */
+    public static Connection getConnection() throws SQLException {
+        return dataSource.getConnection();
+    }
+
+}

+ 14 - 0
jdbc/jdbc01/src/main/java/com/lc/utils/Table.java

@@ -0,0 +1,14 @@
+package com.lc.utils;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE})
+public @interface Table { //标识 数据库中表的名称
+
+    //抽象方法
+    String value() default "";
+}

+ 10 - 0
jdbc/jdbc01/src/main/resources/druid.properties

@@ -0,0 +1,10 @@
+driverClassName=com.mysql.jdbc.Driver
+url=jdbc:mysql:///vipq2?useSSL=false&useServerPrepStmts=true
+username=root
+password=123456
+# ???????
+initialSize=5
+# ?????
+maxActive=10
+# ??????
+maxWait=3000

+ 6 - 0
jdbc/jdbc01/src/main/resources/jdbc.properties

@@ -0,0 +1,6 @@
+#??
+#key=value
+jdbc.driver=com.mysql.jdbc.Driver
+jdbc.url=jdbc:mysql://localhost:3306/vipq2?useUnicode=true&characterEncoding=utf8
+jdbc.username=root
+jdbc.password=123456

+ 56 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestConnection.java

@@ -0,0 +1,56 @@
+package com.lc.jdbc01;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+public class TestConnection {
+
+
+    @DisplayName("测试连接数据库")
+    @Test
+    public void testConnection() throws Exception{
+        // 加载驱动
+        //Class.forName("com.mysql.jdbc.Driver");
+
+        // 获取连接
+        /* Params:
+            url – a database url
+            user – the database user
+            password – the user's password
+         */
+        String url = "jdbc:mysql://localhost:3306/vip33";
+        String user = "root";
+        String password = "123456";
+        Connection connection = DriverManager.getConnection(url, user, password);
+
+        //Sql 语句
+        String sql = "select * from tb_user";
+
+        //sql 执行对象
+        Statement statement = connection.createStatement();
+
+        //执行sql
+        ResultSet resultSet = statement.executeQuery(sql);
+
+        //遍历结果集
+        while (resultSet.next()){
+            System.out.print(resultSet.getString("id")+"  ");
+            System.out.print(resultSet.getString("username")+"  ");
+            System.out.print(resultSet.getString("password")+"  ");
+
+            System.out.println();
+        }
+
+        //关闭资源
+        resultSet.close();
+        statement.close();
+        connection.close();
+
+    }
+
+}

+ 21 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDBUtils.java

@@ -0,0 +1,21 @@
+package com.lc.jdbc01;
+
+import com.lc.utils.DButils;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public class TestDBUtils {
+
+
+    @Test
+    public void test() throws SQLException {
+
+        Connection connection = DButils.getConnection();
+
+        System.out.println(connection);
+
+    }
+
+}

+ 91 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDept.java

@@ -0,0 +1,91 @@
+package com.lc.jdbc01;
+
+import com.lc.jdbc.bean.Dept;
+import com.lc.jdbc.dao.DeptDao;
+import com.lc.jdbc.dao.impl.DeptDaoImpl;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.sql.SQLException;
+import java.util.List;
+
+public class TestDept {
+
+
+    @DisplayName("测试删除")
+    @Test
+    public void test05() throws SQLException {
+
+        DeptDao deptDao = new DeptDaoImpl();
+
+        int del = deptDao.delete(4);
+
+        System.out.println("del:" + del);
+    }
+
+
+    @DisplayName("测试添加")
+    @Test
+    public void test04() throws SQLException {
+
+        DeptDao deptDao = new DeptDaoImpl();
+
+        Dept dept = new Dept();
+        dept.setDeptName("销售部");
+        dept.setDescription("销售部描述");
+        dept.setOrdered(21);
+        dept.setStatus(1);
+
+        int add = deptDao.add(dept);
+
+        System.out.println("add:" + add);
+    }
+
+
+    @DisplayName("测试修改")
+    @Test
+    public void test03() throws SQLException {
+
+        DeptDao deptDao = new DeptDaoImpl();
+
+        Dept dept = new Dept();
+        dept.setId(1);
+        dept.setDeptName("技术部");
+        dept.setDescription("技术部描述");
+        dept.setOrdered(20);
+        dept.setStatus(1);
+
+        int update = deptDao.update(dept);
+
+        System.out.println("update:" + update);
+
+
+    }
+
+
+    @DisplayName("测试查询所有")
+    @Test
+    public void test02() throws SQLException {
+
+        DeptDao deptDao = new DeptDaoImpl();
+
+        List<Dept> deptList = deptDao.getAll();
+
+        deptList.forEach(System.out::println);
+
+    }
+
+
+    @DisplayName("测试根据id查询")
+    @Test
+    public void test01() throws SQLException {
+
+        DeptDao deptDao = new DeptDaoImpl();
+
+        Dept dept = deptDao.getById(1);
+
+        System.out.println("dept:" + dept);
+    }
+}

+ 21 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDept1.java

@@ -0,0 +1,21 @@
+package com.lc.jdbc01;
+
+import com.lc.jdbc.bean.Dept;
+import com.lc.jdbc.dao.impl.DeptDaoImpl1;
+import org.junit.jupiter.api.Test;
+
+public class TestDept1 {
+
+    @Test
+    public void test01(){
+
+        DeptDaoImpl1 daoImpl1 = new DeptDaoImpl1();
+
+        Dept dept = daoImpl1.getId(1);
+
+        System.out.println(dept);
+
+    }
+
+
+}

+ 33 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestDruid.java

@@ -0,0 +1,33 @@
+package com.lc.jdbc01;
+
+
+import com.alibaba.druid.pool.DruidDataSourceFactory;
+import org.junit.jupiter.api.Test;
+
+import javax.sql.DataSource;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.util.Properties;
+
+public class TestDruid {
+
+
+    @Test
+    public void test01() throws Exception {
+
+        InputStream is = TestDruid.class.getClassLoader().getResourceAsStream("druid.properties");
+
+        Properties p = new Properties();
+        p.load(is);
+
+        //构建
+        DataSource dataSource = DruidDataSourceFactory.createDataSource(p);
+
+        //获取链接
+        Connection connection = dataSource.getConnection();
+
+        System.out.println(connection);
+
+    }
+
+}

+ 56 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestRS.java

@@ -0,0 +1,56 @@
+package com.lc.jdbc01;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.sql.*;
+
+public class TestRS {
+
+    Connection connection= null;
+
+    @BeforeEach
+    public void before() throws SQLException {
+        // 获取连接
+        /* Params:
+            url – a database url
+            user – the database user
+            password – the user's password
+         */
+        String url = "jdbc:mysql://localhost:3306/vipq2";
+        String user = "root";
+        String password = "123456";
+        connection = DriverManager.getConnection(url, user, password);
+    }
+
+
+    @DisplayName("测试返回结果")
+    @Test
+    public void test1() throws Exception{
+
+        //Sql 添加 语句
+        String sql = "  select * from tb_user ";
+
+        PreparedStatement statement = connection.prepareStatement(sql);;
+
+        //执行sql
+        ResultSet resultSet = statement.executeQuery();
+
+        while (resultSet.next()){
+            //获取数据 根据字段名
+//            int id = resultSet.getInt("id");
+//            String name = resultSet.getString("username");
+//            String password1 = resultSet.getString("password");
+
+            //索引
+            int id = resultSet.getInt(1);
+            String name = resultSet.getString(2);
+            String password1 = resultSet.getString(3);
+            System.out.println(id + " " + name + " " + password1);
+        }
+        //关闭连接
+        connection.close();
+    }
+
+}

+ 104 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestStatement.java

@@ -0,0 +1,104 @@
+package com.lc.jdbc01;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.sql.*;
+
+public class TestStatement {
+
+    @DisplayName("测试添加用户预编译")
+    @Test
+    public void test1() throws Exception{
+
+        // 获取连接
+        /* Params:
+            url – a database url
+            user – the database user
+            password – the user's password
+         */
+        String url = "jdbc:mysql://localhost:3306/vipq2";
+        String user = "root";
+        String password = "123456";
+        Connection connection = DriverManager.getConnection(url, user, password);
+
+        String name = "aaa)';"; // 影响sql语句
+        String pwd = "2222";
+        String phone = "15000000001";
+        String email = "a222@163.com";
+
+        //PreparedStatement
+        /*
+            sql 改变 所有的参数 用? 代替
+            创建 PreparedStatement 传递sql语句 解析?
+            设置? 的值
+            执行 executeUpdate 不需要 sql 返回值 表中 受影响的记录
+
+         */
+
+        //Sql 添加 语句
+        //String sql = " INSERT INTO `tb_user` (`username`, `password`, `phone`, `email`) VALUES ('aaa)';
+        String sql = " INSERT INTO `tb_user` (`username`, `password`, `phone`, `email`) VALUES ( ?, ?, ?, ? )";
+
+        //sql 执行对象
+        PreparedStatement statement = connection.prepareStatement(sql);
+        // 设置 ? 值
+        // 参数1 ? 的位置 从1开始
+        // 参数2 ? 的值
+        statement.setString(1,name);
+        statement.setString(2,pwd);
+        statement.setString(3,phone);
+        statement.setString(4,email);
+
+        //执行sql
+        int execute = statement.executeUpdate();
+
+        System.out.println(execute);
+
+        //关闭连接
+        statement.close();
+        connection.close();
+
+    }
+
+
+    @DisplayName("测试添加用户")
+    @Test
+    public void test() throws Exception{
+
+        // 获取连接
+        /* Params:
+            url – a database url
+            user – the database user
+            password – the user's password
+         */
+        String url = "jdbc:mysql://localhost:3306/vipq2";
+        String user = "root";
+        String password = "123456";
+        Connection connection = DriverManager.getConnection(url, user, password);
+
+        String name = "aaa)';"; // 影响sql语句
+        String pwd = "2222";
+        String phone = "15000000001";
+        String email = "a222@163.com";
+
+
+        //Sql 添加 语句
+        //String sql = " INSERT INTO `tb_user` (`username`, `password`, `phone`, `email`) VALUES ('aaa)';
+        String sql = " INSERT INTO `tb_user` (`username`, `password`, `phone`, `email`) VALUES ( '"+name+"', '"+pwd+"', '"+phone+"', '"+email+"')";
+
+        //sql 执行对象
+        Statement statement = connection.createStatement();
+
+        //执行sql
+        boolean execute = statement.execute(sql);
+
+        System.out.println(execute);
+
+        //关闭连接
+        statement.close();
+        connection.close();
+
+    }
+
+}

+ 67 - 0
jdbc/jdbc01/src/test/java/com/lc/jdbc01/TestTx.java

@@ -0,0 +1,67 @@
+package com.lc.jdbc01;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.sql.*;
+
+public class TestTx {
+
+    @DisplayName("测试添加用户预编译")
+    @Test
+    public void test1() throws Exception{
+
+        // 获取连接
+        /* Params:
+            url – a database url
+            user – the database user
+            password – the user's password
+         */
+        String url = "jdbc:mysql://localhost:3306/vipq2";
+        String user = "root";
+        String password = "123456";
+        Connection connection = DriverManager.getConnection(url, user, password);
+
+        //开启事务
+        connection.setAutoCommit(false);
+
+        //Sql 添加 语句
+        String sql1 = " UPDATE `vipq2`.`tb_user` SET  `phone` = ? WHERE `id` = ?; ";
+        String sql2 = " UPDATE `vipq2`.`tb_user` SET  `phone` = ? WHERE `id` = ?; ";
+        PreparedStatement statement1 = null;
+        PreparedStatement statement2 = null;
+
+        try {
+            //sql 执行对象
+            statement1 = connection.prepareStatement(sql1);
+            statement2 = connection.prepareStatement(sql2);
+
+            statement1.setString(1,"123456789");
+            statement1.setInt(2,1);
+
+            //执行sql
+            int execute1 = statement1.executeUpdate();
+            //错误
+            int i = 1/0;
+
+            statement2.setString(1,"987654321");
+            statement2.setInt(2,10);
+
+            //执行sql
+            int execute2 = statement2.executeUpdate();
+
+            // 提交事务
+            connection.commit();
+        } catch (SQLException e) {
+            //回滚事务
+            connection.rollback();
+        }
+
+        //关闭连接
+        statement1.close();
+        statement2.close();
+        connection.close();
+
+    }
+
+}

+ 38 - 0
mybatis/mybatis01/.gitignore

@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 8 - 0
mybatis/mybatis01/.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

+ 7 - 0
mybatis/mybatis01/.idea/encodings.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
+  </component>
+</project>

+ 14 - 0
mybatis/mybatis01/.idea/misc.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
+  <component name="MavenProjectsManager">
+    <option name="originalFiles">
+      <list>
+        <option value="$PROJECT_DIR$/pom.xml" />
+      </list>
+    </option>
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 42 - 0
mybatis/mybatis01/pom.xml

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.lc</groupId>
+    <artifactId>mybatis01</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>5.1.49</version>
+        </dependency>
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis</artifactId>
+            <version>3.5.16</version>
+        </dependency>
+        <!-- log4j -->
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <version>1.2.17</version>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <version>RELEASE</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>

+ 9 - 0
mybatis/mybatis01/src/main/java/com/lc/mybatis/mapper/DeptMapper.java

@@ -0,0 +1,9 @@
+package com.lc.mybatis.mapper;
+
+import com.lc.mybatis.pojo.Dept;
+
+public interface DeptMapper {
+
+    public Dept getById( Integer id );
+
+}

+ 74 - 0
mybatis/mybatis01/src/main/java/com/lc/mybatis/pojo/Dept.java

@@ -0,0 +1,74 @@
+package com.lc.mybatis.pojo;
+
+/**
+ * 部门
+ * alt + 鼠标左键:整列编辑
+ * 在实体类中,基本数据类型建议使用其对应的包装类型
+ */
+
+
+public class Dept {
+
+    private int id; //主键
+
+    private String deptName; //部门名称
+
+    private int ordered; //排序
+
+    private String description; //描述
+
+    private int status; //状态
+
+    //省略 set、get方法
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getDeptName() {
+        return deptName;
+    }
+
+    public void setDeptName(String deptName) {
+        this.deptName = deptName;
+    }
+
+    public int getOrdered() {
+        return ordered;
+    }
+
+    public void setOrdered(int ordered) {
+        this.ordered = ordered;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+
+    public void setStatus(int status) {
+        this.status = status;
+    }
+
+    @Override
+    public String toString() {
+        return "Dept{" +
+                "id=" + id +
+                ", deptName='" + deptName + '\'' +
+                ", ordered=" + ordered +
+                ", description='" + description + '\'' +
+                ", status=" + status +
+                '}';
+    }
+}

+ 8 - 0
mybatis/mybatis01/src/main/resources/log4j.properties

@@ -0,0 +1,8 @@
+# ??????
+log4j.rootLogger=ERROR, stdout
+# MyBatis ???? ???
+log4j.logger.com.lc.mybatis=DEBUG
+# ?????
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

+ 19 - 0
mybatis/mybatis01/src/main/resources/mapper/DeptMapper.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<!--
+    namespace 指向 接口的路径名称
+  -->
+<mapper namespace="com.lc.mybatis.mapper.DeptMapper" >
+
+    <!--
+        id 名称  = 方法名称
+        resultType = 返回值类型
+        parameterType = 方法参数  可以省略
+     -->
+    <select id="getById" resultType="com.lc.mybatis.pojo.Dept" >
+        select * from tb_dept where id = #{id}
+    </select>
+
+</mapper>

+ 31 - 0
mybatis/mybatis01/src/main/resources/mybatis-config.xml

@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration
+        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+
+    <!-- properties -->
+    <properties resource="mybatis.properties" />
+
+    <!-- 设置 -->
+    <settings>
+        <setting name="mapUnderscoreToCamelCase" value="true"/>
+    </settings>
+
+    <environments default="development">
+        <environment id="development">
+            <transactionManager type="JDBC"/>
+            <dataSource type="POOLED">
+                <!-- 数据源信息   -->
+                <property name="driver" value="${driver}"/>
+                <property name="url" value="${url}"/>
+                <property name="username" value="${username}"/>
+                <property name="password" value="${password}"/>
+            </dataSource>
+        </environment>
+    </environments>
+    <mappers>
+        <!-- 加载映射文件 -->
+        <mapper resource="mapper/DeptMapper.xml"/>
+    </mappers>
+</configuration>

+ 4 - 0
mybatis/mybatis01/src/main/resources/mybatis.properties

@@ -0,0 +1,4 @@
+driver=com.mysql.jdbc.Driver
+url=jdbc:mysql://localhost:3306/vipq2
+username=root
+password=123456

+ 33 - 0
mybatis/mybatis01/src/test/java/com/lc/mybatis/TestDept.java

@@ -0,0 +1,33 @@
+package com.lc.mybatis;
+
+import com.lc.mybatis.mapper.DeptMapper;
+import com.lc.mybatis.pojo.Dept;
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class TestDept {
+
+
+    @Test
+    public void testDept() throws IOException {
+        //根据核心配置文件创建SqlSessionFactory
+        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
+        //sessionFactory工厂
+        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
+        //sqlSession
+        SqlSession sqlSession = sessionFactory.openSession();
+        //DeptMapper接口
+        DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
+
+        Dept dept = deptMapper.getById(1);
+
+        System.out.println("dept = " + dept);
+    }
+
+}