Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作。
准备工作
(1)将demo5复制为demo6,删除不必要的结构,引入jar包,结构如图:(这里版本有差异,不知道会不会出问题,出问题再改,尝试以下)
(2)创建基础的项目结构,编写配置文件,结构如下:
这里粘出配置文件
<?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="com.francis.spring5"/>
<!--数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql:///book" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean>
<!--JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
1、添加数据
package com.francis.spring5.mapper;
import com.francis.spring5.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* @author Francis
* @create 2021-06-08-14:38
*/
@Repository
public class BookMapperImpl implements BookMapper{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
String sql="insert into book values(?,?,?)";
jdbcTemplate.update(sql, book.getId(), book.getName(), book.getPrice());
}
}
2、修改和删除同样调用update方法,就不赘述了。
3、查询
1、查询返回一个对象
@Override
public Book select(int id) {
String sql="select * from book where id=?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
return book;
}
2、查询返回一个对象 集合
@Test
public void selectBooks(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService",BookService.class);
List<Book> books = bookService.selectBooks();
System.out.println(books);
}
3、批量插入
批量修改和删除不在赘述
package com.francis.spring5.service;
import com.francis.spring5.mapper.BookMapper;
import com.francis.spring5.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author Francis
* @create 2021-06-08-14:38
*/
@Service
public class BookService {
@Autowired
private BookMapper bookMapper;
public void add(Book book){
bookMapper.add(book);
}
public Book selectBookById(int id){
Book book = bookMapper.select(id);
return book;
}
public List<Book> selectBooks(){
List<Book> books = bookMapper.selectAllBook();
return books;
}
public void inserBatch(List<Object[]> batchArgs){
bookMapper.inserBatch(batchArgs);
}
}
package com.francis.spring5.mapper;
import com.francis.spring5.pojo.Book;
import java.util.List;
/**
* @author Francis
* @create 2021-06-08-14:37
*/
public interface BookMapper {
void add(Book book);
Book select(int id);
List<Book> selectAllBook();
void inserBatch(List<Object[]> batchArgs);
}
package com.francis.spring5.mapper;
import com.francis.spring5.pojo.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.List;
/**
* @author Francis
* @create 2021-06-08-14:38
*/
@Repository
public class BookMapperImpl implements BookMapper{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Book select(int id) {
String sql="select * from book where id=?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
return book;
}
@Override
public void add(Book book) {
String sql="insert into book values(?,?,?)";
jdbcTemplate.update(sql, book.getId(), book.getName(), book.getPrice());
}
@Override
public void inserBatch(List<Object[]> batchArgs) {
String sql="insert into book values(?,?,?)";
jdbcTemplate.batchUpdate(sql,batchArgs);
}
@Override
public List<Book> selectAllBook() {
String sql="select * from book";
List<Book> books = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Book.class));
return books;
}
}
package com.francis.spring5.test;
import com.francis.spring5.pojo.Book;
import com.francis.spring5.service.BookService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author Francis
* @create 2021-06-08-18:45
*/
public class JdbcTemplateTest {
@Test
public void insertBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService",BookService.class);
Book book = new Book();
book.setId(2);
book.setName("倚天屠龙记");
book.setPrice(5);
bookService.add(book);
}
@Test
public void selectBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService",BookService.class);
Book book = bookService.selectBookById(1);
System.out.println(book);
}
@Test
public void selectBooks(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService",BookService.class);
List<Book> books = bookService.selectBooks();
System.out.println(books);
}
@Test
public void inserBatch(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService",BookService.class);
List<Object[]> batchArgs=new ArrayList<>();
Object[] o1={3, "天龙八部", 3};
Object[] o2={4, "天龙八部4", 3};
batchArgs.add(o1);
batchArgs.add(o2);
bookService.inserBatch(batchArgs);
}
}