JdbcTemplate概述
它是spring框架提供的一个对象,是对原始Jdbc API对象的简单封装,是用来操作关系型数据的
JdbcTemplate的作用:用于和数据库交互,实现对表的CRUD操作
准备工作
- 导入maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.1</version>
</dependency>
- 创建一个实体类
public class Book {
private int id;
private String name;
private Double price;
public Book(int id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Book() {
}
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 Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
- 创建一个dao接口类
public interface BookDao {
//根据id查询书本
Book queryById(int id);
//根据name查询书本
Book queryByName(String name);
//更新书本
void updateBook(Book book);
}
- 创建一个接口实现类
public class BookDaoImpl implements BookDao{
//由spring容器为我们注入JdbcTemplate对象
private JdbcTemplate jdbcTemplate;
//通过set方法注入
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//通过id查询数据
public Book queryById(int id) {
List<Book> books = jdbcTemplate.query("select * from book where id=?", new BeanPropertyRowMapper<Book>(Book.class), 1);
return books.isEmpty()?null:books.get(0);
}
//通过name查询数据
public Book queryByName(String name) {
List<Book> books = jdbcTemplate.query("select * from book where name=?", new BeanPropertyRowMapper<Book>(Book.class), name);
if (books.isEmpty()){
return null;
}else if (books.size()>1){
throw new RuntimeException("结果集不唯一");
}else {
return books.get(0);
}
}
//更新一条数据
public void updateBook(Book book) {
jdbcTemplate.update("update book set name=?,price=? where id =?","chenhui",8888,4);
}
}
- 配置spring核心配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置JdbcTemplate交给spring来管理-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 引用数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置spring内置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_zr?serverTimezone=PRC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置实现类bean交给spring管理,并且注入jdbcTemplate-->
<bean id="bookDaoImpl" class="com.chenhui.dao.BookDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
测试类:
public static void main(String[] args) {
//加载spring配置文件获取bean容器
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
//获取bean对象
BookDao bookDao = context.getBean("bookDaoImpl", BookDaoImpl.class);
//执行方法
Book book = bookDao.queryById(1);
System.out.println(book);
Book chenhui = bookDao.queryByName("DDD");
System.out.println(chenhui);
bookDao.updateBook(new Book(4,"CCC",10000.0));
}
结果: