spring boot DAO之jdbcTemplate

发布时间:2020-04-23 09:13:03 作者:乾坤刀
来源:网络 阅读:2560

依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

application-database.properties

#初始化数据库的时候,如果错误,是否继续启动。
spring.datasource.continue-on-error=false
#jdbc driver.默认通过uri来自动检测。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jdbc url.连接数据库的uri
spring.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
#全限定名,连接池。默认自动检测classpath
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#sql脚本字符
spring.datasource.sql-script-encoding=UTF-8

#jdbcTemplate配置参数
spring.jdbc.template.fetch-size=-1
spring.jdbc.template.max-rows=-1
spring.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使用

@Repository
public 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)
@SpringBootTest
public class AppContextTest {

    @Autowired
    private TestJdbcDao testJdbcDao;

    @Test
    public void jdbcTest(){
        String menuName = testJdbcDao.getMenuNameById(1);
        System.out.println("menuName = " + menuName);
    }
推荐阅读:
  1. 如何部署同一个Spring boot web 应用到不同的环境
  2. spring boot 访问mysql的主要方式有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot dao emp

上一篇:Redis官方ruby脚本创建cluster双实例三主三从集群的bug

下一篇:c语言double类型默认保留几位小数?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》