Răsfoiți Sursa

jdbc源码演示

wilbur 3 ani în urmă
părinte
comite
17b543515a

+ 33 - 0
lab-springBoot/SpringBoot-4/.gitignore

@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/

+ 68 - 0
lab-springBoot/SpringBoot-4/pom.xml

@@ -0,0 +1,68 @@
+<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.5.3</version>
+        <relativePath/> <!-- lookup parent from repository -->
+    </parent>
+    <groupId>com.xiang</groupId>
+    <artifactId>jdbc</artifactId>
+    <version>1.0</version>
+    <name>jdbc</name>
+    <description>翔基博客学习项目demo</description>
+    <properties>
+        <java.version>1.8</java.version>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-jdbc</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <excludes>
+                        <exclude>
+                            <groupId>org.projectlombok</groupId>
+                            <artifactId>lombok</artifactId>
+                        </exclude>
+                    </excludes>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 13 - 0
lab-springBoot/SpringBoot-4/src/main/java/com/xiang/jdbc/JdbcApplication.java

@@ -0,0 +1,13 @@
+package com.xiang.jdbc;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class JdbcApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(JdbcApplication.class, args);
+    }
+
+}

+ 18 - 0
lab-springBoot/SpringBoot-4/src/main/java/com/xiang/jdbc/entity/User.java

@@ -0,0 +1,18 @@
+package com.xiang.jdbc.entity;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author Xiang
+ * @Description
+ * @create 2021-08-10 11:03
+ **/
+@Data
+@NoArgsConstructor
+public class User {
+
+    private String name;
+    private Integer age;
+
+}

+ 47 - 0
lab-springBoot/SpringBoot-4/src/main/java/com/xiang/jdbc/service/UserService.java

@@ -0,0 +1,47 @@
+package com.xiang.jdbc.service;
+
+import com.xiang.jdbc.entity.User;
+
+import java.util.List;
+
+/**
+ * @author Xiang
+ * @Description
+ * @create 2021-08-10 11:15
+ **/
+public interface UserService {
+
+    /**
+     * 新增一个用户
+     *
+     * @param name
+     * @param age
+     */
+    int create(String name, Integer age);
+
+    /**
+     * 根据name查询用户
+     *
+     * @param name
+     * @return
+     */
+    List<User> getByName(String name);
+
+    /**
+     * 根据name删除用户
+     *
+     * @param name
+     */
+    int deleteByName(String name);
+
+    /**
+     * 获取用户总量
+     */
+    int getAllUsers();
+
+    /**
+     * 删除所有用户
+     */
+    int deleteAllUsers();
+
+}

+ 55 - 0
lab-springBoot/SpringBoot-4/src/main/java/com/xiang/jdbc/service/impl/UserServiceImpl.java

@@ -0,0 +1,55 @@
+package com.xiang.jdbc.service.impl;
+
+import com.xiang.jdbc.entity.User;
+import com.xiang.jdbc.service.UserService;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @author Xiang
+ * @Description
+ * @create 2021-08-10 11:16
+ **/
+@Service
+public class UserServiceImpl implements UserService {
+
+    private JdbcTemplate jdbcTemplate;
+
+    UserServiceImpl(JdbcTemplate jdbcTemplate) {
+        this.jdbcTemplate = jdbcTemplate;
+    }
+
+    @Override
+    public int create(String name, Integer age) {
+        return jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
+    }
+
+    @Override
+    public List<User> getByName(String name) {
+        List<User> users = jdbcTemplate.query("select NAME, AGE from USER where NAME = ?", (resultSet, i) -> {
+            User user = new User();
+            user.setName(resultSet.getString("NAME"));
+            user.setAge(resultSet.getInt("AGE"));
+            return user;
+        }, name);
+        return users;
+    }
+
+    @Override
+    public int deleteByName(String name) {
+        return jdbcTemplate.update("delete from USER where NAME = ?", name);
+    }
+
+    @Override
+    public int getAllUsers() {
+        return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
+    }
+
+    @Override
+    public int deleteAllUsers() {
+        return jdbcTemplate.update("delete from USER");
+    }
+
+}

+ 7 - 0
lab-springBoot/SpringBoot-4/src/main/resources/application.yml

@@ -0,0 +1,7 @@
+
+spring:
+  datasource:
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    url: jdbc:mysql://localhost:3306/springboot_learn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
+    username: root
+    password: 123456

+ 65 - 0
lab-springBoot/SpringBoot-4/src/test/java/com/xiang/jdbc/JdbcApplicationTests.java

@@ -0,0 +1,65 @@
+package com.xiang.jdbc;
+
+import com.xiang.jdbc.entity.User;
+import com.xiang.jdbc.service.UserService;
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.List;
+
+@SpringBootTest
+class JdbcApplicationTests {
+
+    //注入数据源
+    @Autowired
+    DataSource dataSource;
+
+    @Autowired
+    private UserService userSerivce;
+
+    @Test
+    void contextLoads()  throws SQLException {
+        //看一下默认数据源
+        System.out.println(dataSource.getClass());
+        //获得连接
+        Connection connection = dataSource.getConnection();
+        System.out.println(connection);
+        //关闭连接
+        connection.close();
+    }
+
+    @Test
+    public void test() throws Exception {
+        // 准备,清空user表
+        userSerivce.deleteAllUsers();
+
+        // 插入5个用户
+        userSerivce.create("User1", 11);
+        userSerivce.create("User2", 13);
+        userSerivce.create("User3", 36);
+        userSerivce.create("User4", 19);
+        userSerivce.create("User5", 18);
+
+        // 查询名为User1的用户,判断年龄是否匹配
+        List<User> userList = userSerivce.getByName("User1");
+        Assert.assertEquals(11, userList.get(0).getAge().intValue());
+
+        // 查数据库,应该有5个用户
+        Assert.assertEquals(5, userSerivce.getAllUsers());
+
+        // 删除两个用户
+        userSerivce.deleteByName("User4");
+        userSerivce.deleteByName("User5");
+
+        // 查数据库,应该有3个用户
+        Assert.assertEquals(3, userSerivce.getAllUsers());
+
+    }
+
+
+}