spring boot DAO之jdbcTemplate
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,依赖 org.springframework.boot spring-boot-starter-data-jpa
千家信息网最后更新 2026年11月01日spring boot DAO之jdbcTemplate
依赖
org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java application-database.properties
#初始化数据库的时候,如果错误,是否继续启动。spring.datasource.continue-on-error=false#jdbc driver.默认通过uri来自动检测。spring.datasource.driver-class-name=com.mysql.jdbc.Driver#jdbc url.连接数据库的urispring.datasource.url=jdbc:mysql://172.28.1.227:3310/fc?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useServerPrepStmts=false#数据库连接用户名spring.datasource.username=fcdev#数据连接密码spring.datasource.password=123456#全限定名,连接池。默认自动检测classpathspring.datasource.type=com.zaxxer.hikari.HikariDataSource#sql脚本字符spring.datasource.sql-script-encoding=UTF-8#jdbcTemplate配置参数spring.jdbc.template.fetch-size=-1spring.jdbc.template.max-rows=-1spring.jdbc.template.query-timeout=60源码-JdbcProperties
@ConfigurationProperties(prefix = "spring.jdbc")public class JdbcProperties { private final Template template = new Template(); public Template getTemplate() { return this.template; } /** * {@code JdbcTemplate} settings. */ public static class Template { /** * Number of rows that should be fetched from the database when more rows are * needed. Use -1 to use the JDBC driver's default configuration. */ private int fetchSize = -1; /** * Maximum number of rows. Use -1 to use the JDBC driver's default configuration. */ private int maxRows = -1; /** * Query timeout. Default is to use the JDBC driver's default configuration. If a * duration suffix is not specified, seconds will be used. */ @DurationUnit(ChronoUnit.SECONDS) private Duration queryTimeout; }} 源码-JdbcTemplateAutoConfiguration
@Configuration@ConditionalOnClass({ DataSource.class, JdbcTemplate.class })@ConditionalOnSingleCandidate(DataSource.class)@AutoConfigureAfter(DataSourceAutoConfiguration.class)@EnableConfigurationProperties(JdbcProperties.class)public class JdbcTemplateAutoConfiguration { @Configuration static class JdbcTemplateConfiguration { private final DataSource dataSource; private final JdbcProperties properties; JdbcTemplateConfiguration(DataSource dataSource, JdbcProperties properties) { this.dataSource = dataSource; this.properties = properties; } @Bean @Primary @ConditionalOnMissingBean(JdbcOperations.class) public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); JdbcProperties.Template template = this.properties.getTemplate(); jdbcTemplate.setFetchSize(template.getFetchSize()); jdbcTemplate.setMaxRows(template.getMaxRows()); if (template.getQueryTimeout() != null) { jdbcTemplate .setQueryTimeout((int) template.getQueryTimeout().getSeconds()); } return jdbcTemplate; } } @Configuration @Import(JdbcTemplateConfiguration.class) static class NamedParameterJdbcTemplateConfiguration { @Bean @Primary @ConditionalOnSingleCandidate(JdbcTemplate.class) @ConditionalOnMissingBean(NamedParameterJdbcOperations.class) public NamedParameterJdbcTemplate namedParameterJdbcTemplate( JdbcTemplate jdbcTemplate) { return new NamedParameterJdbcTemplate(jdbcTemplate); } }}jdbcTemplate使用
@Repositorypublic class TestJdbcDao { @Autowired private JdbcTemplate jdbcTemplate; public String getMenuNameById(int id){ return jdbcTemplate.queryForObject("select name from t_sys_menu where id = ?", new Object[]{id}, String.class); }}jdbcTemplate测试
@RunWith(SpringRunner.class)@SpringBootTestpublic class AppContextTest { @Autowired private TestJdbcDao testJdbcDao; @Test public void jdbcTest(){ String menuName = testJdbcDao.getMenuNameById(1); System.out.println("menuName = " + menuName); }
数据
数据库
源码
检测
参数
字符
密码
时候
用户
用户名
脚本
错误
测试
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
华服互联网科技靠谱吗
互联网科技工作委员会副主任毛军
科技创新与互联网经济
疫情对互联网科技公司影响
互联网最新的科技有限公司
金融科技创新监管互联网金融
深圳互联网络科技有限公司
电子科技大学互联网
北京禾田雨橡互联网科技
互联网科技小额贷款
互联网科技公司介绍
金融科技和互联网金融
阿里巴巴算科技企业还是互联网
佳云科技和互联网广告
语蝶互联网科技有限公司
佛山市翊博互联网络科技有限公司
互联网科技大赛获奖感言
众安科技工业互联网合作
半导体是互联网高科技吗
互联网高科技行业有哪些企业
互联网和科技技术
A股互联网金融科技公司
凡科互联网科技
互联网科技寓意
来此购互联网科技有限公司
和信科技工业互联网
互联网公司高科技产品
宇信科技在杭州互联网排第几
万物互联网智能科技
安徽零酒互联网科技有限公司