其他分享
首页 > 其他分享> > JdbcTemplate的使用

JdbcTemplate的使用

作者:互联网

在Spring 中可以使用 JdbcTemplate 进行数据库的管理操作。
Spring框架对JDBC 进行独立封装。很方便。

JdbcTemplate 的使用

导入依赖包

<?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>Spring</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>SpringDemo1</module>
        <module>springDemo2</module>
        <module>springDemo3</module>
        <module>springDemo4</module>
        <module>springDemo5</module>
        <module>springDemo6</module>
        <module>springDemo7</module>
        <module>springDemo8</module>
        <module>springDemo9</module>
        <module>springAop</module>
        <module>springaop2</module>
        <module>springAop3</module>
        <module>springAop4</module>
        <module>jdbc_Template</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

<!--        Spring的依赖包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>
<!--        mysql的依赖包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>9.1</version>
        </dependency>
    </dependencies>

</project>

配置jdbc 的文件 jdbc.properties
在Spring配置文件中 配置 数据库的连接池
配置 JdbcTemplate 对象,注入数据源

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.momo.dao,com.momo.service"/>

<!--    导入外部的配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

<!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

<!--    配置 JdbcTemplate 对象,注入数据源-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

实体类

package com.momo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private int id;
    private String name;
    private String pwd;
}

dao层:

package com.momo.dao;

import com.momo.entity.User;

import javax.rmi.CORBA.Stub;
import java.util.List;

public interface UserDao {
    boolean save(User user);


    List<User> findAll();

    // 批量的增加
    public void bathAdd(List<Object[]> users);

}

package com.momo.dao.impl;


import com.momo.dao.UserDao;
import com.momo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean save(User user) {
        String sql = "insert into user(name,pwd) values(?,?)";
        // 增 删 改
        int i = jdbcTemplate.update(sql, user.getName(), user.getPwd());
        return i>0;
    }

    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        System.out.println("=====>"+jdbcTemplate);
        // 只能做查询
        List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return query;
    }

    @Override
    public void bathAdd(List<Object[]> users) {
        String sql =  "insert into user(name,pwd) values(?,?)";
        // 批量的增删改
        int[] ints = jdbcTemplate.batchUpdate(sql, users);
        System.out.println("=====>"+ints.length);
    }
}

service层

package com.momo.service;

import com.momo.entity.User;

import java.util.List;

public interface UserService {
    boolean save(User user);
    List<User> findAll();
    public void bathAdd(List<Object[]> users);
}

package com.momo.service.impl;

import com.momo.dao.UserDao;
import com.momo.entity.User;
import com.momo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean save(User user) {
        return userDao.save(user);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public void bathAdd(List<Object[]> users) {
        userDao.bathAdd(users);
    }
}

测试

package com.momo.test;

import com.momo.entity.User;
import com.momo.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:springContext.xml"})
public class MyTest {

    @Autowired
    private UserService userService;

    @Test
    public void test(){
        System.out.println("=====>"+userService);
        List<User> all = userService.findAll();

        all.forEach(System.out::println);
    }


    @Test
    public void  Test(){

        boolean flag = userService.save(new User(0, "王五", "123321"));

        System.out.println(flag);
    }


    @Test
    public void bathTest(){
        List<Object[]> list = new ArrayList<>();

        Object[] o1 = {"张三","123456"};
        Object[] o2 = {"李四","123456"};
        Object[] o3 = {"王五","123456"};

        list.add(o1);
        list.add(o2);
        list.add(o3);
        userService.bathAdd(list);
    }
}

标签:使用,List,springframework,JdbcTemplate,org,import,com,public
来源: https://blog.csdn.net/m0_46569959/article/details/122768385