Spring 加JdbcTemplate 加内存数据库 完成插入和查询功能JDK17版本

效果 :

项目目录结构 :

代码: JDK17版本 博主是

pom.xml文件

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>spring03</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring03 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.20</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2.1-b03</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.20</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.3.20</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.20</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.9</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 监听器的依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.20</version>
    </dependency>
    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>5.0.1</version>
    </dependency>

    <dependency>
      <groupId>org.hsqldb</groupId>
      <artifactId>hsqldb</artifactId>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-annotations-api</artifactId>
      <version>9.0.63</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>spring03</finalName>
  </build>
</project>

2.jdbc.properties文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:hsqldb:file:testdb
jdbc.username=sa
jdbc.password=

3.AppConfig 类代码、

package com.bjpowernode.config;

import com.bjpowernode.entity.Student;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.StatementCreatorUtils;

import java.sql.ResultSet;

@Configuration//标记本类为配置类
@ComponentScan("com")//扫描本类包下面的Component

public class AppConfig {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);//加载配置类
        JdbcTemplate b=context.getBean(JdbcTemplate.class);//获取对象 从Spring 容器里面


        String sql="insert into users values(?,?)";
        b.update(sql,1,"ww");//简化操作 我都没有写数据库连接,执行就插入语句,这个JdbcTemplate 自动帮我连接了
        String query="select * from users";

        //没有办法 ,下面gou b 语句就这样 返回的是一个Student 类
        Student ss=b.queryForObject(query,null,(ResultSet rs,int rowNum)->{
            Integer i=rs.getInt("id");
            String name=rs.getString("name");
            return new Student(i,name);
        });

        System.out.println(ss);//输出信息


    }


}

4.JdbcConfig类

package com.bjpowernode.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;

@PropertySource("jdbc.properties") //自动加载这个文件的内容 不用我们手动写文件读取代码
@Component//自动 new一个对象JdbcConfig
public class JdbcConfig {
    @Value("${jdbc.url}")
    String jdbcUrl;//内存数据库连接
    @Value("${jdbc.username}")
    String jdbcName;//内存数据库 用户名
    @Value("${jdbc.password}")
    String jdbcPassword;//内存数据库密码
    @Autowired
    JdbcTemplate jdbcTemplate;//简单理解,这个类就是简化sql 操作的 我们不用手动连接数据库 断开数据库,我们只写增删改查 sql语句 就可以了
    //简化操作罢了


    @Bean//自动调用方法 把这个对象 放入spring 容器里面
    public DataSource createDataSource(){
        HikariConfig config=new HikariConfig();
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(jdbcName);
        config.setPassword(jdbcPassword);
        config.addDataSourceProperty("autoCommit", "true");
        config.addDataSourceProperty("connectionTimeout", "5");
        config.addDataSourceProperty("idleTimeout", "60");
        return new HikariDataSource(config);
    }

    @Bean
    JdbcTemplate createJdbcTemplate(@Autowired DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


    @PostConstruct//创建 DatabaseInitializer 之后Spring 自动执行该方法
    public void init() {
        jdbcTemplate.update("CREATE TABLE IF NOT EXISTS users ("
                + "id BIGINT IDENTITY NOT NULL PRIMARY KEY, "
                + "name VARCHAR(100) NOT NULL)"
        );
    }

    @PreDestroy//在销毁JdbcConfig 这个对象前Spring自动调用该方法
    public void clean() {
        jdbcTemplate.update("DROP TABLE users");
    }
}
package com.bjpowernode.entity;

import org.springframework.stereotype.Component;

/**
 *
 */
//没有什么好说的,就是代表数据库一条记录
public class Student {

    private Integer id;//
    private String name;
    public Student(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name  +
                '}';
    }
}

我说不明白,代码个人感觉很少,中等,数据库就两个字段,都不敢3个字段 ,类也就3个类,不敢多写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值