Implementation of JdbcTemplate addition, deletion, modification, and check operation in Spring framework

1. jdbcTemplate concept

The Spring framework encapsulates JDBC and uses JdbcTemplate to facilitate database operations
Insert picture description here

Ready to work

(1) Introduce related Jar packages:

Insert picture description here

(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&amp;characterEncoding=utf8&amp;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

Insert picture description here

  • 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
Insert picture description here

2. Increase-add

  • Create sql statement
  • Call the Insert picture description here
    method to achieve the addition.

Insert picture description here

//添加
@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
    Insert picture description here
    Insert picture description here

Three. Delete-delete

  • Insert picture description here

Insert picture description here

//删除
@Override
public void delete(String id) {
    
    
    String sql="delete from book_t where bookId=?";
    int delete=jdbcTemplate.update(sql,id);
}

Four. Change-update

Insert picture description here

 //修改
 @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);
 }

Insert picture description here

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 Insert picture description here
    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;
}

Insert picture description here
Insert picture description here

(2) The query returns the object

1. Scenario: Query book details
2. JdbcTemplate realizes query and return objects
3. Use method queryForObject():
Insert picture description here

  • 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;
}

Insert picture description here

(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:

Insert picture description here

  • 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;
}

Insert picture description here

Six. Batch operation

(1) Batch increase

Insert picture description here

//批量增加图书
@Override
public int[] batchAddBook(List<Object[]> batchArgs) {
    
    
    String sql="insert into book_t values(?,?,?)";
    int[] res=jdbcTemplate.batchUpdate(sql,batchArgs);
    return res;
}
  • test
    Insert picture description here
    Insert picture description here

(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
    Insert picture description here
    Insert picture description here

(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;
 }

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44307065/article/details/107348838