Spring(五)JdbcTemplate

Config.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: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/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <context:property-placeholder location="jdbc.properties"/>



    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=

测试数据库更新操作

@Test//Spring配置的jdbctemplate
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Config.xml");
        JdbcTemplate jdbcTemplate= (JdbcTemplate) context.getBean("jdbcTemplate");

        //其返回值是受影响的行数
        System.out.println(jdbcTemplate.update("delete from account where name='李明'"));
        jdbcTemplate.update("insert into account values (?,?)","张飞",9000);
        jdbcTemplate.update("update account set money=900 where name='张飞'");
    }

查询用另一种测试方法Junit
查询列表

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:Config.xml")
public class junitText {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testQuery(){
        List<Account> list = jdbcTemplate.query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
        Iterator it = list.iterator();
        while (it.hasNext())
        System.out.println(it.next());
    }
}

结果
在这里插入图片描述
查询单个结果

  @Test
    public void testQueryOne(){
        Account tom= jdbcTemplate.queryForObject("select * from account where name=?",new BeanPropertyRowMapper<Account>(Account.class),"关羽");
        System.out.println(tom);
    }

查询基本数据类型

@Test
    public void testQueryCount(){
        System.out.println("总共有"+jdbcTemplate.queryForObject("select count(*) from account",Integer.class)+"条数据");
    }

关键点有:
jdbctemplate.setDatasource() //可交给spring容器
以及:
jdbctemplate.query() //查列表
jdbctemplate.queryForObject() //查单个
jdbctemplate.update(); //增删改,返回值为操作行数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值