Spring JDBC配置与使用

Spring JDBC


Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常,处理事务,到最后关闭连接。大大简化了开发人员对数据库的操作,使得开发人员可以从繁琐的数据库操作中解脱出来,从而将更多的精力投入到编写业务逻辑中。
Spring框架提供了JdbcTemplate类,该类是Spring框架数据抽象层的基础这是管理所有数据库通信和异常处理的中央框架类。


JdbcTemplate

是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。
JdbcTemplate虽然简单,功能却非常强大。它提供了非常丰富、实用的方法,归纳起来主要有以下几种类型的方法:

  1. execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句。
  2. update、batchUpdate方法:用于执行新增、修改与删除等语句。
  3. query和queryForXXX方法:用于执行查询相关的语句。
  4. call方法:用于执行数据库存储过程和函数相关的语句。

总的来说,新增、删除与修改三种类型的操作主要使用update和batchUpdate方法来完成query和queryForObject方法中主要用来完成查询功能。execute方法可以用来执行任意的SQL、call方法来调用存储过程,

 Spring JDBC 示例

一数据库表

create database mybatis_demo; 

use mybatis_demo;

CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` datetime default NULL COMMENT '生日',
`sex` char(1) default NULL COMMENT '性别',
`address` varchar(256) default NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (1,'老王','2018-02-27
17:47:08','男','北京'),(2,'熊大','2018-03-02 15:09:37','女','上海'),(3,'熊二','2018-03-04
11:34:34','女','深圳'),(4,'光头强','2018-03-04 12:04:06','男','广州');

二添加依赖

pom.xml中加入Spring 和mysql相关的包


  
    junit
    junit
    4.11
    test
  

  
    org.springframework
    spring-core
    5.2.7.RELEASE
  

  
    org.springframework
    spring-beans
    5.2.7.RELEASE
  

  
    org.springframework
    spring-context
    5.2.7.RELEASE
  

  
    org.springframework
    spring-context-support
    5.2.7.RELEASE
  
  
    org.springframework
    spring-expression
    5.2.7.RELEASE
  

  
    commons-logging
    commons-logging
    1.2
  
  
    mysql
    mysql-connector-java
    8.0.20
  
  
    org.springframework
    spring-jdbc
    5.2.7.RELEASE
  
  
    org.springframework
    spring-tx
    5.2.7.RELEASE
  
  
    org.springframework
    spring-aop
    5.2.7.RELEASE
  

三创建实体类

public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    //get  set方法
    
 }

四、在Spring.xml当中配置数据库的链接和idbcTemplate

 




    
    
        
        
        
        
    

    
    
        
    

Spring JDBC配置与使用_第1张图片

 五创建类实现增删改查操作

import com.qcby.entity.User;
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.util.List;

public class SpringTest {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("Application.xml");;
    JdbcTemplate jdbcTemplate= (JdbcTemplate) ctx.getBean("jdbcTemplate");

    @Test
    public void testInsert(){
        String sql="insert into user(username,address) values('李连杰','上海')";
        jdbcTemplate.execute(sql);
    }

    @Test
    public void testUpdate(){
        String sql="update user set username='稳杰',address='南海' where id=?";
        int res=jdbcTemplate.update(sql,2);
        System.out.println(res);
    }

    @Test
    public void testDelete(){
        String sql="delete from user where id=?";
        int res=jdbcTemplate.update(sql,18);
        System.out.println(res);
    }

    //查询列表
    @Test
    public void testQueryList(){
        String sql = "select * from user where address like '%京%'";
        List userList= (List) jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class));
        System.out.println("查询List: ");
        for (User user : userList) {
            System.out.println(user);
        }
        System.out.println("数量: "+userList.size());
    }
}

结果:

 

你可能感兴趣的:(spring,oracle,数据库)