Spring JdbcTemplate的使用

 Spring对JDBC进行了封装,降低了JDBC的使用难度。

Spring JDBC的核心类是JdbcTemplate。

 

 


 

 

Spring JDBC的使用步骤

1、添加需要的jar包

  • spring-jdbc.RELEASE.jar、spring-tx.RELEASE.jar     

即使我们不手动管理事务,也需要添加spring-tx.RELEASE.jar,因为spring-jdbc.RELEASE.jar的源码中要使用spring-tx.RELEASE.jar的事务管理,

 

  • 数据库驱动

 

  • 如果要使用c3p0连接池 ,需添加c3p0的2个jar包(如果是oracle,需添加3个jar包)。

 

  • 如果要使用dbcp连接池,需添加commons-dbcp.jar(dbcp本身的jar包)、commons-pool.jar(dbcp的依赖包)。

 

 

2、在xml中配置数据源、JdbcTemplate

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!-- 配置数据源 -->

    <!-- 不使用连接池 -->
    <bean name="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1/my_db?serverTimezone=GMT" />
        <property name="username" value="chy" />
        <property name="password" value="abcd" />
    </bean>

    <!-- 使用数据库驱动自带的连接池-->
    <!-- 此处使用的是mysql自带的连接池,使用的自然是mysql的驱动类,所以不配置driverClassName属性-->
    <bean name="mysqlConnectionPool" class="com.mysql.cj.jdbc.MysqlConnectionPoolDataSource">
        <property name="url" value="jdbc:mysql://127.0.0.1/my_db?serverTimezone=GMT" />
        <property name="user" value="chy" />
        <property name="password" value="abcd" />
        <property name="maxReconnects" value="30" />
    </bean>

    <!-- 使用dbcp -->
    <bean name="dbcpDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1/my_db?serverTimezone=GMT" />
        <property name="username" value="chy" />
        <property name="password" value="abcd" />
        <property name="initialSize" value="15" />
        <property name="maxTotal" value="30" />
        <property name="minIdle" value="2" />
        <property name="maxIdle" value="10" />
    </bean>

    <!-- 使用c3p0 -->
    <bean name="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/my_db?serverTimezone=GMT" />
        <property name="user" value="chy" />
        <property name="password" value="abcd" />
        <property name="maxPoolSize" value="30" />
        <property name="initialPoolSize" value="15" />
        <property name="minPoolSize" value="8" />
    </bean>

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

可以使用<property>注入,也可以用<constructor-arg>注入。

 

 

3、使用JdbcTemplate

    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
        //获取JdbcTemplate
        JdbcTemplate jdbcTemplate=applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        String sql="select name from student_tb where id = 1";
        String name=jdbcTemplate.queryForObject(sql,String.class);
        System.out.println(name);

 

 


 

 

从properties文件读取数据库配置

把数据库的连接信息写在spring配置文件中,修改起来很麻烦。

通常把数据库的配置信息单独写在一个properties文件中,然后在xml中引入properties配置文件

 

src下新建db.properties:

#dbcp数据源配置
dbcp_driverClassName=com.mysql.cj.jdbc.Driver
dbcp_url=jdbc:mysql://localhost:3306/my_db?serverTimezone=GMT
dbcp_username=chy
dbcp_password=abcd
dbcp_initialSize=15
dbcp_maxTotal=30
dbcp_minIdle=2
dbcp_maxIdle=10

#c3p0数据源配置
#......

注意key不能使用关键字。

 

 

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.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入properties配置文件 -->
    <context:property-placeholder location="db.properties" />
    
    <!-- dbcp -->
    <bean name="dbcpDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${dbcp_driverClassName}" />
        <property name="url" value="${dbcp_url}" />
        <property name="username" value="${dbcp_username}" />
        <property name="password" value="${dbcp_password}" />
        <property name="initialSize" value="${dbcp_initialSize}" />
        <property name="maxTotal" value="${dbcp_maxTotal}" />
        <property name="minIdle" value="${dbcp_minIdle}" />
        <property name="maxIdle" value="${dbcp_maxIdle}" />
    </bean>
    
    <!--配置JdbcTemplate-->
    <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入数据源-->
        <property name="dataSource" ref="dbcpDataSource" />
    </bean>
</beans>

以${key}的方式引用对应的value。

 

 


 

 

引入properties配置文件的2种方式

  • 方式一(推荐)

   <context:property-placeholder location="db.properties" />
    <!-- 有多个properties文件时,逗号分隔即可-->
    <context:property-placeholder location="1.properties,2.properties" />

 

 

  • 方式二

    <!-- bean的name不是必需的 -->
    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="location" value="db.properties" />
    </bean>
    <!-- 引入多个properties文件 -->
    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations">
                <list>
                    <value>db1.properties</value>
                    <value>db2.properties</value>
                </list>
        </property>
    </bean>

 

 


 

 

使用JdbcTemplate构建dao层

接口:

public interface UserDao {
    public String queryNameById(int id);
    //其它方法
    //.....
}

 

 

方式一

将JdbcTemplate作为成员变量,并提供对应的setter方法用于注入依赖:

@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public String queryNameById(int id) {
        String sql = "select name from user_tb where id=?";
        String name = jdbcTemplate.queryForObject(sql, String.class, id);
        return name;
    }

    //其它方法的实现
    //......
}

 

JdbcTemplate要作为依赖注入,需要在xml中配置JdbcTemplate:

<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入数据源-->
        <property name="dataSource" ref="dbcpDataSource" />
</bean>

 

 

 

方式二

继承JdbcDaoSupport:

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
    @Override
    public String queryNameById(int id) {
        String sql = "select name from user_tb where id=?";
        //调用基类JdbcDaoSupport中的方法获取JdbcTemplate
        JdbcTemplate jdbcTemplate = super.getJdbcTemplate();
        String name = jdbcTemplate.queryForObject(sql, String.class, id);
        return name;
    }

    //其它方法的实现
    //......
}

 

JdbcDaoSupport类需要注入一个DataSource类型的依赖,继承了JdbcDaoSupport自然也需要在xml中注入依赖:

<bean name="userDaoImpl" class="com.chy.dao.UserDaoImpl">
        <!--注入数据源-->
        <property name="dataSource" ref="dbcpDataSource" />
</bean>

注入数据源后,JdbcDaoSupport会自动创建JdbcTemplate的实例,我们直接  super.getJdbcTemplate()  获取JdbcTemplate即可,不用在xml中配置JdbcTemplate

 

posted @ 2019-07-17 15:08  chy_18883701161  阅读(676)  评论(0编辑  收藏  举报