commit 2bd2d6f49e26a21dc5ae771cfcf4c11dbb9b1f8d Author: DC_DC <3284155190@qq.com> Date: Fri Nov 24 22:10:59 2023 +0800 初始版本 实现了简单的JDBC操作数据库语法 并进阶使用mybatis来操作数据库 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.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 \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..9ce2b96 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/ssmdemo + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..fbc568f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,71 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..af0e43a --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ec505e --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# 项目流程概述 +- 创建maven工程 +## 回顾JDBC +1. 在pom.xml中使用标签引入MySQL依赖包 +2. 准备数据库示例数据 +3. 编写JDBC测试类 + 1. 加载数据库驱动(新旧版本有区别) + 2. 获取连接(使用数据库地址、用户名、密码) + 3. 执行SQL语句(此处可以加上防注入处理) + 4. 对查询数据返回的数据集进行处理,可以输出到控制台 + 5. 关闭连接、释放资源 +## 使用Mybatis +1. 添加Maven依赖 + 在pom.xml中使用标签引入依赖包 + - mybatis + - mysql +2. 创建配置文件mybatis-config.xml + - 属性配置 + - 驱动 + - 数据库地址 + - 用户名 + - 密码 + - 环境配置 + - 默认环境 + - 其他环境 + - 映射器 + - 指定包名(路径、URL) +3. 创建映射器接口 + - 创建接口,方法名和参数要与映射文件中SQL语句一一对应 +4. 编写SQL映射文件 + 在MyMapper.xml中编写 + 1. 命名空间和唯一标识 + 作为参数传入`sqlSession`对象方法中 + 2. SQL语句 + 操作CRUD,注意映射类型`resultType` +5. 创建数据模型 + 数据模型和映射器接口有所不同,可以没有映射器接口但是必须有数据模型,即Java类 +6. 编写业务逻辑代码 + in your showtime \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ea5b157 --- /dev/null +++ b/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + cn.ssm + DC_JavaWeb2 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + mysql + mysql-connector-java + 8.0.33 + + + org.mybatis + mybatis + 3.5.13 + + + \ No newline at end of file diff --git a/src/main/java/cn/ssm/Main.java b/src/main/java/cn/ssm/Main.java new file mode 100644 index 0000000..1f93b07 --- /dev/null +++ b/src/main/java/cn/ssm/Main.java @@ -0,0 +1,10 @@ +package cn.ssm; + +/** + * @author 32841 + */ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/src/main/java/cn/ssm/domian/User.java b/src/main/java/cn/ssm/domian/User.java new file mode 100644 index 0000000..e74f78d --- /dev/null +++ b/src/main/java/cn/ssm/domian/User.java @@ -0,0 +1,34 @@ +package cn.ssm.domian; + +import java.util.Date; + +/** + * @author 32841 + */ +public class User { + Integer id; + String user_name; + String password; + String name; + Integer age; + Integer sex; + Date birthday; + Date created; + Date updated; + + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", userName='" + user_name + '\'' + + ", password='" + password + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + ", sex=" + sex + + ", birthday=" + birthday + + ", created=" + created + + ", updated=" + updated + + '}'; + } +} diff --git a/src/main/java/cn/ssm/test/JDBCTest.java b/src/main/java/cn/ssm/test/JDBCTest.java new file mode 100644 index 0000000..e0612aa --- /dev/null +++ b/src/main/java/cn/ssm/test/JDBCTest.java @@ -0,0 +1,55 @@ +package cn.ssm.test; + +/* +* 导入Java中数据库的接口 +* */ +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +/** + * @author 32841 + */ +public class JDBCTest { + public static void main(String[] args) throws Exception { // 添加抛出异常到方法签名 + Connection connection = null; // 使定义数据库连接对象 + PreparedStatement prepareStatement = null; // 定义执行SQL语句的对象 + ResultSet rs = null; // 定义数据库返回结果集对象 + + try { + // 加载驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); + // 获取连接 + String url = "jdbc:mysql://127.0.0.1:3306/ssmdemo"; + String user = "root"; + String password = "dcsy961618762"; + connection = DriverManager.getConnection(url, user, password); + // 获取statement,preparedStatement + String sql = "select * from tb_user where id=?"; // 防SQL注入处理 + prepareStatement = connection.prepareStatement(sql); // 执行SQL语句 + // 设置参数 + prepareStatement.setLong(1, 3L); // 设置占位符的值,第一个参数是索引值,第二个是值 + // 执行查询 + rs = prepareStatement.executeQuery(); // rs就是数据库查询返回的结果集 + // 处理结果集 + while (rs.next()) { // 行指针 + System.out.println(rs.getString("user_name")); + System.out.println(rs.getString("name")); + System.out.println(rs.getInt("age")); + System.out.println(rs.getDate("birthday")); + } + } finally { + // 关闭连接,释放资源 + if (rs != null) { + rs.close(); + } + if (prepareStatement != null) { + prepareStatement.close(); + } + if (connection != null) { + connection.close(); + } + } + } +} diff --git a/src/main/java/cn/ssm/test/MybatisTest.java b/src/main/java/cn/ssm/test/MybatisTest.java new file mode 100644 index 0000000..2452da7 --- /dev/null +++ b/src/main/java/cn/ssm/test/MybatisTest.java @@ -0,0 +1,32 @@ +package cn.ssm.test; + +import cn.ssm.domian.User; +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 java.io.IOException; +import java.io.InputStream; + +/** + * @author 32841 + */ +public class MybatisTest { + + public static void main(String[] args) throws IOException { + // 指定全局配置文件 + String resource = "mybatis-config.xml"; + // 读取配置文件 + InputStream inputStream = Resources.getResourceAsStream(resource); + // 构建sqlSessionFactory + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + + // 获取sqlSession + SqlSession sqlSession = sqlSessionFactory.openSession(); + // 操作CRUD,第一个参数:指定statement,规则:命名空间+“.”+statementId + // 第二个参数:指定传入sql的参数:这里是用户id + User user = sqlSession.selectOne("MyMapper.selectUser", 1); + System.out.println(user); + } +} diff --git a/src/main/resources/mappers/MyMapper.xml b/src/main/resources/mappers/MyMapper.xml new file mode 100644 index 0000000..c787241 --- /dev/null +++ b/src/main/resources/mappers/MyMapper.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/src/main/resources/mybatis-config.xml b/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..b31d6f3 --- /dev/null +++ b/src/main/resources/mybatis-config.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +