spring boot DAO之jdbcTemplate
发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,依赖 org.springframework.boot spring-boot-starter-data-jpa
千家信息网最后更新 2024年10月22日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安全错误
数据库的锁怎样保障安全
表单构建后存储到数据库
计算机网络安全技术小论文
什么情况下会使用数据库
计算机二级网络技术基础知识
软件开发行业税务管理
海阳直播软件开发解决方案
西安末梢网络技术
一体机网络安全密钥怎么设置
系统提权 网络安全训练营
sony影像数据库未就绪
网络安全四格漫画大全
保存失败请检查数据库正确性
熙菱信息网络安全概念
思科网络安全技术
崇明区创新数据库服务销售
河南综合软件开发报价表
安徽服务器机箱厂家云空间
违反网络安全法行为名称
香港宇宙环球服务器
上海运营软件开发供应
表单构建后存储到数据库
热门网络安全公司avast
通过网关访问内网服务器
从数据库中读取文字
数据库怎么执行md文件
云服务器进安全模式
维护网络安全的重要性有哪些
数据库连接池的作用与功效
网页显示access数据库
仟和亿北京互联网科技有限公司