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);
    
    
        }
    }
  • 相关阅读:
    spring bean的生命周期
    02-MySQL主要配置文件
    01-MySQL Linux安装
    spring自动装配
    JVMGC+Spring Boot生产部署和调参优化
    java面试-生产环境出现CPU占用过高,谈谈你的分析思路和定位
    java面试-G1垃圾收集器
    java面试-垃圾回收器谈谈你的理解
    java面试-谈谈你对OOM的理解
    RPC介绍以及编程
  • 原文地址:https://www.cnblogs.com/cqyp/p/12515696.html
Copyright © 2011-2022 走看看