spring jdbctample方式访问数据库

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

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

1.获取 驱动

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

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

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

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

4.执行sql

5.处理结果

6.回收资源

jdbctample

spring中提供了 jdbctample辅助查询,设置数据源,将数据源匹配到jdbctample中即可,不需要管statement/preparestatement等

数据源就是连接池

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

<?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="Datasource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql:///user_db"></property>
        <property name="username" value="root"></property>
        <property name="password" value="mysql"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>

    <bean id="JdbcTample" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="Datasource"></property>
    </bean>

    <context:component-scan base-package="com.hxut.rj92.zyk" ></context:component-scan>
</beans>

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>(Student.class),id);
        return student;
    }
    public List<Student> select_all_list_dao()
    {//和查询一个相比,使用的是query方法,参数没变
        String sql="select * from student where id<=?";
        List<Student> students=jdbcTemplate.query(sql,new BeanPropertyRowMapper<Student>(Student.class),99);
       return  students;
    }
    //批量插入,一下插入好几行数据
    public void add_batch_dao(List<Object[]> 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<Object[]> 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<Object[]> 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));
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值