什么是springjdbcTemplate
springjdbcTemplate 是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。通常我们可以理解其为操作jdbc的一个强大工具,
如何使用springjdbcTemplate
- 首先需要建立依赖库,即导入jar包坐标
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.common</groupId>
<artifactId>common-io</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
</dependencies>
- 数据库配置文件
c3p0.driverDriver=com.mysql.jdbc.Driver
c3p0.url=jdbc:mysql://localhost:3306/test
c3p0.user=root
c3p0.password=root
c3p0.initialSize=5
c3p0.maxActive=10
c3p0.maxWait=3000
- 编写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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载外部的properties文件-->
<context:property-placeholder location="c3p0.properties"/>
<!--数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${c3p0.driverDriver}"/>
<property name="jdbcUrl" value="${c3p0.url}"/>
<property name="user" value="${c3p0.user}"/>
<property name="password" value="${c3p0.password}"/>
</bean>
<!--jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
account类
public class Account {
private String name;
private double balance;
public Account() {
}
public Account(String name, double balance) {
this.name = name;
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", balance=" + balance +
'}';
}
}
test类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JDBC01Test {
//@Resource(name = "jdbcTemplate")
@Autowired
@Qualifier("jdbcTemplate")
JdbcTemplate jdbcTemplate = new JdbcTemplate();
@Test
public void test01(){
String sql = "insert into account(name,balance) values (?,?) ";
jdbcTemplate.update(sql,"jack",9000);
System.out.println(jdbcTemplate);
}
@Test
public void test02(){
String sql = "update account set balance = ? where name = ? ";
int row = jdbcTemplate.update(sql, 100000, "tom");
System.out.println(row);
}
@Test
public void test03(){
String sql = "delete from account where name = ? ";
int row = jdbcTemplate.update(sql, "jack");
System.out.println(row);
}
@Test
public void test04(){
String sql = "select * from account ";
List<Account> query = jdbcTemplate.query(sql,
new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(query);
}
@Test
public void test05(){
String sql = " select * from account where name = ?";
Account account = jdbcTemplate.queryForObject(sql,
new BeanPropertyRowMapper<Account>(Account.class), "tom");
System.out.println(account);
}
@Test
public void test06(){
String sql = " select count(*) from account ";
Long count = jdbcTemplate.queryForObject(sql, long.class);
System.out.println(count);
}
}
经测试上述功能完美运行,springjdbcTemplate完美运行。