zoukankan      html  css  js  c++  java
  • 14. Spring JdbcTemplate基本使用

    JdbcTemplate概述:

    它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作 模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操 作消息队列的JmsTemplate等等。

    JdbcTemplate开发步骤:

    ① 导入spring-jdbc和spring-tx坐标

    ② 创建数据库表和实体

    ③ 创建JdbcTemplate对象

    ④ 执行数据库操作

    确保有JDBC 和 内个 C3P0 的gav  下面要用到.

    ① 导入坐标

    <!--导入spring的jdbc坐标-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.0.5.RELEASE</version>
    </dependency>
    <!--导入spring的tx坐标-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.0.5.RELEASE</version>
    </dependency>

    ② 创建数据库表和对应的 JavaBean

    ③ 创建JdbcTemplate对象

    ④ 执行数据库操作

    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.junit.Test;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.beans.PropertyVetoException;
    
    public class TestJdbcTemplate {
        //这个是测试Class
    
        @Test
        public  void Test() throws PropertyVetoException {
            //新建一个C3P0的数据源 ,确保导入gav即可
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("com.mysql.jdbc.Driver"); //抛出一个异常
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc");
            dataSource.setUser("root");
            dataSource.setPassword("root");
    
            //new一个JdbcTemplate 模板对象
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            //设置数据源 这里的数据源我们用C3P0的吧 【数据源就是配置数据库】
            jdbcTemplate.setDataSource(dataSource);
            //执行语句 支持预编译 然后后面补充即可 和C语言的内个语法相似
            jdbcTemplate.update("insert into user values(?,?)","BiHu",18);
        }
    }

    所以 这就是这个SpringMVC的JDBC模板的基本操作(SQL语句)。 上面演示的是Updata ,他可以增删改,查询下面讲

    还可以让Spring 帮我们生成 这个JdbcTemplate  ,其实也学过,其实就是之前内中Spring配置中注入的,当然这个也可以...感觉好无语 一直学这些..

    首先创建一个Spring的配置文件 、

    我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将 数据源DataSource注入到JdbcTemplate模版对象中,配置如下:

    <?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">
    
            <!--配置C3P0-->
            <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="com.mysql.jdbc.Driver"/>
                <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jdbc"/>
                <property name="user" value="root"/>
                <property name="password" value="root"/>
            </bean>
        <!--配置 TestJdbcTemplate -->
            <bean id="TestJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
                <property name="dataSource" ref="c3p0"></property>
            </bean>
    </beans>

    下面是测试: Test.java:

    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.beans.PropertyVetoException;
    
    public class TestJdbcTemplate {
        //这个是测试Class
    
        @Test
        public  void Test() throws PropertyVetoException {
            ApplicationContext app = new ClassPathXmlApplicationContext("Application.xml");
            JdbcTemplate jdbcTemplate = (JdbcTemplate)app.getBean("TestJdbcTemplate");
            jdbcTemplate.update("insert into user values(?,?)","BIHU",18);
        }
    }
    View Code

    当然你也可以自己用内个 parameter 内个文件导入 然后用${jdbc.xx}  这样来读取 你怎么这样的话 解耦更高一点

    其实这个JDBC模板给我们的查询非常的方便,还可以直接封装到JavaBean 中:

    查询是 query  方法,封装是    BeanPropertyRowMapper  这个类 它实现了 RowMapper接口 ,其实翻译就是: 实体类属性行映射

    import com.bihu.Bean.User;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.beans.PropertyVetoException;
    import java.util.List;
    
    public class TestJdbcTemplate {
        //这个是测试Class
    
        @Test
        public  void Test() throws PropertyVetoException {
            //已经通过Spring文件配置好了内个JDBC模板对象
            ApplicationContext app  = new ClassPathXmlApplicationContext("Application.xml");
            JdbcTemplate JdbcTemplate = (JdbcTemplate)app.getBean("TestJdbcTemplate");
    
            //下面进行查询操作: 封装其实是这个类:BeanPropertyRowMapper 你可以去他内部看看,他有个泛型【封装的Bean类型】,参数是Bean类型的Class
            List<User> List = JdbcTemplate.query("select * from user", new BeanPropertyRowMapper<User>(User.class));
            //然后我们打印一下【由于已经ToString】
            for (User item : List){
                //你也可以直接:   System.out.println(list);
                System.out.println(item);
            }
        }
    }

    上面那个是实现查询全部的 返回额是 List<T> ,其实也可以预编译,他有很多重载的方法,其中包含了预编译(查询条件语句)你可以去看看 。

    下面就有查询单个的: 用法差不多 但是呢 他只能查询一个 查到多个会报错 ,他返回的是一个Bean:

    JdbcTemplate方法

    这个方法还有很多重载更方便【具体自己百度API即可】,支持 预编译 + 查询  = 查询某个数据然后 封装对象:

    import com.bihu.Bean.User;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.beans.PropertyVetoException;
    import java.util.List;
    
    public class TestJdbcTemplate {
        //这个是测试Class
    
        @Test
        public  void Test() throws PropertyVetoException {
            //已经通过Spring文件配置好了内个JDBC模板对象
            ApplicationContext app  = new ClassPathXmlApplicationContext("Application.xml");
            JdbcTemplate jdbcTemplate = (JdbcTemplate)app.getBean("TestJdbcTemplate");
    
            //下面进行查询操作:
            //要注意的是 他这个查询的话 他不区分大小写 如果找到两三个的话会报异常 因为这个是单个查询
            User user = jdbcTemplate.queryForObject("select * from user where name=?", new BeanPropertyRowMapper<User>(User.class), "bihu");
            System.out.println(user.getName());
        }
    }

     你还可用  queryForObject 这个 方法使用聚合函数 下面以 count 为例:

    其实吧 你结合起来 ,实战哪里 你会用list.size的.....

    但是这个是使用mysql的聚合函数,效率肯定比你查找出某个然后list.size高啊

    import com.bihu.Bean.User;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.beans.PropertyVetoException;
    import java.util.List;
    
    public class TestJdbcTemplate {
        //这个是测试Class
    
        @Test
        public  void Test() throws PropertyVetoException {
            //已经通过Spring文件配置好了内个JDBC模板对象
            ApplicationContext app  = new ClassPathXmlApplicationContext("Application.xml");
            JdbcTemplate jdbcTemplate = (JdbcTemplate)app.getBean("TestJdbcTemplate");
    
            //下面进行聚合函数查询操作:下面举例子 count 聚合函数
            Long aLong = jdbcTemplate.queryForObject("select count(*) from user", Long.class);
            System.out.println(aLong);
        }
    }

    插入操作不用new 那个 行注入映射,一般查询才要

    本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/14986024.html

  • 相关阅读:
    机器人学 —— 轨迹规划(Artificial Potential)
    机器人学 —— 轨迹规划(Sampling Method)
    机器人学 —— 轨迹规划(Configuration Space)
    机器人学 —— 机器人感知(Kalman Filter)
    机器人学 —— 机器人感知(Gaussian Model)
    机器学习 —— 概率图模型(Homework: Structure Learning)
    机器人学 —— 机器人视觉(Bundle Adjustment)
    机器人学 —— 机器人视觉(极几何)
    机器学习 —— 概率图模型(Homework: CRF Learning)
    机器人学 —— 机器人视觉(估计)
  • 原文地址:https://www.cnblogs.com/bi-hu/p/14986024.html
Copyright © 2011-2022 走看看