Introduction to Spring's use of JDBCTemplate, springjdbctemplate
Spring provides a powerful template class called jdbcTemplate to simplify JDBC operations. Both the DataSource object and the jdbcTemplate object of the data source can be defined in the configuration file in the form of beans, fully exert the power of dependency injection.
Case: Use jdbcTemplate to query all books
1. Introduce the jar package
<!-- MySQLjar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--spring jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.0.RELEASE</version> </dependency>
2. Create a class
3. Configure the xml file
3.1 configure the data source
Method 1
<! -- Data source --> <! -- 1. Default spring configuration 2. dbcp registration data source 3. c3p0 registration data source 4. druid registration data source --> <! -- Spring built-in data source --> <bean id = "dataSources" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" $ {jdbc. driver} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean>
Method 2
<! -- Dbcp register the data source --> <bean id = "dataSources" class = "org. apache. commons. dbcp. basicDataSource "> <property name =" driverClassName "value =" $ {jdbc. driver} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean>
Third Allocation Method
<! -- C3p0 configure the data source --> <bean id = "dataSources" class = "com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" driverClass "value =" $ {jdbc. driver} "/> <property name =" jdbcUrl "value =" $ {jdbc. url} "/> <property name =" user "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean>
Method 4
<! -- Druid: configure the data source --> <bean id = "dataSources" class = "com. alibaba. druid. pool. druidDataSource "> <property name =" driverClassName "value =" $ {jdbc. driver} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean>
Note: In addition to the first method, dependencies must be introduced in other methods.
<! -- Dbcp --> <dependency> <groupId> commons-dbcp </groupId> <artifactId> commons-dbcp </artifactId> <version> 1.4 </version> </dependency> <! -- C3p0 --> <dependency> <groupId> com. mchange </groupId> <artifactId> c3p0 </artifactId> <version> 0.9.5.2 </version> </dependency> <! -- Druid Alibaba, currently popular --> <dependency> <groupId> com. alibaba </groupId> <artifactId> druid </artifactId> <version> 1.0.29 </version> </dependency>
3.2 identify the jdbc. properties File
<! -- Identify the jdbc. properties file --> <! -- Method 1 --> <context: property-placeholder location = "classpath: jdbc. properties"/> <! -- Method 2 --> <bean class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" location "value =" classpath: jdbc. properties "> </property> </bean>
3.3 bind a data source
<! -- Bind a data source --> <bean id = "jdbcTemplates" class = "org. springframework. jdbc. core. jdbcTemplate "> <property name =" dataSource "ref =" CES "/> </bean>
3.4 bind the DAO layer implementation class
<!--DAO--> <bean id="BookDao" class="cn.happy.JDBCTemplate.dao.BookDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplates"/> </bean>
3.5 bind Service layer implementation class
<! -- Service --> <bean id = "BookService" class = "cn. happy. JDBCTemplate. service. BookServiceImpl">
<! -- Create an object in serviceImpl and encapsulate the get set Method -->
<Property name = "dao" ref = "BookDao"/>
</Bean>
4. Writing Method
Import cn. happy. JDBCTemplate. entity. book; import org. springframework. jdbc. core. rowMapper; import org. springframework. jdbc. core. support. jdbcDaoSupport; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. list;/*** Created by Administrator on 2018/3/13. */
// Inherit the JdbcDaoSupport class public class BookDaoImpl extends JdbcDaoSupport implements IBookDao {public List <Book> getAll (){
// Write SQL String SQL = "select * from book ";
// Call the get method List of the JdbcTemplate attribute of the JdbcDaoSupport class <Book> query = this. getJdbcTemplate (). query (SQL, new RowMapper <Book> () {public Book mapRow (ResultSet rs, int I) throws SQLException {/* rs reader I Index */
// Create object Book = new book ();
// Render the Data book. setBookId (rs. getInt ("bookId"); book. setBookName (rs. getString ("bookName"); book. setBookPrice (rs. getInt ("bookPrice"); return book ;}}); return query ;}}
5. Test class
//JDBCTemplate @Test public void Spring(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("JDBCTemplate.xml"); BookService service=(BookService)ctx.getBean("BookService"); List<Book> all = service.getAll(); for (Book item:all) { System.out.println(item.getBookName()); } }
Result: