jdbcTemplate的CRUD操作

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
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

测试类:

/**
 * 定义Account的封装策略
 */
class AccountRowMapper implements RowMapper<Account>{
    /**
     * 把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中
     * @param resultSet
     * @param i
     * @return
     * @throws SQLException
     */
    @Override
    public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account=new Account();
        account.setId(resultSet.getInt("id"));
        account.setUid(resultSet.getInt("uid"));
        account.setMoney(resultSet.getFloat("money"));
        return account;
    }
}

public class jdbcTemplateDemo {

    @Test
    public void testJdbcTemplateDemo(){
        //获取容器
        ApplicationContext factory=new ClassPathXmlApplicationContext("bean.xml");
        //获取对象
        JdbcTemplate jt=factory.getBean("jdbcTemplate",JdbcTemplate.class);
        //执行操作
        //保存
        //jt.update("insert into account(uid,money) values(?,?)",44,60);
        //更新
        //jt.update("update account set uid=?,money=? where id=?",43,99,1);
        //删除
        //jt.update("delete  from account where id=?",1);
        //查询所有
        //List<Account> accounts=jt.query("select * from account where money>?",new AccountRowMapper(),50f);
        //用spring提供的方法
        // List<Account> accounts=jt.query("select * from account where money>?",new BeanPropertyRowMapper<Account>(Account.class),50f);
        //System.out.println(accounts);
        //查询一个
        //List<Account> account=jt.query("select * from account where id=?",new BeanPropertyRowMapper<Account>(Account.class),2);
        //System.out.println(account.isEmpty()?"没有内容":account.get(0));
        //返回一行一列(使用聚合函数,但不加group by子句)
        Long count=jt.queryForObject("select count(1) from account where money>?",Long.class,50f);
        System.out.println(count);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java后端指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值