1. jdbcTemplate concept
The Spring framework encapsulates JDBC and uses JdbcTemplate to facilitate database operations
Ready to work
(1) Introduce related Jar packages:
(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:///book_db?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="MySQLqdl0661" />
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
(3) Configure JdbcTemplate object and inject DataSource
</bean>
<!--创建JDBCTemplate对象-->
<!--注入属性dateSourse-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
(4) Create service class, create dao class, and inject jdbcTemplate object in dao
- Turn on component scanning
- Service class injected into BookDao
@Service
public class BookService {
@Autowired
private BookDao bookDao;
}
- BookDaoImpl injects JdbcTemplate
@Repository
public class BookDaoImpl implements BookDao{
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
}
(5) Create a database
2. Increase-add
- Create sql statement
- Call the
method to achieve the addition.
//添加
@Override
public void add(Book book) {
//1.创建sql语句
String sql="insert into book_t value(?,?,?)";
//2.调用方法实现实现增改查
int add=jdbcTemplate.update(sql,book.getBookId(),book.getBookName(),book.getBookStatus());
}
- Test Results
Three. Delete-delete
//删除
@Override
public void delete(String id) {
String sql="delete from book_t where bookId=?";
int delete=jdbcTemplate.update(sql,id);
}
Four. Change-update
//修改
@Override
public void update(Book book) {
String sql="update book_t set bookName=?,bookStatus=? where bookId=?";
Object[] args ={
book.getBookName(),book.getBookStatus(),book.getBookId()};
int update=jdbcTemplate.update(sql,args);
}
Five. Find
(1) The query returns a certain value
- How many records are in the query table, the return is a certain value:
- Use jdbcTemplate
to implement query operations - The parameter sql is a SQL statement
- Parameter Class is the return type
//查询总数据条数
@Override
public int selectCount() {
//使用count语句
String sql="select count(*) from book_t";//count聚集函数
int count=jdbcTemplate.queryForObject(sql,Integer.class);
return count;
}
(2) The query returns the object
1. Scenario: Query book details
2. JdbcTemplate realizes query and return objects
3. Use method queryForObject():
- Parameter 1: sql query statement
- Parameter 2: RowMapper is an interface, for returning different types of data, use the implementation class in this interface to complete data encapsulation
- Parameter 3: sql statement value
//查询图书对象
@Override
public Book findBookInfo(String id) {
String sql="select * from book_t where bookId=?";
//调用方法
Book book=jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class),id);
return book;
}
(3) Query returns collection
1. Scenario: Query the book list and pagination...
2. Call the JdbcTemplate method to realize the query and return the collection
3. How to use:
- Parameter 1: sql query statement
- Parameter 2: RowMapper is an interface, for returning different types of data, use the implementation class in this interface to complete data encapsulation
- Parameter 3: sql statement value
//查询图书集合
@Override
public List<Book> findAllBook() {
String sql="select * from book_t";
List<Book> bookList=jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
return bookList;
}
Six. Batch operation
(1) Batch increase
//批量增加图书
@Override
public int[] batchAddBook(List<Object[]> batchArgs) {
String sql="insert into book_t values(?,?,?)";
int[] res=jdbcTemplate.batchUpdate(sql,batchArgs);
return res;
}
- test
(2) Batch modification operation
//批量修改
@Override
public int[] batchUpdateBook(List<Object[]> batchArgs) {
String sql="update book_t set bookName=?,bookStatus=? where bookId=?";
int[] res=jdbcTemplate.batchUpdate(sql,batchArgs);
return res;
}
- test
(3) Batch delete operation
//批量删除操作
@Override
public int[] batchDeleteBook(List<Object[]> batchArgs) {
String sql="delete from book_t where bookId=?";
int[] res=jdbcTemplate.batchUpdate(sql,batchArgs);
return res;
}