首页 > 编程语言 >javaee spring jdbcTemplate的使用

javaee spring jdbcTemplate的使用

时间:2023-09-03 12:35:26浏览次数:37  
标签:spring javaee springframework jdbcTemplate import test org com


依赖

<?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>org.example</groupId>
  <artifactId>testspring02</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>testspring02 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

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

    <!-- 导入spring的核心jar包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.37</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
    <!-- 配置的 spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>testspring02</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

测试类

package com.test.springJdbc;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;

/**
 * @description:
 * @projectName:testspring2
 * @see:com.test.springJdbc
 * @author:123
 * @createTime:2023/8/28 19:33
 */
public class TestJdbcTemplate {
    @Test
    public void test()
    {
        //1.创建一个连接池

        ComboPooledDataSource pooledDataSource=new ComboPooledDataSource();
        try {
            pooledDataSource.setDriverClass("com.mysql.jdbc.Driver");

            pooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/itstar");

            pooledDataSource.setUser("itstar");

            pooledDataSource.setPassword("yyy123456");

            //2.创建JdbcTemplate对象
            JdbcTemplate jdbcTemplate=new JdbcTemplate(pooledDataSource);

            //3.执行sql
            String sql="insert into user values(null,?,?)";

            jdbcTemplate.update(sql,"daimenglaoshi","123");


        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }






    }
}

用spring管理jdbc相关类

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 开启注解-->
    <context:component-scan base-package="com.test" />

    <!-- 导入db.properties -->
    <context:property-placeholder location="db.properties" />

    <!-- 创建数据源-->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}" />
        <property name="jdbcUrl" value="${jdbcUrl}" />
        <property name="user" value="${user}" />
        <property name="password" value="${password}" />
    </bean>
    <!--创建JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="comboPooledDataSource" />
    </bean>

</beans>

测试类

package com.test.dao.impl;

import com.test.dao.IUsersDao;
import com.test.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @projectName:testspring2
 * @see:com.test.dao.impl
 * @author:杨钧博
 * @createTime:2023/8/28 21:56
 */
@Component
public class UsersDao implements IUsersDao {

    //注入jdbcTemplate对象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Users user) {
        String sql = "insert into users values(null,?,?)";

        jdbcTemplate.update(sql, user.getName(), user.getPassword());
    }
}
package com.test.springJdbc;

import com.test.dao.IUsersDao;
import com.test.pojo.Users;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestUsersDao {

    @Test
    public void test()
    {
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        IUsersDao usersDao=  applicationContext.getBean("usersDao",IUsersDao.class);

        Users user=new Users("xiaowanglaoshi","123");

        usersDao.add(user);
    }



}


标签:spring,javaee,springframework,jdbcTemplate,import,test,org,com
From: https://blog.51cto.com/u_15295225/7339396

相关文章

  • javaee spring 依赖注入之复杂类型的注入数组 集合 等
    spring依赖注入之复杂类型的注入packagecom.test.pojo;importjava.util.List;importjava.util.Map;importjava.util.Properties;/***@description:*@projectName:testSpring*@see:com.test.pojo*@createTime:2023/8/2714:39*/publicclassAA{pri......
  • javaee spring 依赖注入之构造方法注入
    依赖注入之构造方法注入constructor-arg标签指定构造方法的参数packagecom.test.pojo;/***@description:*@projectName:testSpring*@see:com.test.pojo*@author:杨钧博*@createTime:2023/8/2711:18*/publicclassDrink{privateStringname;pri......
  • javaee spring依赖注入之property注入
    spring依赖注入之property注入如下图,如果是基本数据类型,用value属性,如果是对象,则用ref......
  • javaee spring创建对象的三种方式
    方式一调用类的构造方法创建方法二使用静态工厂创建方法三使用非静态工厂创建......
  • javaee spring配置文件bean标签详解
    <beanid="drink_01"name="drink_02"scope="singleton"lazy-init="true"init-method="init"destroy-method="destroy"class="com.test.pojo.Drink"/>scope属性bean标签中添加......
  • Java:SpringBoot实现定时任务Scheduled
    代码示例packagecom.example.demo.config;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.annotation.EnableScheduling;importorg.springframework.scheduling.annotation.Scheduled;importjava.text.SimpleDate......
  • Java:SpringBoot整合SSE(Server-Sent Events)实现后端主动向前端推送数据
    SpringBoot整合SSE(Server-SentEvents)可以实现后端主动向前端推送数据目录核心代码完整代码参考文章核心代码依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>后端接收sse连接@Controller......
  • Java:SpringBoot使用AES对JSON数据加密和解密
    目录1、加密解密原理2、项目示例2.1、项目结构2.2、常规业务代码2.3、加密的实现2.4、接口测试2.5、总结1、加密解密原理客户端和服务端都可以加密和解密,使用base64进行网络传输加密方字符串->AES加密->base64解密方base64->AES解密->字符串2、项目示例2.1、项目结构$tr......
  • SpringBoot集成redis集群
    1、添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><!--过滤lettuce,使用jedis作为redis客户端--><exclusion&......
  • Spring-webflux简介及基本使用
    spring-webflux是spring在5.0版本后提供的一套响应式编程风格的web开发框架。这个框架包含了spring-framework和springmvc,它可以运行在Netty、Undertow以及3.1版本以上的Serlvet容器上。你可以在项目中同时使用spring-webmvc和spring-webflux,或者只用其中一个来开发web应用。......