JdbcTemplate的基本使用笔记

本文详细介绍Spring框架中JdbcTemplate的配置与使用方法,包括在DAO层的集成、CRUD操作示例及去除重复配置的策略。通过XML配置与注解、继承JdbcDaoSupport类的方式,展示了如何在Spring中高效利用JdbcTemplate进行数据库交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先在bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="accountDao" class="com.xp.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <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/maven"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

 

JdbcTemplate在Dao中的使用

 

public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Account findAccountById(Integer id) {
       List<Account> accounts = jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class),id);
       return accounts.isEmpty()?null:accounts.get(0);
    }
public void updateAccount(Account account) {
    jdbcTemplate.update("update account set name =? ,money = ? where id = ?",account.getName(),account.getMoney(),account.getId());
}

 

test类:

public static void main(String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    AccountDao accountDao = ac.getBean("accountDao",AccountDao.class);
    Account a1 = accountDao.findAccountById(2);
}

JdbcTemplate在spring的ioc中使用

public static void main(String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    JdbcTemplate jdbcTemplate = ac.getBean("jdbcTemplate",JdbcTemplate.class);
    jdbcTemplate.execute("insert into account(name,money) values ('ddd',222)");
}

 

 

 

JdbcTemplate的CRUD操作

dbutils靠ResultSetHandler的返回值决定query方法的返回值类型

spring靠query方法的参数列表决定返回值类型


 

编写测试方法:

test类:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);


方式一:

//查询,自己编写结果集处理类 不常用
// List<Account> list = jt.query("select * from account where money>?", new AccountRowMapper(), 500);

//自己编写的结果集处理类↓

class AccountRowMapper implements RowMapper<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;
    }
}
方式二:

//用spring提供的类似与ResultSetHandler的结果集处理类,常用

 List<Account> list = jt.query("select * from account where money>?", new BeanPropertyRowMapper<Account>(Account.class), 500);
for (Account a : list){
     System.out.println(a);
}

//查询一个↓

List<Account> list = jt.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 1);
System.out.println(list.isEmpty()?"空":list.get(0));

 

//涉及聚集函数的查询

Long i  =jt.queryForObject("select count(*) from account where money>?", Long.class, 500);

 

 

 

去除Dao层重复配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 配置账户的持久层-->
    <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/eesy"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>
</beans>
/**
 * 账户的持久层实现类
 */
@Repository
public class AccountDaoImpl2 implements IAccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Account findAccountById(Integer accountId) {
        List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

 

 

二。继承JdbcDaoSupport类采用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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

       <!-- 配置账户的持久层-->

      <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
             <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/eesy"></property>

         <property name="username" value="root"></property>

         <property name="password" value="1234"></property>

   </bean>

</beans>

当继承JdbcDaoSupport 后,JdbcDaoSupport会自动创建JdbcTemplate

/**
 * 账户的持久层实现类
 */
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {

    @Override
    public Account findAccountById(Integer accountId) {
        List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值