[Turn] Several different usages of SPRINGJDBC

Source: Internet
Author: User

Transferred from: http://vsp.iteye.com/blog/1182887

Spring has made a good encapsulation of JDBC, and I have tried to do the following in my study
Home first to create a DAO interface

Java code
    1. Package Com.wys.dao;
    2. Public interface Iuserdao {
    3. void Save ();
    4. }



The first way to get jdbctemplate by inheriting Jdbcdaosupport is to inject jdbctemplate

Java code
  1. Package Com.wys.dao.impl;
  2. Import Java.util.Map;
  3. Import Org.springframework.jdbc.core.support.JdbcDaoSupport;
  4. Import Com.wys.dao.IUserDao;
  5. Public class Userdaoimpl extends Jdbcdaosupport implements Iuserdao {
  6. @Override
  7. public Void Save () {
  8. String sql = "**********";
  9. map<string,?> map = this.getjdbctemplate (). queryForMap (SQL);
  10. System.out.println ("Success!");
  11. }
  12. }


The configuration file is as follows:

Java code
    1. <bean id= "JdbcTemplate"   class= "Org.springframework.jdbc.core.JdbcTemplate" >  
    2.         <property name= "DataSource"  ref= "DataSource" &NBSP;/>&NBSP;&NBSP;
    3.     </bean>  
    4.     <bean id=< Span class= "string" > "Userimpdao"  class= " Com.wys.dao.impl.UserDaoImpl ">&NBSP;&NBSP;
    5.          <property name= "JdbcTemplate"  ref= " JdbcTemplate "&NBSP;/>&NBSP;&NBSP;
    6. </bean>  


JdbcTemplate provides a number of ways to manipulate the database
The second method is to combine the JdbcTemplate, because JdbcTemplate is created to inject datasource, so it only needs to be injected directly into the datasource to

Java code
  1. Package Com.wys.dao.impl;
  2. Import Java.util.Map;
  3. Import Javax.sql.DataSource;
  4. Import Org.springframework.context.ApplicationContext;
  5. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  6. Import Org.springframework.jdbc.core.JdbcTemplate;
  7. Import Org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  8. Import Org.springframework.jdbc.core.support.JdbcDaoSupport;
  9. Import Com.wys.dao.IUserDao;
  10. Public class Userdao2impl implements Iuserdao {
  11. private JdbcTemplate JdbcTemplate;
  12. @Override
  13. public Void Save () {
  14. String sql = "******";
  15. map<string,?> map = this.jdbcTemplate.queryForMap (SQL);
  16. System.out.println ("Success!");
  17. }
  18. //Injection DataSource
  19. public void Setdatasource (DataSource DataSource) {
  20. JdbcTemplate = new JdbcTemplate (DataSource);
  21. }
  22. }



Configuration file

Java code
    1. <bean id="Userimpl2dao" class="Com.wys.dao.impl.UserDao2Impl" >
    2. <property name="DataSource" ref="DataSource"/>
    3. </bean>



The third approach is to inherit the Simplejdbcdaosupport, which can be obtained through Simplejdbcdaosupport Simplejdbctemplate,simplejdbctemplate also provides a large number of methods to manipulate the database, Because Simplejdbcdaosupport inherits the Jdbcdaosupport, all the methods that can get jdbctemplate,jdbctemplate relative simplejdbctemplate provide more and more advanced operations, As needed, this only needs to be injected directly into the JdbcTemplate, because Simplejdbctemplate is created by JdbcTemplate.

Java code
  1. Package Com.wys.dao.impl;
  2. Import Java.util.Map;
  3. Import Javax.sql.DataSource;
  4. Import Org.springframework.context.ApplicationContext;
  5. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  6. Import Org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
  7. Import Org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  8. Import Org.springframework.jdbc.core.support.JdbcDaoSupport;
  9. Import Com.wys.dao.IUserDao;
  10. Public class Userdao3impl extends Simplejdbcdaosupport implements Iuserdao {
  11. @Override
  12. public Void Save () {
  13. String sql = "* * *";
  14. map<string,object> map = this.getsimplejdbctemplate (). queryForMap (SQL); Simplejdbctemplate
  15. map<string,object> map2 = this.getjdbctemplate (). queryForMap (SQL); JdbcTemplate
  16. System.out.println ("Success!");
  17. }
  18. }


The configuration is as follows:

Java code
    1. <bean id="Userimpl3dao" class="Com.wys.dao.impl.UserDao3Impl" >
    2. <property name="JdbcTemplate" ref="JdbcTemplate"/>
    3. </bean>


The fourth is a direct combination of simplejdbctemplate, because Simplejdbctemplate is created to be datasource, so it needs to be injected datasource

Java code
  1. Package Com.wys.dao.impl;
  2. Import Java.util.Map;
  3. Import Javax.sql.DataSource;
  4. Import Org.springframework.context.ApplicationContext;
  5. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  6. Import Org.springframework.jdbc.core.JdbcTemplate;
  7. Import Org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  8. Import Org.springframework.jdbc.core.support.JdbcDaoSupport;
  9. Import Com.wys.dao.IUserDao;
  10. Public class Userdao4impl implements Iuserdao {
  11. private simplejdbctemplate JdbcTemplate;
  12. @Override
  13. public Void Save () {
  14. String sql = "******";
  15. map<string,?> map = this.jdbcTemplate.queryForMap (SQL);
  16. System.out.println ("Success!");
  17. }
  18. public void Setdatasource (DataSource DataSource) {
  19. JdbcTemplate = new Simplejdbctemplate (DataSource);
  20. }
  21. }


Configuration file:

Java code
    1. <bean id="Userimpl4dao" class="Com.wys.dao.impl.UserDao4Impl" >
    2. <property name="DataSource" ref="DataSource"/>
    3. </bean>



The fifth method is also a group full jdbctemplate, directly injected JdbcTemplate, rather than DataSource, because in JdbcTemplate has been injected datasource

Java code
  1. Package Com.wys.dao.impl;
  2. Import Java.util.Map;
  3. Import Javax.sql.DataSource;
  4. Import Org.springframework.context.ApplicationContext;
  5. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  6. Import Org.springframework.jdbc.core.JdbcTemplate;
  7. Import Org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  8. Import Org.springframework.jdbc.core.support.JdbcDaoSupport;
  9. Import Com.wys.dao.IUserDao;
  10. Public class Userdao5impl implements Iuserdao {
  11. private JdbcTemplate JdbcTemplate;
  12. public void Setjdbctemplate (JdbcTemplate jdbctemplate) {
  13. this.jdbctemplate = JdbcTemplate;
  14. }
  15. @Override
  16. public Void Save () {
  17. String sql = "* * * * *";
  18. map<string,?> map = this.jdbcTemplate.queryForMap (SQL);
  19. System.out.println ("Success!");
  20. }
  21. }



Configured as follows

Java code
    1. <bean id="JdbcTemplate" class="Org.springframework.jdbc.core.JdbcTemplate" >
    2. <property name="DataSource" ref="DataSource"/>
    3. </bean>
    4. <bean id="Userimpl5dao" class="Com.wys.dao.impl.UserDao5Impl" >
    5. <property name="JdbcTemplate" ref="JdbcTemplate"/>
    6. </bean>



In fact, there are many methods, the key is to see whether you need to inherit or combination, need to inject the object is what!

[Go] springjdbc several different uses

Related Article

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.