您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
07-JdbcTemplate基本使用-常用操作-更新操作(及删除操作)
发布时间:2025-01-05 18:46:51编辑:雪饮阅读()
-
其实之前也了解过Spring提供的单元测试可以直接结合注解拿到JdbcTemplate上下文,那么对于数据库表的update与delete操作就很容易了。实现起来也是一眼就能看懂的。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:JDBCApplicationContext.xml")
public class Test5 {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testUpdate() {
jdbcTemplate.update("update account set money=? where name=?",10,"tom");
}
@Test
public void testDelete() {
jdbcTemplate.update("delete from account where name=?","tom");
}
}
这里基于上篇中有将JdbcTemplate在Spring Config配置文件xml中定义并配置数据源了所以可以直接获取了。
关键字词:JdbcTemplate,更新,删除
上一篇:06-JdbcTemplate基本使用-spring产生模板对象代码实现(抽取jdbc.properties)
下一篇:08-JdbcTemplate基本使用-常用操作-查询操作(查询所有,查询总记录数,查询单条,查看接口都有哪些实现类,类搜索,快速定义方法的接收变量类型及变量名)