[刘阳Java]_JdbcTemplate用法_第11讲
JdbcTemplate模板提供操作数据库的方法应用,下面我们来说一下它的用法(注意:建议大家结合Spring API文档学习效果更好,因为下面的代码只是“抱砖引玉”)
1. 遵循常见的数据库操作(增,删,改,查),我们可以把JdbcTemplate的功能应用大致分成下几种
- execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句
- update方法:update方法用于执行新增、修改、删除等语句
- batchUpdate方法:用于执行批处理相关语句
- query方法、queryForXXX方法:用于执行查询相关语句
- call方法:用于执行存储过程、函数相关语句
2. queryForXXX方法
introwCount = this.jdbcTemplate.queryForInt("select count(0) from t_accrual"); //返回一个int值 intcountOfActorsNamedJoe = this.jdbcTemplate.queryForInt( "select count(0) from t_actors where first_name = ?", new Object[]{"Joe"}); //通过参数的绑定来返回int值 String surname = (String) this.jdbcTemplate.queryForObject( "select surname from t_actor where id = ?", new Object[]{new Long(1212)}, String.class); //返回一个String类型 Actor actor = (Actor) this.jdbcTemplate.queryForObject( "selectfirst_name, surname from t_actor where id = ?", new Object[]{new Long(1212)}, new RowMapper() { public Object mapRow(ResultSetrs, introwNum) throws SQLException { Actor actor = new Actor(); actor.setFirstName(rs.getString("first_name")); actor.setSurname(rs.getString("surname")); return actor; } }); //返回一个domain对象(JavaBean对象)
2. update方法
this.jdbcTemplate.update( "insert into t_actor (first_name, surname) values (?, ?)", new Object[] {"Leonor", "Watling"}); //插入操作 this.jdbcTemplate.update( "updatet_actor set weapon = ? where id = ?", new Object[] {"Banjo", new Long(5276)}); //更新操作 this.jdbcTemplate.update( "delete from actor where id = ?", new Object[] {new Long.valueOf(actorId)}); //删除操作
3. execute方法
this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))"); //执行一个DDL语句
4. 批量SQL语句操作
@Repository public class JdbcActorDao { @Autowried private JdbcTemplatejdbcTemplate; public int[] batchUpdate(final List actors) { int[] updateCounts = jdbcTemplate.batchUpdate( "updatet_actor set first_name = ?, last_name = ? where id = ?", new BatchPreparedStatementSetter() { public void setValues(PreparedStatementps, int i) throws SQLException { ps.setString(1, ((Actor)actors.get(i)).getFirstName()); ps.setString(2, ((Actor)actors.get(i)).getLastName()); ps.setLong(3, ((Actor)actors.get(i)).getId().longValue()); } public intgetBatchSize() { return actors.size(); } } ); return updateCounts; } }
5. JdbcDaoSupport支持类应用,它需要让Dao层中的类去继承。然后就可以在Dao层中不用去定义JdbcTemplate,但是在Dao层中的类还是需要依赖注入DataSource
package com.spring.dao; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.support.JdbcDaoSupport; public class SpringJdbcDao extends JdbcDaoSupport { public void getTbUsers() { String sql = "select * from tb_user"; List<Map> list = getJdbcTemplate().queryForList(sql); for (Map m : list) { System.out.println(m.get("USER_ID")+"\t"+m.get("USER_NAME")); } } }
<bean id="springjdbcdao" class="com.spring.dao.SpringJdbcDao"> <property name="dataSource" ref="dataSource"></property> </bean>
分类:
Spring_学习笔记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下