真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

springbootDAO之jdbcTemplate

依賴

成都創(chuàng)新互聯(lián)專注于治多企業(yè)網(wǎng)站建設,自適應網(wǎng)站建設,成都做商城網(wǎng)站。治多網(wǎng)站建設公司,為治多等地區(qū)提供建站服務。全流程按需網(wǎng)站開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            MySQL
            mysql-connector-java
        

application-database.properties

#初始化數(shù)據(jù)庫的時候,如果錯誤,是否繼續(xù)啟動。
spring.datasource.continue-on-error=false
#jdbc driver.默認通過uri來自動檢測。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jdbc url.連接數(shù)據(jù)庫的uri
spring.datasource.url=jdbc:mysql://172.28.1.227:3310/fc?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useServerPrepStmts=false
#數(shù)據(jù)庫連接用戶名
spring.datasource.username=fcdev
#數(shù)據(jù)連接密碼
spring.datasource.password=123456
#全限定名,連接池。默認自動檢測classpath
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#sql腳本字符
spring.datasource.sql-script-encoding=UTF-8

#jdbcTemplate配置參數(shù)
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);
    }

網(wǎng)站名稱:springbootDAO之jdbcTemplate
文章地址:http://weahome.cn/article/jdcecs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部