1. Concept and preparation
- What is JdbcTemplate?
Spring framework encapsulates JDBC, and using JdbcTemplate can easily implement database operations. - Preparation
(1) Introduce related jar packages and dependencies
(2) Configure the database connection pool in the spring configuration file
<!--数据库连接池配置-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql:///user_db"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
(3) Configure the jdbcTemplate object and inject the DataSource
<!--创建jdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
(4) Create the service class, create the dao class, and inject the jdbcTemplate object in the dao class
- Configuration file
<!--组件扫描-->
<context:component-scan base-package="org.example"></context:component-scan>
- service class
@Service
public class BookService {
//注入dao
@Autowired
private BookDao userDao;
}
- dao class
public interface BookDao {
}
@Repository
public class BookDaoImpl implements BookDao{
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
}
2. Jdbc Template operation database (add)
- Create entity class corresponding to database table
public class Book {
private String bookId;
private String bookName;
private String bookStatus;
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 getBookStatus() {
return bookStatus;
}
public void setBookStatus(String bookStatus) {
this.bookStatus = bookStatus;
}
}
- Write service and dao
(1) Add database operation in dao
public interface BookDao {
void add(Book book);
}
@Service
public class BookService {
//注入dao
@Autowired
private BookDao bookDao;
public void addBook(Book book) {
bookDao.add(book);
}
}
(2) Call the update method in the jdbcTemplate object to implement the operation.
There are two parameters: the first parameter is a sql statement, and the second parameter is a variable parameter, which sets the value of the sql statement.
@Repository
public class BookDaoImpl implements BookDao{
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
//1. 创建sql语句
String sql = "insert into t_book values(?,?,?)";
//2. 调用方法实现
int update = jdbcTemplate.update(sql, book.getBookId(), book.getBookName(), book.getBookStatus());
System.out.println(update);
}
}
Three, JdbcTemplate operation database (modify and delete)
@Override
public void updateBook(Book book) {
String sql = "update t_book set book_name=?,book_status=? where book_id=?";
Object[] args = {
book.getBookName(), book.getBookStatus(), book.getBookId()};
int update = jdbcTemplate.update(sql, args);
System.out.println(update);
}
@Override
public void delete(String id) {
String sql = "delete from t_book where book_id=?";
int update = jdbcTemplate.update(sql, id);
System.out.println(update);
}
Four, JdbcTemplate operating database (the query returns a certain value)
- How many records are in the query table, the return is a certain value.
- Use Jdbctemplate to realize that the query returns a certain value.
@Override
public int selectCount() {
String sql = "select count(*) from t_book";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
5. JdbcTemplate operation database (the query returns an object)
1. Scenario: Please check the book for details.
2. JdbcTemplate query returns the object.
There are three parameters
. The first is: sql statement and the
second is: RowMapper, which is an interface, which returns different types of data. Use the implementation class in this interface to complete data encapsulation. The
third parameter: sql statement value
@Override
public Book findOne(String id) {
String sql = "select * from t_book where book_id=?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
return book;
}
6. JdbcTemplate operation database (query return collection)
1. Scenario: Query book list pagination...
2. Call JdbcTemplate method to implement query return method
@Override
public List<Book> findAllBook() {
String sql = "select * from t_book";
List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
return bookList;
}
Seven, JdbcTemplate operation database (batch operation)
1. Batch operation: multiple records in the operation table
2. JdbcTemplate realizes batch addition operation
There are two parameters: the first parameter: sql statement. The second parameter: List collection, add multiple records of data
@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));
}
3. JdbcTemplate realizes batch modification operation
@Override
public void batchUpdateBook(List<Object[]> objects) {
String sql = "update t_book set book_name=?, book_status=? where book_id=?";
jdbcTemplate.batchUpdate(sql, objects);
}
Note: Each item in the Ojbect[] array should correspond to the placeholder "?" in the sql statement one by one, don't make a mistake in the order.
4. JdbcTemplate realizes batch delete operation
@Override
public void batchDeleteBook(List<Object[]> objects) {
String sql = "delete from t_book where book_id=? and book_name=? and book_status=?";
jdbcTemplate.batchUpdate(sql, objects);
}