JdbcTemplate
- JdbcTemplate (概念和准备)
- 1. 什么是 JdbcTemplate
- (1) Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
- 2. 准备工作
- (1)配置文件的准备
- (2)在 Spring 的配置文件中配置数据库的连接池
- (3)配置 JdbcTemplate 对象,注入 DataSource
- (4)创建 service 类,创建 dao 类,在 dao 注入 jdbaTemplate 对象
- JdbcTemplate 操作数据库(添加)
- 1. 对应数据库创建实体类
- 2. 编写 service 和 dao
- (1)在 dao 进行数据库的添加操作
- (2)调用 JdbcTemplate 对象里面 update 方法实现添加操作
- 3. 测试类
JdbcTemplate (概念和准备)
1. 什么是 JdbcTemplate
(1) Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
2. 准备工作
(1)配置文件的准备
<?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:aop = "http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
</beans>
<project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring依赖 -->
<!-- 1.Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<!-- 2.Spring dao依赖 -->
<!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.20</version>
</dependency>
<!-- 3.Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.20</version>
</dependency>
<!--aop相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
</dependency>
<!-- 加载 mysql 的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
</project>
(2)在 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:aop = "http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 配置dbcp的连接池 -->
<bean name = "dataSource" class = "org.apache.commons.dbcp2.BasicDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "jdbc:mysql://192.168.56.10:3306"/>
<property name = "username" value = "root"/>
<property name = "password" value = "root"/>
</bean>
<bean name = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref = "dataSource"/>
</bean>
</beans>
(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:aop = "http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 配置dbcp的连接池 -->
<bean name = "dataSource" class = "org.apache.commons.dbcp2.BasicDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "jdbc:mysql://192.168.56.10:3306"/>
<property name = "username" value = "root"/>
<property name = "password" value = "root"/>
</bean>
<!-- JdbcTemplate 对象 -->
<bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入 dataSource -->
<property name = "dataSource" ref = "dataSource"></property>
</bean>
</beans>
(4)创建 service 类,创建 dao 类,在 dao 注入 jdbaTemplate 对象
- 开启组件扫描
<?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:aop = "http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 开启组件扫描 -->
<context:component-scan base-package = "com.fairykunkun"></context:component-scan>
<!-- 配置dbcp的连接池 -->
<bean name = "dataSource" class = "org.apache.commons.dbcp2.BasicDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "jdbc:mysql://192.168.56.10:3306"/>
<property name = "username" value = "root"/>
<property name = "password" value = "root"/>
</bean>
<!-- JdbcTemplate 对象 -->
<bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入 dataSource -->
<property name = "dataSource" ref = "dataSource"></property>
</bean>
</beans>
- service
package com.fairykunkun.service;
import com.fairykunkun.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookService {
// 注入 dao
@Autowired
private UserDao userDao;
}
- dao
package com.fairykunkun.dao;
public interface BookDao {
}
package com.fairykunkun.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class BookDaoImpl implements BookDao {
// 注入 jdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
}
JdbcTemplate 操作数据库(添加)
1. 对应数据库创建实体类
package com.fairykunkun.entity;
public class Book {
private String bookId;
private String bookName;
private String bstatus;
public String getBookId ( ) {
return bookId;
}
public void setBookId ( String bookId ) {
this.bookId = bookId;
}
public String getBookName ( ) {
return bookName;
}
public void setBookName ( String bookName ) {
this.bookName = bookName;
}
public String getBstatus ( ) {
return bstatus;
}
public void setBstatus ( String bstatus ) {
this.bstatus = bstatus;
}
@Override
public String toString ( ) {
return "Book{" +
"bookId='" + bookId + '\'' +
", bookName='" + bookName + '\'' +
", bstatus='" + bstatus + '\'' +
'}';
}
}
2. 编写 service 和 dao
(1)在 dao 进行数据库的添加操作
(2)调用 JdbcTemplate 对象里面 update 方法实现添加操作
package com.fairykunkun.dao;
import com.fairykunkun.entity.Book;
public interface BookDao {
void add ( Book book );
}
package com.fairykunkun.dao;
import com.fairykunkun.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class BookDaoImpl implements BookDao {
// 注入 jdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
// 添加的方法
@Override
public void add ( Book book ) {
// 创建 sql 语句
String sql = "insert into book_t (bookname,bstatus) values (?,?)";
// 调用方法实现
Object[] args = { book.getBookName ( ) , book.getBstatus ( ) };
int update = jdbcTemplate.update ( sql , args );
System.out.println ( sql );
System.out.println ( update );
}
}
package com.fairykunkun.service;
import com.fairykunkun.dao.BookDao;
import com.fairykunkun.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookService {
// 注入 dao
@Autowired
private BookDao bookDao;
// 添加的方法
public void addBook ( Book book ) {
bookDao.add ( book );
}
}
update (String sql , Object … args)
- 有两个参数
- 第一个参数: sql 语句
- 第二个参数:可变参数
在数据创建表
create table book_t (
id int primary key auto_increment,
bookname varchar(100),
bstatus varchar(20)
);
3. 测试类
package com.fairykunkun;
import com.fairykunkun.service.BookService;
import com.fairykunkun.entity.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestJdbcTemplate {
@Test
public void testBook ( ) {
ApplicationContext context =
new ClassPathXmlApplicationContext ( "bean13.xml" );
BookService bookService = context.getBean ( "bookService" , BookService.class );
Book book = new Book ( );
book.setBookName ( " Java 编程 " );
book.setBstatus ( "可用" );
bookService.addBook ( book );
}
}