JdbcTemplate(概念和准备)
1、概念
(1)Spring框架对JDBC进行封装,使用JDBCTemplate方便实现对数据库操作
2、准备工作
(1)引入相关jar包
(2)在Spring配置文件配置数据库连接池
(3)配置JdbcTemplate对象,注入DataSource
(4)创建service类,dao类,在dao注入jdbcTemplate对象
①组件扫描
②Service
③Dao
JdbcTemplate操作数据库(添加、修改和删除)添加为例
1、对应数据库表创建实体类
2、编写service和dao
(1)在dao和service增加添加、修改和删除方法
(2)调用JdbcTemplate对象里面update方法实现添加、修改和删除方法
第一个参数:sql语句
第二个参数:可变参数,设置sql语句值
(3)测试类(实际情况下book的值是通过页面传输过来的)
JdbcTemplate操作数据库(查询返回某个值)
1、查询表里有多少条记录 select count(*) from t_book
2、使用JdbcTemplate实现查询返回某个值的代码
第二个参数 返回类型的Class
实现BookService类方法
实现BookDao类方法
JdbcTemplate操作数据库(查询返回对象)
1、场景:查询某本书图书详情
2、调用JdbcTemplate方法实现查询返回对象
第二个参数:RowMappe接口,返回不同类型数据,使用这个接口里面实现类完成数据封装
第三个参数:sql语句值
JdbcTemplate操作数据库(查询返回集合)
1、场景:查询图书列表分页
2、调用JdbcTemplate方法实现查询返回对象
JdbcTemplate操作数据库(批量操作)
1、操作表里多条记录
2、批量添加
第二个参数:List集合,添加多条记录的数据
3、批量删除
4、批量修改
代码目录
BookDao代码
package jdbcTemplate.dao;
import jdbcTemplate.entity.Book;
import org.springframework.stereotype.Service;
import java.util.List;
public interface BookDao {
void add(Book book);
void update(Book book);
void delete(String id);
int selectCount();
Book findBookInfo(String id);
List<Book> findAllBook();
void batchAddBook(List<Object[]> batchArgs);
void batchUpdateBook(List<Object[]> batchArgs);
void batchDeleteBook(List<Object[]> batchArgs);
}
BookDaoImpl代码
package jdbcTemplate.dao;
import jdbcTemplate.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Arrays;
import java.util.List;
@Repository
public class BookDaoImpl implements BookDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
String sql = "insert into t_book values(?,?,?)";
jdbcTemplate.update(sql,book.getUserId(),book.getUsername(),book.getUstatus());
}
@Override
public void update(Book book) {
String sql = "update t_book set username=?,ustatus=? where user_id=?";
jdbcTemplate.update(sql,book.getUsername(),book.getUstatus(),book.getUserId());
}
@Override
public void delete(String id) {
String sql = "delete from t_book where user_id=?";
jdbcTemplate.update(sql,id);
}
@Override
public int selectCount() {
String sql = "select count(*) from t_book";
return jdbcTemplate.queryForObject(sql,Integer.class);
}
@Override
public Book findBookInfo(String id) {
String sql = "select * from t_book where user_id=?";
return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Book>(Book.class),id);
}
@Override
public List<Book> findAllBook() {
String sql = "select * from t_book";
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
}
@Override
public void batchAddBook(List<Object[]> batchArgs) {
String sql = "insert into t_book values(?,?,?)";
//返回值表示每次执行修改数据库的行数
int[] ints = jdbcTemplate.batchUpdate(sql,batchArgs);
System.out.println(Arrays.toString(ints));
}
@Override
public void batchUpdateBook(List<Object[]> batchArgs) {
String sql = "update t_book set username=?,ustatus=? where user_id=?";
jdbcTemplate.batchUpdate(sql,batchArgs);
}
@Override
public void batchDeleteBook(List<Object[]> batchArgs) {
String sql = "delete from t_book where user_id=?";
jdbcTemplate.batchUpdate(sql,batchArgs);
}
}
Book代码
package jdbcTemplate.entity;
public class Book {
private String userId;
private String username;
private String ustatus;
// public Book(String userid, String username, String ustatus) {
// this.userid = userid;
// this.username = username;
// this.ustatus = ustatus;
// }
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUstatus() {
return ustatus;
}
public void setUstatus(String ustatus) {
this.ustatus = ustatus;
}
@Override
public String toString() {
return "Book{" +
"userId='" + userId + '\'' +
", username='" + username + '\'' +
", ustatus='" + ustatus + '\'' +
'}';
}
}
BookService代码
package jdbcTemplate.service;
import com.alibaba.druid.sql.builder.UpdateBuilder;
import jdbcTemplate.dao.BookDao;
import jdbcTemplate.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
private BookDao bookDao;
//添加的方法
public void addBook(Book book) {
bookDao.add(book);
}
//修改
public void updateBook(Book book) {
bookDao.update(book);
}
//删除
public void deleteBook(String id) {
bookDao.delete(id);
}
//查询返回某个值
public int findCount() {
return bookDao.selectCount();
}
//查询返回对象
public Book findOne(String id) {
return bookDao.findBookInfo(id);
}
//查询返回集合
public List<Book> findAll() {
return bookDao.findAllBook();
}
//批量添加
public void batchAdd(List<Object[]> batchArgs) {
bookDao.batchAddBook(batchArgs);
}
//批量修改
public void batchUpdate(List<Object[]> batchArgs) {
bookDao.batchUpdateBook(batchArgs);
}
//批量删除
public void batchDelete(List<Object[]> batchArgs) {
bookDao.batchDeleteBook(batchArgs);
}
}
test测试类代码
package jdbcTemplate.test;
import jdbcTemplate.entity.Book;
import jdbcTemplate.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
public class test {
@Test
public void testAdd() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
// bookService.addBook(new Book("1","java","a"));
Book book = new Book();
book.setUserId("2");
book.setUsername("java");
book.setUstatus("aaa");
bookService.addBook(book);
}
@Test
public void testUpdate() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setUserId("1");
book.setUsername("spring");
book.setUstatus("c");
bookService.updateBook(book);
}
@Test
public void testDelete() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.deleteBook("1");
}
@Test
public void testFindCount() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
System.out.println(bookService.findCount());
}
@Test
public void testFindOne() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
System.out.println(bookService.findOne("1"));
}
@Test
public void testFindAll() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
System.out.println(bookService.findAll());
}
@Test
public void testBatchAddBook() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"3","c++","a"};
Object[] o2 = {"4","c","a"};
Object[] o3 = {"5","mysql","a"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
bookService.batchAdd(batchArgs);
}
@Test
public void testBatchUpdateBook() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"c","a","3"};
Object[] o2 = {"mysql","a","4"};
Object[] o3 = {"c++","a","5"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
bookService.batchUpdate(batchArgs);
}
@Test
public void testBatchDeleteBook() {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate/bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"3"};
Object[] o2 = {"4"};
Object[] o3 = {"5"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
bookService.batchDelete(batchArgs);
}
}
配置文件bean1.xml代码
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="jdbcTemplate"></context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClassName}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入DataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>