What is a JdbcTemplate?

1. Spring framework encapsulates JDBC and uses JdbcTemplate to facilitate database operations

2. Preparation

(1) Configure the database connection pool in the Spring configuration file

<! -- Database connection pool --> 
<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>
Copy the code

Insert JdbcTemplate into DataSource

<! -- JdbcTemplate object --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 <! - inject the dataSource - >
 <property name="dataSource" ref="dataSource"></property>
</bean>
Copy the code

(3) Create the Service class, create the DAO class, inject the jdbcTemplate object into the DAO

* 配置文件
<!-- 组件扫描 --> 
<context:component-scan base-package="com.atguigu"> < / context: component - scan > ⚫ Service@Service
public class BookService {
 / / into the dao
 @Autowired
 private BookDao bookDao; 
 }
 
⚫ Dao
@Repository
public class BookDaoImpl implements BookDao {
 / / into the JdbcTemplate
 @Autowired
 private JdbcTemplate jdbcTemplate; 
 }
Copy the code

SQL > select * from JdbcTemplate;

1. Create an entity class for the database

2. Write services and DAOs

@Repository
public class BookDaoImpl implements BookDao {
/ / into the JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
// Add the method
@Override
public void add(Book book) {
	//1 Create an SQL statement
	String sql = "insert into t_book values(? ,? ,?) ";
	//2 calls the method implementation
	Object[] args = {book.getUserId(), book.getUsername(), 
	book.getUstatus()};
	intupdate = jdbcTemplate.update(sql,args); System.out.println(update); }}Copy the code

3. Test classes

@Test
public void testJdbcTemplate(a) {
 ApplicationContext context =
 new ClassPathXmlApplicationContext("bean1.xml");
 BookService bookService = context.getBean("bookService", 
BookService.class);
 Book book = new Book();
 book.setUserId("1");
 book.setUsername("java");
 book.setUstatus("a");
 bookService.addBook(book);
}
Copy the code

Select * from JdbcTemplate;

1, modify,

@Override
public void updateBook(Book book) {
	 String sql = "update t_book set username=? ,ustatus=? where user_id=?";
	 Object[] args = {book.getUsername(), book.getUstatus(),book.getUserId()};
	 int update = jdbcTemplate.update(sql, args);
	 System.out.println(update);
}
Copy the code

2, remove,

@Override
public void delete(String id) {
	 String sql = "delete from t_book where user_id=?";
	 int update = jdbcTemplate.update(sql, id);
	 System.out.println(update);
}
Copy the code

SQL > select * from JdbcTemplate;

// Query the number of records in the table
@Override
public int selectCount(a) { 
	String sql = "select count(*) from t_book";
	Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
	return count;
}
Copy the code

SQL > select * from JdbcTemplate;

SQL > select * from JdbcTemplate;

SQL > alter database set JdbcTemplate;

1. Batch operation: Operate multiple records in the table

2. JdbcTemplate implements batch add operation

// Batch add
@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));
}
// Add tests in batches
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"3"."java"."a"};
Object[] o2 = {"4"."c++"."b"};
Object[] o3 = {"5"."MySQL"."c"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
// Call batch add
bookService.batchAdd(batchArgs);
Copy the code

3. JdbcTemplate implements batch modification operations

// Batch change
@Override
public void batchUpdateBook(List<Object[]> batchArgs) {
	 String sql = "update t_book set username=? ,ustatus=? where user_id=?";
	 int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
	 System.out.println(Arrays.toString(ints));
}
// Batch change
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"java0909"."a3"."3"};
Object[] o2 = {"c++1010"."b4"."4"};
Object[] o3 = {"MySQL1111"."c5"."5"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
// Call the method to implement batch modification
bookService.batchUpdate(batchArgs);
Copy the code

4, JdbcTemplate to achieve batch delete operation

// Batch delete
@Override
public void batchDeleteBook(List<Object[]> batchArgs) {
	 String sql = "delete from t_book where user_id=?";
	 int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
	 System.out.println(Arrays.toString(ints));
}
// Batch delete
List<Object[]> batchArgs = newArrayList<>(); Object[] o1 = {"3"};
Object[] o2 = {"4"};
batchArgs.add(o1);
batchArgs.add(o2);
// Call method to implement batch delete
bookService.batchDelete(batchArgs);
Copy the code