1. Create a Maven project import dependency


      
<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>spring-jdbc</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.5. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.5. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
    </dependencies>

</project>
Copy the code

2. Write the Account entity class and create the database table

2.1 Creating an Account Entity Class

package com.abchina.domain;

public class Account {
    private String name;
    private double money;

    public String getName(a) {
        return name;
    }

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

    public double getMoney(a) {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString(a) {
        return "Account{" +
                "name='" + name + '\' ' +
                ", money=" + money +
                '} '; }}Copy the code

Table 2.2 built

/* Navicat Premium Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Source Server Version : 50719 Source Host : localhost:3306 Source Schema : test Target Server Type : MySQL Target Server Version : 50719 File Encoding : 65001 Date: 25/04/2021 14:34:22 */

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for account
-- ----------------------------
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account`  (
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `money` double NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;
Copy the code

3. Write the Spring core configuration file and configure the data source and jdbcTemplate


      
<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"
       xmlns:con="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>
Copy the code

4. Write test classes

package com.abchina.test;

import com.abchina.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestJdbc {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testInsert(a) {
        jdbcTemplate.update("insert into account values (? ,?) "."zhangsan".1000);
    }

    @Test
    public void testUpdate(a) {
        jdbcTemplate.update("update account set money=? where name=?".2000."zhangsan");
    }

    @Test
    public void testQueryAll(a) {
        List<Account> accountList = jdbcTemplate.query("select * from account".new BeanPropertyRowMapper<Account>(Account.class));
        for(Account account : accountList) { System.out.println(account); }}@Test
    public void delete(a) {
        int ret = jdbcTemplate.update("delete from account where name=?"."zhangsan");
    }

    @Test
    public void QueryOne(a) {
        List<Account> accountList = jdbcTemplate.query("select * from account where name=?".new BeanPropertyRowMapper<Account>(Account.class), "zhangsan");
        for(Account account : accountList) { System.out.println(account); }}@Test
    public void testAccount(a) {
        Integer sumCount = jdbcTemplate.queryForObject("select count(*) from account", Integer.class); System.out.println(sumCount); }}Copy the code

5. The source code

Notice: This is a simple test of the jdbcTemplate. There are no corresponding update class operations and no transaction control