zoukankan      html  css  js  c++  java
  • JdbcTemplate在spring中的使用

    一、spring中内置的数据源

             DriverManagerDataSource

           JdbcTemplate的基础知识:https://www.cnblogs.com/cqyp/p/12433184.html

    二、CRUD操作

            

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    <!--DriverManagerDataSource:spring内置的数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///spring"/>
            <property name="username" value="root"/>
            <property name="password" value="123"/>
        </bean>

    public class JdbcTemplateDemo3 {
        public static void main(String[] args) {
    
            ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
            JdbcTemplate template = ac.getBean("jdbcTemplate", JdbcTemplate.class);
            //保存
            //template.execute("insert into account (name,money) values (?,?)","eee","456");
             //更新
            //template.update("update account set name=?,money=? where id=?","ccc","133",5);
           //删除
            //template.update("delete from account where id=?",5);
             //查询所有
            List<Account> accountList = template.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
             accountList.forEach(account -> System.out.println(account));
           //按id查询
            List<Account> account = template.query("select * from account where id=?", new BeanPropertyRowMapper<Account>(Account.class), 5);
            System.out.println(account.get(0));
            //查询数量
    
            Integer integer = template.queryForObject("select count(id) from account", Integer.class);
    
    
        }
    }
  • 相关阅读:
    Swift
    Swift
    Swift
    Swift
    Cocos2d Lua 越来越小样本 内存游戏
    Android组件系列----ContentProvider内容提供商【5】
    看你的门-攻击服务器(4)-HTTP参数注入攻击
    图片缩放中心
    正确lua简单的扩展,可以加速相关C++数据。
    epoll()无论涉及wait队列分析
  • 原文地址:https://www.cnblogs.com/cqyp/p/12515696.html
Copyright © 2011-2022 走看看