91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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);
    }
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

和政县| 库尔勒市| 绥滨县| 恩平市| 阿克苏市| 保定市| 泉州市| 汤原县| 岱山县| 宿松县| 名山县| 五莲县| 武邑县| 铜川市| 临沭县| 榕江县| 静安区| 新田县| 大荔县| 五河县| 防城港市| 滦平县| 周口市| 兴文县| 当雄县| 阿巴嘎旗| 诸城市| 枣阳市| 开封县| 宣城市| 长汀县| 伊春市| 康乐县| 稻城县| 五华县| 宁波市| 新源县| 汝南县| 精河县| 灯塔市| 宜君县|