zoukankan      html  css  js  c++  java
  • spring系统学习--4 Spring:JDBC Template

    1.1.JdbcTemplate概述

    他是Spring框架中提供的一个对象:是对原始JDBC API对象的简单封装。Spring框架为我们提供了很多的操作模板类。

      操作关系型数据库的:

        JdbcTemplate

        Hibernate Template
      操作nosql数据库的:
        RedisTemplate
      操作消息队列的:

        jmsTemplate
    我们今天的主角在spring-jdbc-5.0.2.RELEASE.jar中,我们在导包的时候,除了要导入这个jar包外,还需要导入一个spring-tx-5.0.2.RELEASE.jar(它是和事务相关的)。

     1.2原始用法

    public class JdbcTemplateDemo01 {
        public static void main(String[] args) throws PropertyVetoException {
    //        获取JdbcTemplate对象
            JdbcTemplate jt = new JdbcTemplate();
    //        给JdbcTemplate对象设置数据源
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring5");
            dataSource.setUser("root");
            dataSource.setPassword("admin123");
            jt.setDataSource(dataSource);
            jt.execute("insert into account (name,money)values ('template',1000)");
        }
    }

    1.3 Spring配置数据源用法

    <?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="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring5"></property>
            <property name="user" value="root"></property>
            <property name="password" value="admin123"></property>
        </bean>
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="comboPooledDataSource"></property>
        </bean>
    </beans>
    public class JdbcTemplateDemo02 {
        public static void main(String[] args) throws PropertyVetoException {
    //        获取Spring容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config/bean.xml");
            JdbcTemplate jt = (JdbcTemplate) context.getBean("jdbcTemplate");
            jt.execute("insert into account (name,money)values ('template1',10888)");
            context.close();
        }
    }

    1.4增删改查

    public class JdbcTemplateDemo02 {
        public static void main(String[] args) throws PropertyVetoException {
            /**
             *JdbcTemplate的CRUD操作
             * 用于增删改的方法:update(string sql,object..args);
             * 参数的含义:
             * String sq1:要执行sq1语句。该语句可以有占位符。占位符用问号替代。
             * *Object...args:当前执行语句所需的参数。
             */
    //        1.获取Spring容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    //        2.获取对象
            JdbcTemplate jt = (JdbcTemplate) context.getBean("jdbcTemplate");
    //        3.执行操作
    //            插入数据
    //        jt.update("insert into account (name ,money) values (?,?)","update",666);
    //            删除数据
    //        jt.update("delete from account where name =?","update");
    //            更新数据
    //        jt.update("update account set money=888 where name =?", "aaa");
    //            查询数据
    //                 查询所有(同时适用于查询一个)
            List<Account> query = jt.query("select * from account where money >?", new RowMapper() {
                @Override
                public Account mapRow(ResultSet resultSet, int i) throws SQLException {
                    Account account = new Account();
                    account.setId(resultSet.getInt("id"));
                    account.setName(resultSet.getString("name"));
                    account.setMoney(resultSet.getFloat("money"));
                    return account;
                }
            }, 888);
            for (Account account : query) {
                System.out.println(account);
            }
            System.out.println("*****************");
            /**
             * spring封装好的查询一个和多个的方法,但是有使用条件
             * BeanPropertyRowMapper的使用要求:
             * 要求:实体类中的set方法和数据库表中的列名保持一致。
             * setName =name
             */
            List<Account> list2 = jt.query("select * from account where money >?", new BeanPropertyRowMapper<Account>(Account.class), 888);
            for (Account account : list2) {
                System.out.println(account);
            }
    //        查询返回一行一列(聚合查询)
            Integer integer = jt.queryForObject("select count(*)from account where money>?", Integer.class, 888);
            System.out.println(integer);
            context.close();
        }
    }

     1.5 Spring内置的数据源(不用导其他jar了)

    <!--    创建数据源的bean:使用ComboPooledDataSource数据源需要导入外部jar包:c3p0-->
        <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring5"></property>
            <property name="user" value="root"></property>
            <property name="password" value="admin123"></property>
        </bean>
    <!--    创建另外一种数据源的bean:使用BasicDataSource数据源需要导入外部jar包commons-dbcp和commons-pool;-->
        <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/spring5"></property>
            <property name="username" value="root"></property>
            <property name="password" value="admin123"></property>
        </bean>
    <!--    spring内置数据源-->
    <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring5"></property>
        <property name="username" value="root"></property>
        <property name="password" value="admin123"></property>
    </bean>

      

  • 相关阅读:
    leetcode-Rotate Image
    leetcode- Rotate Array
    leetcode- Remove Element
    项目小结
    java到底有哪些重要知识点???
    js学习笔记 -- await/ async
    js学习笔记 -- Promise
    js学习笔记 -- 函数
    js学习笔记 -- 随记
    4、栈的实现:顺序存储和链式存储
  • 原文地址:https://www.cnblogs.com/luzhanshi/p/13237068.html
Copyright © 2011-2022 走看看