一、什么是 JdbcTemplate(概念和准备)
1、Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
2、准备工作
(1)在 spring 配置文件配置数据库连接池
<bean id&#61;"dataSource" class&#61;"com.alibaba.druid.pool.DruidDataSource" destroy-method&#61;"close">
<property name&#61;"url" value&#61;"jdbc:mysql:///user_db" />
<property name&#61;"username" value&#61;"root" />
<property name&#61;"password" value&#61;"root" />
<property name&#61;"driverClassName" value&#61;"com.mysql.jdbc.Driver" />
bean>
&#xff08;2&#xff09;配置 JdbcTemplate 对象&#xff0c;注入 DataSource
<bean id&#61;"jdbcTemplate" class&#61;"org.springframework.jdbc.core.JdbcTemplate">
<property name&#61;"dataSource" ref&#61;"dataSource">property>
bean>
&#xff08;3&#xff09;创建 service 类&#xff0c;创建 dao 类&#xff0c;在 dao 注入 jdbcTemplate 对象
* 配置文件
<!-- 组件扫描 -->
<context:component-scan base-package&#61;"com.atguigu"></context:component-scan>
⚫ Service
&#64;Service
public class BookService {
&#64;Autowired
private BookDao bookDao;
}
⚫ Dao
&#64;Repository
public class BookDaoImpl implements BookDao {
&#64;Autowired
private JdbcTemplate jdbcTemplate;
}
二、JdbcTemplate 操作数据库&#xff08;添加&#xff09;
1、对应数据库创建实体类
2、编写 service 和 dao
&#64;Repository
public class BookDaoImpl implements BookDao {
&#64;Autowired
private JdbcTemplate jdbcTemplate;
&#64;Override
public void add(Book book) {
String sql &#61; "insert into t_book values(?,?,?)";
Object[] args &#61; {book.getUserId(), book.getUsername(),
book.getUstatus()};
int update &#61; jdbcTemplate.update(sql,args); System.out.println(update);
}
}
3、测试类
&#64;Test
public void testJdbcTemplate() {
ApplicationContext context &#61;
new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService &#61; context.getBean("bookService",
BookService.class);
Book book &#61; new Book();
book.setUserId("1");
book.setUsername("java");
book.setUstatus("a");
bookService.addBook(book);
}
三、JdbcTemplate 操作数据库&#xff08;修改和删除&#xff09;
1、修改
&#64;Override
public void updateBook(Book book) {
String sql &#61; "update t_book set username&#61;?,ustatus&#61;? where user_id&#61;?";
Object[] args &#61; {book.getUsername(), book.getUstatus(),book.getUserId()};
int update &#61; jdbcTemplate.update(sql, args);
System.out.println(update);
}
2、删除
&#64;Override
public void delete(String id) {
String sql &#61; "delete from t_book where user_id&#61;?";
int update &#61; jdbcTemplate.update(sql, id);
System.out.println(update);
}
四、JdbcTemplate 操作数据库&#xff08;查询返回某个值&#xff09;
&#64;Override
public int selectCount() {
String sql &#61; "select count(*) from t_book";
Integer count &#61; jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
五、JdbcTemplate 操作数据库&#xff08;查询返回对象&#xff09;
六、JdbcTemplate 操作数据库&#xff08;查询返回集合&#xff09;
七、JdbcTemplate 操作数据库&#xff08;批量操作&#xff09;
1、批量操作&#xff1a;操作表里面多条记录
2、JdbcTemplate 实现批量添加操作
&#64;Override
public void batchAddBook(List<Object[]> batchArgs) {
String sql &#61; "insert into t_book values(?,?,?)";
int[] ints &#61; jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}
List<Object[]> batchArgs &#61; new ArrayList<>();
Object[] o1 &#61; {"3","java","a"};
Object[] o2 &#61; {"4","c&#43;&#43;","b"};
Object[] o3 &#61; {"5","MySQL","c"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
bookService.batchAdd(batchArgs);
3、JdbcTemplate 实现批量修改操作
&#64;Override
public void batchUpdateBook(List<Object[]> batchArgs) {
String sql &#61; "update t_book set username&#61;?,ustatus&#61;? where user_id&#61;?";
int[] ints &#61; jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}
List<Object[]> batchArgs &#61; new ArrayList<>();
Object[] o1 &#61; {"java0909","a3","3"};
Object[] o2 &#61; {"c&#43;&#43;1010","b4","4"};
Object[] o3 &#61; {"MySQL1111","c5","5"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
bookService.batchUpdate(batchArgs);
4、JdbcTemplate 实现批量删除操作
&#64;Override
public void batchDeleteBook(List<Object[]> batchArgs) {
String sql &#61; "delete from t_book where user_id&#61;?";
int[] ints &#61; jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}
List<Object[]> batchArgs &#61; new ArrayList<>();Object[] o1 &#61; {"3"};
Object[] o2 &#61; {"4"};
batchArgs.add(o1);
batchArgs.add(o2);
bookService.batchDelete(batchArgs);