一、JdbcTemplate概述与准备工作
1、什么是 JdbcTemplate
(1)Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
2、准备工作
(1)引入相关 jar 包(下图中的部分)
(2)在 spring 配置文件配置数据库连接池
(3)配置 JdbcTemplate 对象,注入 DataSource
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="spring5_tx"></context:component-scan>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean>
<!--JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
(4)创建与数据库表格对应的 service 类,创建 dao 类,在 dao 注入 jdbcTemplate 对象
entity(对应数据库中一张表)
/**
* 用于存放在Customer表中查找到的数据
* ORM编程思想(object relational mapping)
* 一个数据表对应一个java类
* 表中的一条记录对应java类的一个对象
* 表中的一个字段对应java类的一个属性
*/
@Repository
public class Customer {
private int id;
private String name;
private String email;
private Date birth;
public Customer() {
}
public Customer(int id, String name, String email, Date birth) {
this.id = id;
this.name = name;
this.email = email;
this.birth = birth;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", birth=" + birth +
'}';
}
}
Dao层
接口:
public interface CustomerDao {
//添加的方法
void add(Customer customer);
//修改的方法
void update(Customer customer);
//删除的方法,通过id删除
void delete(int id);
//查询的方法 只有一个对象结果集
Customer queryInstance(int id);
//查询返回某个值
int queryvalue();
//查询返回集合
List<Customer> query();
}
接口实现类:
@Repository
public class CustomerDaoImpl implements CustomerDao {
//注入JdbcTemlate
@Autowired
private JdbcTemplate jdbcTemplate;
//添加的方法
@Override
public void add(Customer customer) {
//调用JdbcTemplate对象中update方法实现添加操作
//update方法中有两个参数 1.sql语句 2.可变形参
String sql = "insert into customers(name,email,birth)values(?,?,?)";
int i = jdbcTemplate.update(sql, customer.getName(), customer.getEmail(), customer.getBirth());
System.out.println(i);
}
//修改的方法
@Override
public void update(Customer customer) {
//通过给定的customer的id属性进行修改
//获取需要修改的customer的id属性
int id = customer.getId();
String sql = "update customers set name=?,email=?,birth=? where id=?";
int i = jdbcTemplate.update(sql, customer.getName(), customer.getEmail(), customer.getBirth(), id);
System.out.println(i);
}
//删除的方法
@Override
public void delete(int id) {
//调用JdbcTemplate对象中update方法实现添加操作
//update方法中有两个参数 1.sql语句 2.可变形参
String sql = "DELETE FROM customers WHERE id =?";
int i = jdbcTemplate.update(sql,id);
System.out.println("影响数据库中"+ i + "条数据");
}
//查询返回对象
@Override
public Customer queryInstance(int id) {
String sql = "select * from customers where id = ?";
//调用jdbcTemplate中queryForObject()方法实现
//第二个参数RowMapper为:接口 返回不同类型的数据,使用这个接口里面实现类完成数据的封装
Customer customer = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Customer>(Customer.class), id);
return customer;
}
//查询返回某个值:调用jdbcTemplate的queryForObject()实现
//查询有多少条数据
@Override
public int queryvalue() {
//update方法中有两个参数 1.sql语句 2.可变形参
String sql = "select count(*) from customers";
Integer value = jdbcTemplate.queryForObject(sql, Integer.class);
return value;
}
//查询返回对象集合
@Override
public List<Customer> query() {
String sql = "select * from customers where id > ?";
List<Customer> customers = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Customer>(Customer.class), 24);
return customers;
}
}
service层
@Service
public class CustomerService {
//注入dao
@Autowired
private CustomerDao customerDao;
//添加的方法
public void add(Customer customer){
customerDao.add(customer);
}
//删除的方法
public void delete(int id){
customerDao.delete(id);
}
//修改的方法
public void update(Customer customer){
customerDao.update(customer);
}
//查询返回某个值的方法
public int queryValue(){
return customerDao.queryvalue();
}
//查询返回某个对象
public Customer queryInstance(int id){
return customerDao.queryInstance(id);
}
//查询返回对象集合
public List<Customer> query(){
return customerDao.query();
}
}