Spring作为一个一站式框架,对开发中三层架构都有着企业级的解决方案,其中对应持久层的解决方案就是Spring JdbcTemplate。
今天就详细讲解Spring JdbcTemplate的配置与使用。
项目结构图:
第一步,引入依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <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> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies>
<build> <plugins> <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>
|
第二步,编写Spring JdbcTemplate的配置文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>
<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>
<context:component-scan base-package="com.hrp.dao.impl"/> </beans>
|
第三步:准备log4j日志的配置文件。
1 2 3 4 5 6
| # 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
|
第四步:编写实体类。
1 2 3 4 5 6 7 8 9 10
| package com.hrp.domain;
import lombok.Data;
@Data public class User { private Integer id; private String name; private String password; }
|
@Data是lombox的注解,用于生成getter和setter方法,如果没有安装这个插件的可以手写getter和setter方法,效果是一样的。
第五步:编写Dao层接口和实现类。
接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.hrp.dao;
import com.hrp.domain.User;
import java.util.List;
public interface UserDao {
List<User> findAll(); }
|
实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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;
@Repository public class UserDaoImpl implements UserDao {
@Autowired private JdbcTemplate jdbcTemplate;
@Override public List<User> findAll() { return jdbcTemplate.query("select * from tb_user",new BeanPropertyRowMapper<User>(User.class)); } }
|
第六步:编写测试类,整合Spring和junit环境。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 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(){ List<User> list = userDao.findAll(); for (User user : list) { System.out.println(user); } } }
|
运行结果: