JDBC是Spring数据访问/集成中的最重要模块。
1. Spring JDBC的配置
Spring JDBC模块主要由4个包组成:
包名 | 说明 |
---|---|
core | 包含了JDBC的核心功能,例如JdbcTemplate等 |
dataSource | 访问数据源的实用工具类 |
object | 以面向对象的方式访问数据库 |
support | 包含了core和object包的支持类 |
2. 使用Spring JdbcTemplate方法
针对数据库的操作,Spring框架提供了JdbcTemplate类,该类是Spring框架数据抽象层的基础,可以说,JdbcTemplate类是Spring JDBC的核心类。
2.1 execute()
- 使用的数据库是Mysql(版本为8.0的),创建一个名Spring的数据库
- 创建web项目,导入jar包
- 创建包(com.xhh.jdbc)创建配置文件applicationContext.xml,在该文件中配置id为dataSource的数据源Bean和id为jdbcTemplate的JDBC模板Bean,并将数据源注入到JDBC模板中。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">
<!-- 1 配置数据源,熟练使用 alt + / 有提示 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<!-- 连接数据库的url -->
<property name="url" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC"></property>
<!-- 连接数据库的用户名 -->
<property name="username" value="root"></property>
<!-- 连接数据库的密码 -->
<property name="password" value="025431"></property>
</bean>
<!-- 2 配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
上述代码使用到了设值注入,Spring的依赖注入(DI)。在前两篇已举例。
-
快速找到org.springframework.jdbc.datasource.DriverManagerDataSource并复制
-
忘记
<property name="" ></property>
中应该填什么值?,ctrl + 鼠标左键进入源码中查看需要的setter
配置JDBC模板类似。 -
在包中创建类JdbcTemplateTest
使用单元测试
public class JdbcTemplateTest {
@Test
public void mainTest() {
// 加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//获取JdbcTemplate实例
JdbcTemplate jdTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
//使用execute()方法执行SQL语句,创建用户账号管理表account
jdTemplate.execute("create table account("
+ "id int primary key auto_increment,"
+ "username varchar(50),"
+ "balance double)");
System.out.println("账号表account创建成功");
}
}
快捷键alt + shift + x + t 运行。
2.2 update(),query()
- 在包中创建Account类,定义上述创建表中的属性,使用alt + s + r 快速创建getter/setter。
Account.java
package com.xhh.jdbc;
public class Account {
private int id;
private String username;
private double balance;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
}
}
- 创建接口AccountDao,定义增删查改方法。
AccountDao.java
package com.xhh.jdbc;
import java.util.List;
public interface AccountDao {
// 添加用户
public int addAccount(Account account);
// 更新用户
public int updateAccount(Account account);
// 删除用户
public int deleteAccount(int id);
// 查询所有用户
public List<Account> findAllAccount();
}
- 创建实现类AccountDaoImpl,将JdbcTemplate模板通过依赖注入,并使用该模板实现增删查改。
AccountDaoImpl.java
package com.xhh.jdbc;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public int addAccount(Account account) {
String sql = "insert into account(username,balance) values(?,?)";
Object [] obj = {
account.getUsername(),
account.getBalance()
};
int update = jdbcTemplate.update(sql, obj);
return update;
}
@Override
public int updateAccount(Account account) {
String sql = "update account set username=?,balance=? where id=?";
Object [] obj = {
account.getUsername(),
account.getBalance(),
account.getId()
};
int update = jdbcTemplate.update(sql, obj);
return update;
}
@Override
public int deleteAccount(int id) {
String sql = "delete from account where id=?";
int update = jdbcTemplate.update(sql, id);
return update;
}
// 查询所有账户信息
@Override
public List<Account> findAllAccount() {
String sql = "select * from account";
RowMapper<Account> rowMapper =
new BeanPropertyRowMapper<Account>(Account.class);
return this.jdbcTemplate.query(sql, rowMapper);
}
}
- 在
applicationContext.xml
中定义一个accountDao的Bean,该Bean实现了将模板注入到accountDao实例中。
<!-- 定义Bean -->
<bean id="accountDao" class="com.xhh.jdbc.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
- 在测试类中添加测试方法
@Test
public void addTest() {
Account account = new Account();
account.setUsername("xhh");
account.setBalance(600.0);
int addAccount = accountDao.addAccount(account);
if (addAccount > 0) {
System.out.println("添加成功。。");
}
}
@Test
public void findAllTest() {
List<Account> findAllAccount = accountDao.findAllAccount();
for (Account account : findAllAccount) {
System.out.println(account.toString());
}
}
完成。