其实JdbcTemplate有点类似于我们之前使用的jdbcutils工具,在JdbcTemplate中,操作数据库的功能Spring都封装好了,我们只要利用IoC操作注入JdbcTemplate对象即可使用。先用一个小案例来演示用法。
账户实体类
package com.domain;
import java.io.Serializable;
//账户的实体类
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
IAccountDao接口类
package com.dao;
import com.domain.Account;
//账户的持久层接口
public interface IAccountDao {
Account findAccountById(int id);
Account findAccountByname(String name);
void updateAccount(Account account);
}
IAccountDao实现类
package com.dao.impl;
import com.dao.IAccountDao;
import com.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
//账户的持久层实现类
public class AccountDaoImpl implements IAccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public Account findAccountById(int id) {
List<Account> accountList = jdbcTemplate.query("select * from account where id = ?" , new BeanPropertyRowMapper<Account>(Account.class) , id);
return accountList.isEmpty()? null : accountList.get(0);
}
public Account findAccountByname(String name) {
List<Account> accountList = jdbcTemplate.query("select * from account where name = ? " , new BeanPropertyRowMapper<Account>(Account.class) , name);
if(accountList.isEmpty()){
return null;
}
if(accountList.size() > 1){
throw new RuntimeException("结果集不唯一");
}
return accountList.get(0);
}
public void updateAccount(Account account) {
jdbcTemplate.update("update account set name = ? , money = ? where id = ?" , account.getName() , account.getMoney() , account.getId());
}
}
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置AccountDao-->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--配置jdbctemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
</beans>
测试类
package jdbctemplate;
import com.dao.IAccountDao;
import com.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//JdbcTemplate的最基本用法
public class JdbcTemplateDemo4 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
IAccountDao iAccountDao = applicationContext.getBean("accountDao" , IAccountDao.class);
Account account = iAccountDao.findAccountById(1);
System.out.println(account);
}
}
测试结果

查看测试结果就可以看到,我们使用了JdbcTemplate工具去进行根据id查询账户。那么注意以下几点:
①增删改都可以使用jdbcTemplate中封装的update方法,因为增删改的操作,其中参数只需要要执行的SQL语句以及传入的参数。
②查询使用query方法,其中参数为:要执行的SQL语句、封装返回的结果集以及在SQL语句中需要传入的参数。
③返回结果集可以使用JdbcTemplate封装的BeanPropertyRowMapper,泛型选择你要返回的对象类型,参数选择返回值类型对象的实体类字节码。也可以自己编写返回值对象结果集,如下所示:
//定义Account的封装策略
class AccountRoleMapper implements RowMapper<Account>{
//把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setName(resultSet.getString("name"));
account.setMoney(resultSet.getFloat("money"));
return account;
}
这样将返回的结果集修改一下即可:
List<Account> accounts = jdbcTemplate.query("select * from account where money > ?" , new AccountRoleMapper(),"900f");

480

被折叠的 条评论
为什么被折叠?



