Spring's way of self-study in JdbcTemplate

Source: Internet
Author: User

Spring's path to self-study jdbctemplate using JdbcTemplate in DAO

It is common to use jdbctimplate in the DAO class, which can be injected into the DAO after it is configured in the XML configuration file.

1  PackageCom.smart.dao;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 Importorg.springframework.jdbc.core.JdbcTemplate;5 Importorg.springframework.stereotype.Repository;6 7 @Repository8  Public classTestdao {9     Privatejdbctemplate JdbcTemplate;Ten @Autowired One      Public voidsetjdbctemplate (JdbcTemplate jdbctemplate) { A          This. JdbcTemplate =JdbcTemplate; -     } -      Public voidInitdb () { theString sql = "CREATE TABLE test_user (user_id int primary key,user_name varchar (60))"; - jdbctemplate.execute (SQL); -     } -}

You can easily create other DAO classes in the same way, define JdbcTemplate in the spring configuration file, and inject it into each DAO

1<!--configuration transaction manager--><!--Scan class package, automatically converts the class that labels spring annotations, and completes the bean injection--2<context:component-scan base- Package= "Com.smart.dao"/>3 4<!--configuring data sources-5<bean id= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"6Destroy-method= "Close"7P:driverclassname= "${jdbc.driverclassname}"8P:url= "${jdbc.url}"9P:username= "${jdbc.name}"TenP:password= "${jdbc.password}"/> One  A<!--Configure the JDBC template-- -<!--JdbcTemplate has several properties that can be used to control the underlying JDBC API - QueryTimeout: Set the maximum time-out for statement query data created by Jdbctimplate, default 0 the fetchsize: Sets the number of rows that the underlying resultset returns each time from the database. This property has a large effect on the program, and if the settings are too large, - memory consumption is high because the data loaded at once is put into memory, and vice versa. The default value for the 0,oracle driver is ten - maxRows: Sets the maximum number of rows that the underlying resultset returns from the database, with a default value of 0.  - Ignorewarnings: Whether to ignore the SQL warning message. By default true, all warning messages are recorded in the log; + If False, JdbcTemplate will throw sqlwarningexception. -- +<bean id= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate" Ap:datasource-ref= "DataSource"/>

Spring's way of self-study in JdbcTemplate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.