学无先后,达者为师

网站首页 编程语言 正文

spring jdbctample方式访问数据库

作者:仰望星空的快乐 更新时间: 2022-05-10 编程语言

springjdbcTemplatetest访问mysql数据库,批量插入数据-Java文档类资源-CSDN下载

这是传统的javaweb方式访问数据库:

1.获取 驱动

2.获取连接 ----配置链接池,并从链接池获取连接,就是链接池技术

3.创建执行sql语句的处理器 ----

Statement stat = conn.createStatement(); 或者 preparedstatement

或者dbutil辅助类,接收不同类型的数据库查询结果

4.执行sql

5.处理结果

6.回收资源

jdbctample

spring中提供了 jdbctample辅助查询,不用再管连接池 回收资源 之类的

1.需要在xml中声明 jdbctample,并提供数据源




    
        
        
        
        
    

    
        
    

    

2.调用时,@autowired声明 jdbctample

package com.hxut.rj92.zyk.dao;

import com.hxut.rj92.zyk.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;

@Component
public class UserdaoImpl implements Userdao{
    @Autowired

JdbcTemplate jdbcTemplate;

    @Override
    public int add_dao(Student student) {

        int a= jdbcTemplate.update(" insert  student values(?,?,?);", new Object[]{student.getName(), (Integer)student.getId(), student.getLoc()});
        return a;
    }
    @Override
    public int change_dao(Student student) {
        int a= jdbcTemplate.update(" update  student set name=?,loc=? where id=?;", new Object[]{student.getName(),student.getLoc(),(Integer)student.getId()  });
        return a;
    }
    public int del_dao(int id)
    {
        int a =jdbcTemplate.update("delete  from student where id =?",new Object [] {id});
        return  a;
    }
    public int select_counts_dao()//查询行数
    {
        int a=jdbcTemplate.queryForObject("select count(*) from student",Integer.class);//第二个参数表示返回类型的class,因为是int类型,所以就是intenger
        return a;
    }
    public Student select_one_info_dao(int id)//查询一行数据的详细信息
    {   //注意student类中要有无参的构造函数
        String sql="select * from student where id=?";
        //第一个参数是sql语句,第二个是返回值类型(固定写法),第三个是sql参数的值
        Student student=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(Student.class),id);
        return student;
    }
    public List select_all_list_dao()
    {//和查询一个相比,使用的是query方法,参数没变
        String sql="select * from student where id<=?";
        List students=jdbcTemplate.query(sql,new BeanPropertyRowMapper(Student.class),99);
       return  students;
    }
    //批量插入,一下插入好几行数据
    public void add_batch_dao(List a)
    {
        //原理:a是一个列表,batchUpdate(sql,a)会遍历这个列表,用列表中的每个元素去执行那句sql,
        // 所以返回值是一个int类型数组,里面是每句的执行情况,成功返回[1, 1, 1]
        String sql="insert into student values(?,?,?)";
        int [] result=jdbcTemplate.batchUpdate(sql,a);
        System.out.println(Arrays.toString(result));
    }
    public  void update_batch_dao(List a)
    {   //原理:a是一个列表,batchUpdate(sql,a)会遍历这个列表,用列表中的每个元素去执行那句sql,
        // 所以返回值是一个int类型数组,里面是每句的执行情况,成功返回[4, 4, 3]
        //代表第一句改了四行,第二句四行,第三句三行
        String sql="update  student set name=?,loc=? where id=?;";
        int [] result=jdbcTemplate.batchUpdate(sql,a);
        System.out.println(Arrays.toString(result));
    }
    public  void delete_batch_dao(List a)
    {   //原理:a是一个列表,batchUpdate(sql,a)会遍历这个列表,用列表中的每个元素去执行那句sql,
        // 所以返回值是一个int类型数组,里面是每句的执行情况,成功返回[4, 4, 3]
        //代表第一句改了四行,第二句四行,第三句三行
        String sql="delete from student where id=?";
        int [] result=jdbcTemplate.batchUpdate(sql,a);
        System.out.println(Arrays.toString(result));
    }

}

原文链接:https://blog.csdn.net/sharesb/article/details/123967037

栏目分类
最近更新