Spring JdbcTemplate的使用
- 1 JdbcTemplate开发步骤
- 2 JdbcTemplate常用操作
- 2.1 更新、删除操作
- 2.2 查询操作
1 JdbcTemplate开发步骤
① 导入spring-jdbc和spring-tx坐标
② 创建数据库表和实体
③ 创建JdbcTemplate对象
④ 执行数据库操作
① 导入spring-jdbc和spring-tx坐标基本包以及连接池数据库等坐标
<dependency>
<!-- 导入spring坐标-->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
② 创建数据库表和实体
CREATE TABLE account (
name VARCHAR ( 10 ),
money DOUBLE
)
public class Account {
private String name;
private String money;
public void setName(String name) {
this.name = name;
}
public void setMoney(String money) {
this.money = money;
}
public String getName() {
return name;
}
public String getMoney() {
return money;
}
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", money='" + money + '\'' +
'}';
}
}
③ 创建JdbcTemplate对象:这里使用C3P0数据库连接池
在applicationContext.xml中配置数据源对象等:
<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:jdbc.properties"/>
<!-- 数据源对象 ComboPooledDataSource-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- JDBC模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
④ 执行数据库操作
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
//测试spring产生jdbc模板
public void test1() {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
int row = jdbcTemplate.update("insert into account values (?,?)", "lisi", 1000);
System.out.println(row);
}
}
2 JdbcTemplate常用操作
使用spring进行测试:
1) 导入spring-test坐标
2) (SpringJUnit4ClassRunner.class)
3) ("classpath:applicationContext.xml")
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2.1 更新、删除操作
SpringJUnit4ClassRunner.class)
("classpath:applicationContext.xml")
public class SpringTest {
//注入
private JdbcTemplate jdbcTemplate;
public void testUpdate() {
jdbcTemplate.update("update account set money = ? where name = ?", 200, "lisi");
}
public void testDelete() {
jdbcTemplate.update("delete from account where name = ?", "lisi");
}
}
(
2.2 查询操作
SpringJUnit4ClassRunner.class)
("classpath:applicationContext.xml")
public class SpringTest {
//注入
private JdbcTemplate jdbcTemplate;
public void testQueryOne() {
//查询单个
Account query = jdbcTemplate.queryForObject("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), "黄蓉");
System.out.println(query);
}
public void testQueryAll() {
//查询全部
List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(accountList);
}
public void testQueryAccount() {
//查询总数
Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
}
(