As a one-stop framework, Spring has enterprise-level solutions for all three layers of architecture in development, and the corresponding persistence layer solution is the Spring JdbcTemplate. Today we’ll cover the configuration and use of Spring JdbcTemplate in detail.

Project structure Drawing:

The first step is to introduce dependencies.

<properties>
	<! Character set utF-8 -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <! -- Manage version numbers -->
    <spring.version>4.2.4. RELEASE</spring.version>
    <mysql.version>5.1.32</mysql.version>
    <druid.version>1.0.9</druid.version>
    <slf4j.version>1.6.6</slf4j.version>
</properties>

<dependencies>
    <! The Lombox plugin is used to automatically generate getters and setters. It is not necessary to automatically generate getters and setters.
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.22</version>
        <scope>provided</scope>
    </dependency>
    <! -- Spring dependencies are automatically introduced into Spring jars based on the transitivity of the dependencies.
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <! JdbcTemplate jar package -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <! -- Spring-test dependencies, mainly used to integrate Spring and junit environments -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <! -- MySql driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    <! -- Druid connection pool, Ali open Source project
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
    </dependency>
     <! -- Junit4 dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <! -- log4j log -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
    	<! -- build environment, JDK8-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code

The second step is to write the Spring JdbcTemplate configuration file.


      
<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<! Insert dataSource into JdbcTemplate
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

	<! DataSource = 'dataSource';
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///db_database08"/>
        <property name="username" value="root"/>
        <property name="password" value="admin"/>
    </bean>

	<! -- To configure annotation scanning, we put the DAO layer implementation in com.hrp.dao.impl package -->
    <context:component-scan base-package="com.hrp.dao.impl"/>
</beans>
Copy the code

Step 3: Prepare the log4J log configuration file.

# Global logging configuration
log4j.rootLogger=info, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Copy the code

Step 4: Write the entity class.

package com.hrp.domain;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private String password;
}
Copy the code

@data is lombox’s annotation to generate getters and setters. If you don’t have the plugin installed, you can write getters and setters by hand and the effect is the same.

Step 5: Write Dao layer interfaces and implementation classes.

Interface:

package com.hrp.dao;

import com.hrp.domain.User;

import java.util.List;

/ * * *@author hrp
 */
public interface UserDao {

    /** * Query all users *@return* /
    List<User> findAll(a);
}
Copy the code

Implementation class:

package com.hrp.dao.impl;

import com.hrp.dao.UserDao;
import com.hrp.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/ * * *@author hrp
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<User> findAll(a) {
        return jdbcTemplate.query("select * from tb_user".newBeanPropertyRowMapper<User>(User.class)); }}Copy the code

Step 6: Write test classes and integrate the Spring and junit environments.

package com.hrp.test;

import com.hrp.dao.UserDao;
import com.hrp.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class UserDaoTest {

    @Autowired
    private UserDao userDao;

    @Test
    public void findAllTest(a){
        List<User> list = userDao.findAll();
        for(User user : list) { System.out.println(user); }}}Copy the code

Running results: