Spring整合JdbcTemplate
V哥官网:http://www.vgxit.com
本文对应视频教程:http://www.vgxit.com/course/23
1,概述
我们之前之前也开发过做过很多JDBC查询数据库相关代码。我们可以看到。使用JDBC开发程序,我们是非常痛苦的。因为我们要经历创建连接,获取PreparedStatement,获取ResultSet,组装数据等各种一系列的问题。非常的麻烦。而Spring为我们提供的JdbcTemplate就为我们很好的提供了这一方式。但是V哥给大家说一下,这种JdbcTemplate的数据库编程的方式,在真的线上开发的时候,很少使用。大家还是喜欢使用mybatis,hibernate(jpa)这样的一些框架。
2,配置整合
1,引入对应的jar包:
<?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">
<parent>
<artifactId>spring.ktdm</artifactId>
<groupId>com.vgxit.learn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.vgxit.learn.spring.ktdm</groupId>
<artifactId>jdbctemplate</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mysq驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--引入jdbc驱动包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--引入对应的数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
</project>
2,配置对应的数据源,datasource.properties:
datasource.class=com.alibaba.druid.pool.DruidDataSource
datasource.url=jdbc:mysql://localhost:3306/mybatis.ktdm?serverTimezone=Asia/Shanghai
datasource.username=root
datasource.password=Abc@123456
datasource.initalSize=5
datasource.minIdel=5
maxActive=20
maxWait=3000
3,定义spring的配置文件,spring-cfg.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"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载数据源配置文件-->
<context:property-placeholder location="classpath:datasource.properties"/>
<!--注入数据源-->
<bean id="datasource" class="${datasource.class}">
<property name="driverClassName" value="${datasource.driver}"/>
<property name="url" value="${datasource.url}"/>
<property name="username" value="${datasource.username}"/>
<property name="password" value="${datasource.password}"/>
<property name="initialSize" value="${datasource.initalSize}"/>
<property name="minIdle" value="${datasource.minIdel}"/>
<property name="maxActive" value="${datasource.maxActive}"/>
<property name="maxWait" value="${datasource.maxWait}"/>
</bean>
<!--注入JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"/>
</bean>
</beans>
4,测试运行:
package com.vgxit.learn.spring.ktdm.jdbctemplate.test;
import com.vgxit.learn.spring.ktdm.jdbctemplate.po.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcTemplate001 {
/**
* 测试用JdbcTemplate查询数据
*/
private static void testSelect() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml");
String sql = "select * from user where id=1";
JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
//查询用户
User user = jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
/**
* 该方法会在数据库返回了查询结果之后调用,其作用就是返回数据库查询之后组装的数据
* @param resultSet 结果集
* @param i
* @return 封装的结果集
* @throws SQLException
*/
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = User.builder()
.id(resultSet.getInt("id"))
.name(resultSet.getString("name"))
.gender(resultSet.getShort("gender"))
.age(resultSet.getInt("age"))
.nickName(resultSet.getString("nick_name"))
.build();
return user;
}
});
System.out.println(user);
}
private static void testUpdate() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml");
String sql = "update user set name=? where id=?";
JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
//添加和删除的时候也是调用update方法
jdbcTemplate.update(sql, "李一桐", 1);
}
public static void main(String[] args) {
// testUpdate();
testSelect();
}
}