jdbcTemplate如何交给spring管理一遍文章让你快速了解

spring是一个非常优秀的轻量级框架,spring的ioc更是解决了代码耦合度的难题,那么如何让jdbcTemplate交给spring管理呢?方法虽然简单,但是这个整合交给spring管理的思想确是一个很大提升

首先写一个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" xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
       <context:component-scan base-package="com.lwh"></context:component-scan>
       <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
           //配置jdbcTemplate 交给spring容器管理
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
           //给dataSource赋值依赖注入
       <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:///spring"></property>
              <property name="password" value="root"></property>
              <property name="username" value="root"></property>
       </bean>

</beans>

然后写一个jdbcTemplateDemo测试类

public class JdbcTemplateDemo {
    public static void main(String[] args) {
        ApplicationContext ap=new ClassPathXmlApplicationContext("bean.xml");
        JdbcTemplate jt = (JdbcTemplate) ap.getBean("jdbcTemplate");
        jt.execute("insert into account1(username,balance)values('dd',1000)");
        //spring 自带的封装类
        List<Account> accounts1= jt.query("select * from account1 where id >?",new BeanPropertyRowMapper<Account>(Account.class),10);
        System.out.println(accounts1.isEmpty()?"kong":accounts1.get(0));
        //自己写的封装类
        List<Account> accounts= jt.query("select * from account1 where id >?",new AccountRowMappper(),1);
        for (Account account:accounts1){
           System.out.println(account);
       }
    }

}
class  AccountRowMappper implements RowMapper<Account>{

    @Override
    //写一个封装account的方法
    public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account =new Account();
        account.setId(resultSet.getInt("id"));
        account.setUsername(resultSet.getString("username"));
        account.setBalance(resultSet.getDouble("balance"));
        return account;
    }
}

这是结果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值