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
- <Beanid= "sqlmapclient"class="Org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
- <propertyname= "configlocation"value="/web-inf/sqlmap-config.xml"/>
- <propertyname= "dataSource"ref="DataSource"/>
- <propertyname= "lobhandler"ref="Oraclelobhandler"/>
- Bean>
2. Inheriting and using Sqlmapclientdaosupport class
Declaring a Java class:
Java code
- ......
- Import Org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
- ......
- Publicclass Reportdaoimpl extends Sqlmapclientdaosupport {
- ......
- }
- To assemble a Java class in a springframework configuration file:
- "Reportdao"class="Com.test.dao.ReportDAOImpl" >
- "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
- 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
- Long id = new Long ("2");
- Object resultobj = Getsqlmapclienttemplate (). queryForObject ("testspace.gettest", id);
When you query by certain criteria:
Java code
- Objecta Obja = new Objecta ();
- OBJA.SETPARAM1 ("test1");
- Obja.setparam2 ("test2");
- ......
- 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
- Map result = Getsqlmapclienttemplate (). queryForMap ("Testspace.qrytestbyparam", Obja, "Mapkey");
4. Add data using Sqlmapclienttemplate
Java code:
- Objecta Obja = new Objecta ();
Java code
- OBJA.SETPARAM1 ("test1");
- Obja.setparam2 ("test2");
- ......
- Getsqlmapclienttemplate (). Insert ("Testspace.inserttest", obja);
5. Update data with Sqlmapclienttemplate
Java code:
Java code
- Objecta Obja = new Objecta ();
- OBJA.SETPARAM1 ("test1");
- Obja.setparam2 ("test2");
- ......
- Getsqlmapclienttemplate (). Update ("Testspace.updatetest", obja);
Update the first 20 records:
Java code
- Getsqlmapclienttemplate (). Update ("Testspace.updatetest", Obja, );
6. Delete Data using Sqlmapclienttemplate
Java code:
Java code
- Long id = new Long ("2");
- Getsqlmapclienttemplate (). Delete ("testspace.deletetest", id);
Detailed use of sqlmapclienttemplate in Ibaits