public interface BookDao {
public void add(Book book);
public void delete(String id);
public void myupdate(Book book);
public int findSelect();
public Book findObjectSelect(String id);
public List<Book> findListBook();
public void batchAdd(List<Object[]> batchArgs);
public void batchUpdate(List<Object[]> batchArgs);
public void batchDelete(List<Object[]> batchArgs);
}
@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void add(Book book) {
String sql="insert into t_book values(?,?,?)";
int update=jdbcTemplate.update(sql,
book.getUserId(),book.getUsername(),book.getUstatus());
System.out.println("添加成功:"+update);
}
@Override
public void myupdate(Book book) {
String sql="update t_book set username=?,ustatus=? where user_id=?";
int update=jdbcTemplate.update(sql,
book.getUsername(),book.getUstatus(),book.getUserId());
System.out.println("修改成功:"+update);
}
@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);
}
@Override
public int findSelect() {
String sql="select count(*) from t_book";
int count=jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
@Override
public Book findObjectSelect(String id) {
String sql="select * from t_book where user_id=?";
Book book=jdbcTemplate.queryForObject(
sql, new BeanPropertyRowMapper<Book>(Book.class), id);
return book;
}
@Override
public List<Book> findListBook() {
String sql="select * from t_book";
List<Book> listBook=jdbcTemplate.query(
sql, new BeanPropertyRowMapper<Book>(Book.class));
return listBook;
}
@Override
public void batchAdd(List<Object[]> batchArgs) {
String sql="insert into t_book values(?,?,?)";
int[] listBook=jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println("批量添加成功:"+Arrays.toString(listBook));
}
@Override
public void batchUpdate(List<Object[]> batchArgs) {
String sql="update t_book set username=?,ustatus=? where user_id=?";
int[] listBook=jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println("批量修改成功:"+Arrays.toString(listBook));
}
@Override
public void batchDelete(List<Object[]> batchArgs) {
String sql="delete from t_book where user_id=?";
int[] listBook=jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println("批量删除成功:"+Arrays.toString(listBook));
}
}
public class TestBook {
@Test
public void testAccount() {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
List<Object[]> batchArgs=new ArrayList<>();
Object [] data1= {"6"};
Object [] data2= {"8"};
Object [] data3= {"10"};
batchArgs.add(data1);
batchArgs.add(data2);
batchArgs.add(data3);
bookService.batchDelete(batchArgs);
}
}
import javax.annotation.Resource;
@Service
public class BookService {
@Autowired
private BookDao userDao;
public void batchDelete(List<Object[]> batchArgs) {
userDao.batchDelete(batchArgs);
}
}
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="com.test"></context:component-scan>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/spring" />
<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>