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