Spring中的JdbcTemplate,JdbcDaoSupport

使用

1.JDBCTemplate基本使用

1.编写实体类Account,在bean.xml中配置,DriverManagerDataSource是Spring自带的数据源,不用导包
(实际应用还可参照下面更方便高级)

<!-- 配置账户的持久层-->
<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>

获取和常用的使用方法。
注意new BeanPropertyRowMapper(Account.class),是spring自带的,让我们更方便的的获取数据并返回。

JdbcTemplate jdbcTemplate;

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

    /**
     *查询,返回List<>
     */
    public List<User> findAll() {
        List<User> users=jdbcTemplate.query("select * from user where id > ?", new BeanPropertyRowMapper<User>(User.class),0);
        return users;
    }
    /**
     * queryForObject
     */
    public int count() {
        return jdbcTemplate.queryForObject("select count(*) from user where id > ?",Integer.class,1);
    }
    /**
     * 更新,增删改
     */
    public void update(User user) {
        jdbcTemplate.update("update user set name= ? , password= ? where id = ?",user.getName(),user.getPassword(),user.getId());
    }

2.JdbcDaoSupport

如果有多个Dao继承要配Jdbc 那么就有很多重复的代码,将Jdbc部分提取出来。
这是Spring封装好的父类,Dao直接继承应用。

XML配置同上

注意 AccountDaoImpl继承JdbcDaoSupport后,就不能用注解的方式来配置AccountDaoImpl到IOC了,那就用XML配置方式配置了。

super.getJdbcTemplate() 来获取JdbcTemplate对象

public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {

    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);
    }

    @Override
    public Account findAccountByName(String accountName) {
        List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
        if(accounts.isEmpty()){
            return null;
        }
        if(accounts.size()>1){
            throw new RuntimeException("结果集不唯一");
        }
        return accounts.get(0);
    }

    @Override
    public void updateAccount(Account account) {
        super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值