Detailed use of sqlmapclienttemplate in Ibaits

Source: Internet
Author: User

Apache IBatis (now moving to Google Code, renamed MyBatis) is a very broad, semi-automatic ORM framework used in the current IT project, which differs from the fully automated framework such as hibernate, iBatis more flexible control over the operation of the database. , Ibatis is a great choice for developers who often need to invoke local database functions for custom SQL statements, or who prefer to optimize their SQL execution efficiency . And the wide application of open source Enterprise Architecture Springframework, but also very good integration, making ibatis in springframework use more convenient and fast. All the developers have to do is inherit the Sqlmapclientdaosupport class provided in the Springframework. Below, I will use my own experience to share with you:

1, the Assembly of Sqlmapclientfactorybean

Sqlmapclientfactorybean is the basis for sqlmapclienttemplate use, if the

There is no assembly Sqlmapclientfactorybean in the Springframework application, then Sqlmapclienttemplate will not be available and the null pointer is wrong.

Java code

  1. <Beanid= "sqlmapclient"class="Org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
  2. <propertyname= "configlocation"value="/web-inf/sqlmap-config.xml"/>
  3. <propertyname= "dataSource"ref="DataSource"/>
  4. <propertyname= "lobhandler"ref="Oraclelobhandler"/>
  5. Bean>

2. Inheriting and using Sqlmapclientdaosupport class

Declaring a Java class:

Java code

    1. ......
    2. Import Org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
    3. ......
    4. Publicclass Reportdaoimpl extends Sqlmapclientdaosupport {
    5. ......
    6. }
    7. To assemble a Java class in a springframework configuration file:
    8. "Reportdao"class="Com.test.dao.ReportDAOImpl" >
    9. "Sqlmapclient" ref="sqlmapclient"/>

To assemble a Java class in a springframework configuration file:

3, use sqlmapclienttemplate query

Java code:

When you execute a query that has no parameters:

Java code

    1. List result = Getsqlmapclienttemplate (). queryForList ("testspace.qrytest");

"Testspace" is the namespace of the Ibatis Sqlmap file; " Qrytest "Query method ID for Ibatis Sqlmap

When a record information is obtained by the primary key:

Java code

    1. Long id = new Long ("2");
    2. Object resultobj = Getsqlmapclienttemplate (). queryForObject ("testspace.gettest", id);

When you query by certain criteria:

Java code

    1. Objecta Obja = new Objecta ();
    2. OBJA.SETPARAM1 ("test1");
    3. Obja.setparam2 ("test2");
    4. ......
    5. List result = Getsqlmapclienttemplate (). queryForList ("Testspace.qrytestbyparam", obja);

If you need to fetch 4~40 data:

List result = Getsqlmapclienttemplate (). queryForList ("Testspace.qrytestbyparam", Obja, 4, 40);

You can also return to map

Java code

    1. Map result = Getsqlmapclienttemplate (). queryForMap ("Testspace.qrytestbyparam", Obja, "Mapkey");

4. Add data using Sqlmapclienttemplate

Java code:

    1. Objecta Obja = new Objecta ();

Java code

    1. OBJA.SETPARAM1 ("test1");
    2. Obja.setparam2 ("test2");
    3. ......
    4. Getsqlmapclienttemplate (). Insert ("Testspace.inserttest", obja);

5. Update data with Sqlmapclienttemplate

Java code:

Java code

    1. Objecta Obja = new Objecta ();
    2. OBJA.SETPARAM1 ("test1");
    3. Obja.setparam2 ("test2");
    4. ......
    5. Getsqlmapclienttemplate (). Update ("Testspace.updatetest", obja);

Update the first 20 records:

Java code

    1. Getsqlmapclienttemplate (). Update ("Testspace.updatetest", Obja, );

6. Delete Data using Sqlmapclienttemplate

Java code:

Java code

    1. Long id = new Long ("2");
    2. Getsqlmapclienttemplate (). Delete ("testspace.deletetest", id);

Detailed use of sqlmapclienttemplate in Ibaits

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.