美文网首页
Spring JdbcTemplate + Druid数据源

Spring JdbcTemplate + Druid数据源

作者: 上杉丶零 | 来源:发表于2019-04-03 13:08 被阅读0次
  1. 导入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.21</version>
</dependency>
  1. jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/dxbase?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=root
  1. jdbcContext.xml
<?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-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  <!-- 引入外部属性文件 -->
  <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

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

  <!-- 配置Spirng的JdbcTemplate并指定数据源 -->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

  <!-- 配置NamedParameterJdbcTemplate, 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 -->
  <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg ref="dataSource"></constructor-arg>
  </bean>

  <!-- 配置JDBCUtil -->
  <bean id="jdbcUtil" class="org.fas.util.JDBCUtil">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    <property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
  </bean>
</beans>
  1. JDBCUtil
package org.fas.util;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.List;
import java.util.Map;

/**
 * JDBC工具类
 *
 * @author Leif Liu
 * @since 2020/3/31
 */
public class JDBCUtil {
    private static final AbstractApplicationContext ABSTRACT_APPLICATION_CONTEXT = new ClassPathXmlApplicationContext("jdbcContext.xml");

    private JDBCUtil() {}

    @Getter
    @Setter
    private JdbcTemplate jdbcTemplate;

    @Getter
    @Setter
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public static JDBCUtil getBean() {
        return (JDBCUtil) ABSTRACT_APPLICATION_CONTEXT.getBean("jdbcUtil");
    }

    public static void closeContext() {
        ABSTRACT_APPLICATION_CONTEXT.close();
    }

    public <T> List<T> doDQL(String sql, Class<T> t, Object... args) {
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t), args);
    }

    public int doDML(String sql, Object... args) {
        return jdbcTemplate.update(sql, args);
    }

    public <T> List<T> queryForList(String sql, SqlParameterSource sqlParameterSource, Class<T> t) {
        return JSON.parseArray(JSON.toJSONString(namedParameterJdbcTemplate.queryForList(sql, sqlParameterSource)), t);
    }

    public <T> List<T> queryForList(String sql, Map<String, ?> parameterMap, Class<T> t) {
        return JSON.parseArray(JSON.toJSONString(namedParameterJdbcTemplate.queryForList(sql, parameterMap)), t);
    }
}

相关文章

网友评论

      本文标题:Spring JdbcTemplate + Druid数据源

      本文链接:https://www.haomeiwen.com/subject/fngabqtx.html