您的位置:首页 > 编程语言 > Java开发

Spring -jdbcTemlpate使用小简介

2017-10-14 15:08 316 查看

1.首先在创建一个数据库spring,里面创建两个表

为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架.

作为 SpringJDBC 框架的核心, JDBC 模板的设计目的是为不同类型的JDBC操作提供模板方法. 每个模板方法都能控制整个过程,并允许覆盖过程中的特定任务.通过这种方式,可以在尽可能保留灵活性的情况下,将数据库存取的工作量降到最低.

JdbcTemplate主要提供以下五类方法:

execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;

query方法及queryForXXX方法:用于执行查询相关语句;

call方法:用于执行存储过程、函数相关语句。

使用示例:

在数据库中先准备两张表:

employee和department







并且根据这两个表创建两个类

employee类

package com.atguigu.spring.jdbc;

public class Employee {
private int id;
private String lastname;
private String email;

private Department department;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

@Override
public String toString() {
return "Employee [id=" + id + ", lastname=" + lastname + ", email=" + email + ", department=" + department
+ "]";
}

}
department类

package com.atguigu.spring.jdbc;

public class Department {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}

}


下面介绍了使用的方法

JDBCTest类
package com.atguigu.spring.jdbc;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

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 org.springframework.jdbc.core.RowMapper;

public class JDBCTest {

private ApplicationContext cx=null;
private JdbcTemplate jdbcTemplate;
/**
* 代码块时一个类中,最先也是必须会执行的一个部分
*/
{
cx=new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate=(JdbcTemplate) cx.getBean("jdbcTemplate");
}
/**
* 测试一下数据库连接是否成功
* @throws SQLException
*/

@Test
public void testDataSource() throws SQLException {
DataSource dataSource=cx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
/**
* 更新一下
*/
@Test
public void testUpdate(){
String sql="update employee set last_name=? where id=?";
jdbcTemplate.update(sql, "jack",5);
}
/**
* 更新一批数据,可以删除,插入,更新等
*/
@Test
public void testBatchUpdate(){
String sql="insert into employee(last_name,email,dept_id) values(?,?,?)";
List<Object[]> batchArgs=new ArrayList<>();
batchArgs.add(new Object[]{"arao","a1304001740@qq.com",5});
batchArgs.add(new Object[]{"brao","b1304001740@qq.com",4});
batchArgs.add(new Object[]{"crao","c1304001740@qq.com",3});
batchArgs.add(new Object[]{"drao","d1304001740@qq.com",2});
jdbcTemplate.batchUpdate(sql, batchArgs);
}
/**
*
* last_name AS lastname 这个要和你封装的类中的属性名字一致,否则赋值不成功,起个别名就可以解决
*
* ctrl+t 看以看到Rowmapper 下有个实现类时BeanPropertyRowMapper
*/
@Test
public void testQueryForObject(){
String sql="select id,last_name AS lastname,email from employee where id=?";
RowMapper<Employee> rowMapper=new BeanPropertyRowMapper<>(Employee.class);
Employee employee=jdbcTemplate.queryForObject(sql, rowMapper, 1);
System.out.println(employee);
}
/**获得一个对象的集合
* ctrl+t 看以看到Rowmapper 下有个实现类时BeanPropertyRowMapper
*/
@Test
public void testQueryForList(){
String sql="select id,last_name AS lastname,email from employee where id
aaf7
>?";
RowMapper<Employee> rowMapper=new BeanPropertyRowMapper<>(Employee.class);
List<Employee> employees=jdbcTemplate.query(sql, rowMapper, 3);
for (Employee employee : employees) {
System.out.println(employee);
}
}
/**
* 获取某个列的值,或坐统计查询
*/
@Test
public void testQueryForObject2(){
String sql="select count(id)from employee";
Long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
}
}


applocationContex.xml

jdbc.user=root
jdbc.password=199692
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring

jdbc.initPoolSize=1
jdbc.maxPoolSize
jdbc.initPoolSize 连接池最开始连接数

jdbc.maxPoolSize 连接池最大连接数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: